Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 7, 2015, 4:06:37 PM (8 years ago)
Author:
gania
Message:

fix for sigsegv?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/campaignHS15/src/orxonox/controllers/FlyingController.cc

    r10923 r10946  
    99 *   modify it under the terms of the GNU General Public License
    1010 *   as published by the Free Software Foundation; either version 2
    11  *   of the License, or ( at your option )any later version.
     11 *   of the License, or (at your option)any later version.
    1212 *
    1313 *   This program is distributed in the hope that it will be useful,
     
    2727 *
    2828 */
     29#include <OgreMatrix3.h>                        //for Quaternion manipulations
     30
     31#include "util/Math.h"                         
     32#include "core/XMLPort.h"
    2933#include "controllers/FlyingController.h"
    30 #include "core/XMLPort.h"
    31 #include "worldentities/pawns/SpaceShip.h"
    32 #include "util/Math.h"
    33 #include <OgreMatrix3.h>
     34
     35#include "worldentities/pawns/SpaceShip.h"      //for boosting
    3436
    3537namespace orxonox
     
    6062        FormationMode::Value value;
    6163       
    62         if ( valUpper == "WALL" )
     64        if (valUpper == "WALL")
    6365            value = FormationMode::WALL;
    64         else if ( valUpper == "FINGER4" )
     66        else if (valUpper == "FINGER4")
    6567            value = FormationMode::FINGER4;
    66         else if ( valUpper == "DIAMOND" )
     68        else if (valUpper == "DIAMOND")
    6769            value = FormationMode::DIAMOND;
    6870        else
    69             ThrowException(ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'.");
     71            ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '")+ val + "'.");
    7072        this->setFormationMode(value);
    7173    }
    7274    std::string FlyingController::getFormationModeXML() const
    7375    {
    74         switch ( this->formationMode_ )
     76        switch (this->formationMode_)
    7577        {
    7678            case FormationMode::WALL:
     
    8890        this->bHasTargetPosition_ = false;
    8991    }
     92    /**
     93    @brief
     94      if distance to targetPosition is smaller than this->tolerance_, no moving should be made, otherwise
     95      find amount of yaw and pitch that have to be applied, so that ship looks at targetPosition, then
     96      ship is moved forward towards targetPosition. Also target orientation is being applied.
     97    */
    9098    void FlyingController::moveToPosition(const Vector3& targetPosition, float dt)
    9199    {
     
    94102        ControllableEntity* entity = this->getControllableEntity();
    95103
    96         float distance = ( targetPosition - entity->getPosition() ).length();
    97 
    98         if ( distance >= this->tolerance_ )
    99         {
     104        float distance = (targetPosition - entity->getPosition()).length();
     105
     106        if (distance >= this->tolerance_)
     107        {
     108            //function that calculates how much yaw and pitch are to be applied
    100109            Vector2 coord = get2DViewCoordinates
    101                 ( entity->getPosition() ,
     110                (entity->getPosition() ,
    102111                entity->getOrientation()  * WorldEntity::FRONT,
    103112                entity->getOrientation()  * WorldEntity::UP,
    104                 targetPosition );
    105             float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
    106             float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
    107             entity->rotateYaw( ROTATEFACTOR * rotateX * dt );
    108             entity->rotatePitch( ROTATEFACTOR * rotateY * dt );
    109          
     113                targetPosition);
     114            //limit yaw and pitch by [-1,1]
     115            float rotateX = -clamp(coord.x * 10, -1.0f, 1.0f);
     116            float rotateY = clamp(coord.y * 10, -1.0f, 1.0f);
     117
     118            if (!entity)
     119                return;
     120
     121            //apply yaw and pitch
     122            entity->rotateYaw(ROTATEFACTOR * rotateX * dt);
     123            entity->rotatePitch(ROTATEFACTOR * rotateY * dt);
     124           
     125            if (!entity)
     126                return;
     127           
     128            //only move either if ship looks at target with a certain tolerance, or if ship is far enough for it to be ok to move in a curve.
    110129            if (distance > this->tolerance_*1.5f || (rotateX > -0.03 && rotateX < 0.03 && rotateY > -0.03 && rotateY < 0.03))
    111                 entity->moveFrontBack( SPEED * dt );
    112                 copyTargetOrientation(dt);
     130                entity->moveFrontBack(SPEED * dt);
     131            //roll
     132            copyTargetOrientation(dt);
    113133        }
    114134        else
     
    117137        }
    118138    }
    119    
     139    /**
     140    @brief
     141      fly towards a preset targetPosition_
     142    */
    120143    void FlyingController::moveToTargetPosition(float dt)
    121144    {
    122145        this->moveToPosition (this->targetPosition_, dt);
    123146    }
     147    /**
     148    @brief
     149      roll ship so that it has same roll as orient
     150    */
    124151    void FlyingController::copyOrientation(const Quaternion& orient, float dt)
    125152    {
     
    155182        }
    156183    }
    157 
     184    /**
     185    @brief
     186      roll ship so that it has same roll as a preset targetOrientation_
     187    */
    158188    void FlyingController::copyTargetOrientation(float dt)
    159189    {
     
    163193        }
    164194    }
    165    
     195    /**
     196    @brief
     197      set Vector to fly to
     198    */
    166199    void FlyingController::setTargetPosition(const Vector3& target)
    167200    {
     
    169202        this->bHasTargetPosition_ = true;
    170203    }
    171 
     204    /**
     205    @brief
     206      set orientation to apply
     207    */
    172208    void FlyingController::setTargetOrientation(const Quaternion& orient)
    173209    {
     
    175211        this->bHasTargetOrientation_=true;
    176212    }
    177 
     213    /**
     214    @brief
     215      set orientation to apply
     216    */
    178217    void FlyingController::setTargetOrientation(ControllableEntity* target)
    179218    {
     
    181220            this->setTargetOrientation(target->getOrientation());
    182221    }
     222    /**
     223    @brief
     224      boost if you can
     225    */
    183226    void FlyingController::boostControl()
    184227    {
     
    187230        SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity());
    188231        if(ship == NULL) return;
    189         if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost
     232        if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower()) //upper limit ->boost
    190233        {
    191234            this->getControllableEntity()->boost(true);
     
    196239        }
    197240    }
     241    /**
     242    @brief
     243      keep this ship in a formation with its division
     244    */
    198245    void FlyingController::keepFormation(const ControllableEntity* leaderEntity, Vector3& targetRelativePosition)
    199246    {
     247        if (!this->getControllableEntity())
     248            return;
    200249        ControllableEntity* myEntity = this->getControllableEntity();
    201250        Vector3 myPosition = myEntity->getWorldPosition();
     
    212261            return;
    213262        }
     263        //calculate where in world coordinates this ship should fly
    214264        Vector3 targetAbsolutePosition =
    215265            (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5)
    216266             + (orient* (targetRelativePosition)));
    217         //let ship finish rotating. also don't call copyOrientation to often as it is a slow function.
     267        //let ship finish rotating. also don't call copyOrientation too often as it is a slow function. Don't know how to do it different
    218268        if (static_cast<int>(rnd(1.0f) * 100) % 3 == 0)
    219269            this->setTargetOrientation (orient);
     270        //set a position to fly to
    220271        this->setTargetPosition (targetAbsolutePosition);
     272
     273        //boost if too far
    221274        if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f)
    222275        {
     
    225278        else
    226279        {
    227            this->getControllableEntity()->boost(false);
     280            if (!this->getControllableEntity())
     281                return;
     282            this->getControllableEntity()->boost(false);
    228283        }
    229284    }
Note: See TracChangeset for help on using the changeset viewer.