Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 7, 2015, 8:34:20 AM (8 years ago)
Author:
gania
Message:

check in

File:
1 edited

Legend:

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

    r10888 r10923  
    2424
    2525 *   Co-authors:
    26  *      Dominik Solenicki
     26 *   ...
    2727 *
    2828 */
     
    4646    FlyingController::~FlyingController()
    4747    {
    48 
    4948    }
    5049
     
    7675        {
    7776            case FormationMode::WALL:
    78             { return "WALL"; break; }
     77            { return "WALL"; }
    7978            case FormationMode::FINGER4:
    80             { return "FINGER4"; break; }
     79            { return "FINGER4"; }
    8180            case FormationMode::DIAMOND:
    82             { return "DIAMOND"; break; }
     81            { return "DIAMOND"; }
    8382            default:
    84                 return "DIAMOND"; break;
     83                return "DIAMOND";
    8584        }
    8685    }
     
    8988        this->bHasTargetPosition_ = false;
    9089    }
    91     void FlyingController::moveToPosition(const Vector3& target, float dt)
    92     {
     90    void FlyingController::moveToPosition(const Vector3& targetPosition, float dt)
     91    {
     92        if (!this->getControllableEntity())
     93            return;
    9394        ControllableEntity* entity = this->getControllableEntity();
    9495
    95         float distance = ( target - entity->getPosition() ).length();
     96        float distance = ( targetPosition - entity->getPosition() ).length();
    9697
    9798        if ( distance >= this->tolerance_ )
     
    101102                entity->getOrientation()  * WorldEntity::FRONT,
    102103                entity->getOrientation()  * WorldEntity::UP,
    103                 target );
     104                targetPosition );
    104105            float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
    105106            float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
     
    107108            entity->rotatePitch( ROTATEFACTOR * rotateY * dt );
    108109         
    109             if (distance > this->tolerance_*1.5f || (rotateX > -0.01 && rotateX < 0.01 && rotateY > -0.01 && rotateY < 0.01))
     110            if (distance > this->tolerance_*1.5f || (rotateX > -0.03 && rotateX < 0.03 && rotateY > -0.03 && rotateY < 0.03))
    110111                entity->moveFrontBack( SPEED * dt );
    111112                copyTargetOrientation(dt);
     
    123124    void FlyingController::copyOrientation(const Quaternion& orient, float dt)
    124125    {
    125         //inspired by
     126        //copied from
    126127        //http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Quaternion+and+Rotation+Primer&structure=Tutorials#Q._How_can_I_make_my_objects_rotate_smoothly_You_mentioned_slerp_etc_
    127128        //how can I make my objects rotate smoothly?
    128        
     129        if (!this->getControllableEntity())
     130            return;
    129131        Quaternion myOrient = this->getControllableEntity()->getOrientation();
    130132        this->rotationProgress_ += dt;
     
    137139        else
    138140        {
    139 
    140             Quaternion delta = Quaternion::Slerp(rotationProgress_, myOrient, orient, true);
     141            Quaternion deltaOrientation = Quaternion::Slerp(rotationProgress_, myOrient, orient, true);
    141142           
    142             //rotate roll builds a Quaternion in roll method of WorldEntity, then it sets orientation.
    143             //it is faster just to set orientation, plus that way there is no need in calculating the roll angle.
    144             //On the downside, however, ship might also yaw and pitch, but this effect is neglectable, as we only call
    145             //copyOrientation after we move our ship, thus it doesn't affect ships's flying direction too much.
    146             //If you don't like the code style, you are welcomed to uncomment the code below
    147             //and comment out setOrientation part, it will work just fine, but it will also be a tiny bit slower.
    148             //P.S. apperantly it did affect ship's direction and did so way too much.
    149             Matrix3 orientMatrix, myMatrix;
    150 
    151             delta.ToRotationMatrix(orientMatrix);
     143            Matrix3 deltaMatrix, myMatrix;
     144
     145            deltaOrientation.ToRotationMatrix(deltaMatrix);
    152146            myOrient.ToRotationMatrix (myMatrix);
    153147
    154             Radian yRad, pRad, rRad, yMy, pMy, rMy;
    155             orientMatrix.ToEulerAnglesYXZ(yRad, pRad, rRad);
    156             myMatrix.ToEulerAnglesYXZ (yMy, pMy, rMy);
    157 
    158             this->getControllableEntity()->rotateRoll ((rRad.valueRadians() - rMy.valueRadians())*ROTATEFACTOR*dt);
    159             // this->getControllableEntity()->setOrientation(delta);
    160         }
    161        
    162        
    163     }
    164     //change log: increased precision, increased rotation speed
     148            Radian yawDelta, pitchDelta, rollDelta, yawMy, pitchMy, rollMy;
     149            deltaMatrix.ToEulerAnglesYXZ(yawDelta, pitchDelta, rollDelta);
     150            myMatrix.ToEulerAnglesYXZ (yawMy, pitchMy, rollMy);
     151
     152            if (!this->getControllableEntity())
     153                return;
     154            this->getControllableEntity()->rotateRoll ((rollDelta.valueRadians() - rollMy.valueRadians())*ROTATEFACTOR*dt);
     155        }
     156    }
     157
    165158    void FlyingController::copyTargetOrientation(float dt)
    166159    {
    167160        if (bHasTargetOrientation_)
    168161        {   
    169             copyOrientation(targetOrientation_, dt);
     162            this->copyOrientation(targetOrientation_, dt);
    170163        }
    171164    }
     
    186179    {
    187180        if (target)
    188             setTargetOrientation(target->getOrientation());
     181            this->setTargetOrientation(target->getOrientation());
    189182    }
    190183    void FlyingController::boostControl()
    191184    {
     185        if (!this->getControllableEntity())
     186            return;
    192187        SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity());
    193188        if(ship == NULL) return;
     
    221216             + (orient* (targetRelativePosition)));
    222217        //let ship finish rotating. also don't call copyOrientation to often as it is a slow function.
    223         if (this->actionCounter_ % 6 == 0 && !this->bHasTargetOrientation_)
     218        if (static_cast<int>(rnd(1.0f) * 100) % 3 == 0)
    224219            this->setTargetOrientation (orient);
    225220        this->setTargetPosition (targetAbsolutePosition);
Note: See TracChangeset for help on using the changeset viewer.