Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 31, 2015, 12:20:00 PM (10 years ago)
Author:
gania
Message:

move functions were added, everyone stays in formations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc

    r10725 r10729  
    3232
    3333    RegisterClass(CommonController);
     34    static const float SPEED = 0.6f;
     35    static const float ROTATEFACTOR = 0.2f;
    3436
     37    /*static const float SPEED_FREE = 0.8f;
     38    static const float ROTATEFACTOR_FREE = 0.8f;*/
    3539   
    3640    bool CommonController::setWingman (CommonController* wingman)
     
    5155
    5256        RegisterObject(CommonController);
    53         //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&LeaderController::action, this)));
    5457    }
    5558
     
    5861    {
    5962    }
     63    //copy the Roll orientation of given Quaternion.
     64    void CommonController::copyOrientation(const Quaternion& orient)
     65    {
     66        //roll angle difference in radian
     67        float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians());
     68        while(diff>math::twoPi) diff-=math::twoPi;
     69        while(diff<-math::twoPi) diff+=math::twoPi;
     70        this->getControllableEntity()->rotateRoll(-diff);
     71
     72
     73
     74    }
     75    void CommonController::setTargetPosition(const Vector3& target)
     76    {
     77        this->targetPosition_ = target;
     78        this->bHasTargetPosition_ = true;
     79    }
     80
     81    void CommonController::copyTargetOrientation()
     82    {
     83        if (bHasTargetOrientation_)
     84        {   
     85            copyOrientation(targetOrientation_);
     86        }
     87    }
     88    void CommonController::setTargetOrientation(const Quaternion& orient)
     89    {
     90        this->targetOrientation_=orient;
     91        this->bHasTargetOrientation_=true;
     92    }
     93    void CommonController::setTargetOrientation(ControllableEntity* target)
     94    {
     95        if (target)
     96            setTargetOrientation(target->getOrientation());
     97    }
     98
    6099
    61100    void CommonController::moveToPosition(const Vector3& target)
     
    63102        if (!this->getControllableEntity())
    64103            return;
     104       
     105        //100 is (so far) the smallest tolerance (empirically found) that can be reached,
     106        //with smaller distance spaceships can't reach position and go circles around it instead
     107        int tolerance = 100;
    65108
    66         Vector2 coord = get2DViewCoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
     109        ControllableEntity* entity = this->getControllableEntity();
     110        Vector2 coord = get2DViewCoordinates
     111            (entity->getPosition(),
     112            entity->getOrientation() * WorldEntity::FRONT,
     113            entity->getOrientation() * WorldEntity::UP,
     114            target);
     115
    67116        float distance = (target - this->getControllableEntity()->getPosition()).length();
     117
     118        //rotates should be in range [-1,+1], clamp cuts off all that is not
    68119        float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
    69120        float rotateY = clamp(coord.y * 10, -1.0f, 1.0f);
    70121
    71122       
    72         if (this->target_ || distance > 10)
     123        if (distance > tolerance)
    73124        {
    74             this->getControllableEntity()->rotateYaw(-1.0f * 0.8f * rotateX);
    75             this->getControllableEntity()->rotatePitch(0.8f * rotateY);
     125            //Yaw and Pitch are enough to start facing the target
     126            this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX);
     127            this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY);
     128
     129            //300 works, maybe less is better
     130            if (distance < 300)
     131            {
     132                //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory
     133                //It's important that roll is not changed in the process of changing yaw and pitch
     134                //Wingmen won't face same direction as Leaders, but when Leaders start moving
     135                //Yaw and Pitch will adapt.
     136                if (bHasTargetOrientation_)
     137                {
     138                    copyTargetOrientation();
     139                }
     140            }
     141            this->getControllableEntity()->moveFrontBack(1.2f*SPEED);
     142        }
     143        else
     144        {     
     145            bHasTargetPosition_ = false;
     146            bHasTargetOrientation_ = false;
    76147        }
    77148
    78         if (this->target_ && distance <  200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
    79         {
    80             this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
    81         }
    82         else if (distance > 100)
    83             this->getControllableEntity()->moveFrontBack(0.8f);
    84        
    85 
    86 
    87         if (distance < 100)
    88         {
    89             this->positionReached();
    90             bHasTargetOrientation_=false;
    91         }
     149     
     150    }
     151    void CommonController::moveToTargetPosition()
     152    {
     153        this->moveToPosition(this->targetPosition_);
    92154    }
    93155 
Note: See TracChangeset for help on using the changeset viewer.