Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10799


Ignore:
Timestamp:
Nov 12, 2015, 1:03:34 PM (8 years ago)
Author:
gania
Message:

prettier now

File:
1 edited

Legend:

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

    r10798 r10799  
    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,
     
    4444{
    4545
    46     RegisterClass(CommonController);
     46    RegisterClass( CommonController );
    4747    float SPEED = 0.7f/0.02f;
    4848    float ROTATEFACTOR = 0.3f/0.02f;
    4949
    50     CommonController::CommonController(Context* context) : Controller(context)
     50    CommonController::CommonController( Context* context ): Controller( context )
    5151    {
    5252        this->bSetupWorked = false;
     
    5656
    5757        this->maneuverType_ = ManeuverType::NONE;
    58         RegisterObject(CommonController);
    59     }
    60 
    61 
    62     CommonController::~CommonController()
    63     {
    64     }
    65 
    66     void CommonController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    67     {
    68         SUPER(CommonController, XMLPort, xmlelement, mode);
    69         XMLPortParam(CommonController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode);
    70 
    71     }
    72     void CommonController::setFormationModeXML(std::string val)
    73     {
    74         const std::string valUpper = getUppercase(val);
     58        RegisterObject( CommonController );
     59    }
     60
     61
     62    CommonController::~CommonController() 
     63    {
     64    }
     65
     66    void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode )
     67    {
     68        SUPER( CommonController, XMLPort, xmlelement, mode );
     69        XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode );
     70
     71    }
     72    void CommonController::setFormationModeXML( std::string val )
     73    {
     74        const std::string valUpper = getUppercase( val );
    7575        FormationMode::Value value;
    76         if (valUpper == "VEE")
     76        if ( valUpper == "VEE" )
    7777            value = FormationMode::VEE;
    78         else if (valUpper == "WALL")
     78        else if ( valUpper == "WALL" )
    7979            value = FormationMode::WALL;
    80         else if (valUpper == "FINGER4")
     80        else if ( valUpper == "FINGER4" )
    8181            value = FormationMode::FINGER4;
    82         else if (valUpper == "DIAMOND")
     82        else if ( valUpper == "DIAMOND" )
    8383            value = FormationMode::DIAMOND;
    8484        else
    85             ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '") + val + "'.");
    86         this->setFormationMode(value);
     85            ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." );
     86        this->setFormationMode( value );
    8787       
    8888    }
    89     std::string CommonController::getFormationModeXML()
    90     {
    91         switch (this->formationMode_)
     89    std::string CommonController::getFormationModeXML() 
     90    {
     91        switch ( this->formationMode_ )
    9292        {
    9393            case FormationMode::VEE:
     
    117117        }
    118118    }
    119     void CommonController::maneuver()
    120     {
    121 
    122         if (this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity())
    123         {
    124             Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
     119    void CommonController::maneuver()
     120    {
     121
     122        if ( this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity() )
     123        {
     124            Vector3 thisPosition = this->getControllableEntity() ->getWorldPosition();
     125            Quaternion thisOrientation = this->getControllableEntity() ->getOrientation();
     126
     127            /*this->setPositionOfTarget( getPredictedPosition(
     128                thisPosition,
     129                hardcoded_projectile_speed,
     130                this->target_->getWorldPosition() ,
     131                this->target_->getVelocity() 
     132                ) );*/
     133            this->setPositionOfTarget( this->target_->getWorldPosition()  );
     134            this->setOrientationOfTarget( this->target_->getControllableEntity() ->getOrientation() );
     135
     136
     137            Vector3 diffVector = this->positionOfTarget_ - thisPosition;
    125138            float diffLength = diffVector.length();
    126139            Vector3 diffUnit = diffVector/diffLength;
    127140
    128             Vector3 myForwardVector = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT;
    129             float myDotProduct = diffVector.dotProduct(myForwardVector);
    130 
    131             Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT;
    132             float opponentDotProduct = diffVector.dotProduct(opponentForwardVector);           
    133 
    134             float myAngle = math::arccos ( myForwardVector.dotProduct(diffVector)/(diffLength) );
    135             float targetAngle = math::arccos ( opponentForwardVector.dotProduct(-diffVector)/(diffLength) );
     141            Vector3 thisForwardVector = thisOrientation * WorldEntity::FRONT;
     142            float thisDotProduct = diffVector.dotProduct( thisForwardVector );
     143
     144            Vector3 targetForwardVector = this->orientationOfTarget_ * WorldEntity::FRONT;
     145            float targetDotProduct = diffVector.dotProduct( targetForwardVector );           
     146
     147            float thisAngle = getAngle( thisPosition, thisForwardVector, this->positionOfTarget_ );
     148            float targetAngle = getAngle( this->positionOfTarget_, targetForwardVector, thisPosition );
     149
     150
     151            bool bThisIsLookingAtTarget = ( thisAngle/( diffLength*diffLength ) < math::pi/8000000.0f );
     152            bool bTargetIsLookingAtThis = ( targetAngle/( diffLength*diffLength ) < math::pi/8000000.0f );
    136153           
    137             bool bThisIsLookingAtTarget = (myAngle/(diffLength*diffLength) < math::pi/8000000.0f);
    138             bool bTargetIsLookingAtThis = (targetAngle/(diffLength*diffLength) < math::pi/8000000.0f);
    139            
    140             float angleDiff = targetAngle - myAngle;
     154            float angleDiff = targetAngle - thisAngle;
    141155
    142156            //if his angle is bigger than mine
     
    152166                        moveToPoint(
    153167                            *target,
    154                             randomInRange(45, 180)
     168                            randomInRange( 45, 180 )
    155169                            );
    156170                    }
     
    158172                    else
    159173                    {
    160                         Vector3 target = (diffUnit) * 150.0f
     174                        Vector3 target = ( diffUnit )* 150.0f
    161175                        Vector3* randVector = new Vector3(
    162                             randomInRange(-300, 300),
    163                             randomInRange(-300, 300),
    164                             randomInRange(-300, 300)
     176                            randomInRange( -300, 300 ),
     177                            randomInRange( -300, 300 ),
     178                            randomInRange( -300, 300 )
    165179                        );
    166                         Vector3 projection = randVector.dotProduct(diffUnit) * diffUnit;
     180                        Vector3 projection = randVector.dotProduct( diffUnit )* diffUnit;
    167181                        *randVector -= projection;
    168182                        target += randVector;
    169183                        moveToPoint(
    170184                            *target,
    171                             randomInRange(45, 180)
     185                            randomInRange( 45, 180 )
    172186                            );
    173187                    }
     
    179193                    if ( diffLength < 300 )
    180194                    {
    181                         this->setTargetPosition( this->getControllableEntity()->getWorldPosition() );
     195                        this->setTargetPosition( this->getControllableEntity() ->getWorldPosition() );
    182196                    }
    183197                    //move closer
    184198                    else
    185199                    {
    186                         this->setTargetPosition( this->positionOfTarget_ - 0.6f*diffVector);
     200                        this->setTargetPosition( this->positionOfTarget_ - 0.6f*diffVector );
    187201                    }
    188202                }
     
    190204           
    191205        }
    192         if (this->getControllableEntity() && !this->target_)
     206        if ( this->getControllableEntity()  && !this->target_ )
    193207        {
    194208            this->maneuverType_ = ManeuverType::NONE;
    195209        }
    196         orxout (internal_error) << "ManeuverType = " << this->maneuverType_ << endl;
    197     }
    198     void CommonController::chooseManeuverType()
    199     {
    200 
    201         if (this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity())
    202         {
    203             Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
    204             Vector3 myForwardVector = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT;
    205             float myDotProduct = diffVector.dotProduct(myForwardVector);
    206 
    207             Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT;
    208             float opponentDotProduct = diffVector.dotProduct(opponentForwardVector);           
    209 
    210 
    211             switch ((myDotProduct > 0) - (myDotProduct < 0))
     210        orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl;
     211    }
     212    void CommonController::chooseManeuverType() 
     213    {
     214
     215        if ( this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity() )
     216        {
     217            Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();
     218            Vector3 thisForwardVector = this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT;
     219            float thisDotProduct = diffVector.dotProduct( thisForwardVector );
     220
     221            Vector3 targetForwardVector = this->target_->getOrientation() * WorldEntity::FRONT;
     222            float targetDotProduct = diffVector.dotProduct( targetForwardVector );           
     223
     224
     225            switch ( ( thisDotProduct > 0 )- ( thisDotProduct < 0 ))
    212226            {
    213227                case 1:
    214228                {
    215                     switch ((opponentDotProduct > 0) - (opponentDotProduct < 0))
     229                    switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 ))
    216230                    {
    217231                        case 1:
     
    235249                case 0:
    236250                {
    237                     switch ((opponentDotProduct > 0) - (opponentDotProduct < 0))
     251                    switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 ))
    238252                    {
    239253                        case 1:
     
    258272                case -1:
    259273                {
    260                     switch ((opponentDotProduct > 0) - (opponentDotProduct < 0))
     274                    switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 ))
    261275                    {
    262276                        case 1:
     
    280294            }
    281295        }
    282         if (this->getControllableEntity() && !this->target_)
     296        if ( this->getControllableEntity()  && !this->target_ )
    283297        {
    284298            this->maneuverType_ = ManeuverType::NONE;
    285299        }
    286         orxout (internal_error) << "ManeuverType = " << this->maneuverType_ << endl;
    287     }
    288     bool CommonController::setWingman (CommonController* wingman)
     300        orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl;
     301    }
     302    bool CommonController::setWingman ( CommonController* wingman )
    289303    {
    290304        return false;
    291305    }
    292306   
    293     bool CommonController::hasWingman()
     307    bool CommonController::hasWingman() 
    294308    {
    295309        return true;
    296310    }
    297     void CommonController::setTarget(ControllableEntity* target)
     311    void CommonController::setTarget( ControllableEntity* target )
    298312    {
    299313        this->target_ = target;
    300         orxout (internal_error) << " TARGET SET " << endl;
     314        orxout ( internal_error ) << " TARGET SET " << endl;
    301315       
    302         if (this->target_ )
    303         {
    304             this->setPositionOfTarget(target_->getWorldPosition());
    305 
    306         }
    307     }
    308     bool CommonController::hasTarget()
    309     {
    310         if (this->target_)
     316        if ( this->target_ )
     317        {
     318            this->setPositionOfTarget( target_->getWorldPosition() );
     319
     320        }
     321    }
     322    bool CommonController::hasTarget() 
     323    {
     324        if ( this->target_ )
    311325            return true;
    312326        return false;
    313327    }
    314     void CommonController::setPositionOfTarget(const Vector3& target)
     328    void CommonController::setPositionOfTarget( const Vector3& target )
    315329    {
    316330        this->positionOfTarget_ = target;
    317331        this->bHasPositionOfTarget_ = true;
    318332    }
    319     void CommonController::setOrientationOfTarget(const Quaternion& orient)
     333    void CommonController::setOrientationOfTarget( const Quaternion& orient )
    320334    {
    321335        this->orientationOfTarget_=orient;
     
    323337    }
    324338
    325     void CommonController::setTargetPosition(const Vector3& target)
     339    void CommonController::setTargetPosition( const Vector3& target )
    326340    {
    327341        this->targetPosition_ = target;
     
    329343    }
    330344
    331     void CommonController::setTargetOrientation(const Quaternion& orient)
     345    void CommonController::setTargetOrientation( const Quaternion& orient )
    332346    {
    333347        this->targetOrientation_=orient;
     
    335349    }
    336350
    337     void CommonController::setTargetOrientation(ControllableEntity* target)
    338     {
    339         if (target)
    340             setTargetOrientation(target->getOrientation());
    341     }
    342 
    343     /*void CommonController::spin()
     351    void CommonController::setTargetOrientation( ControllableEntity* target )
     352    {
     353        if ( target )
     354            setTargetOrientation( target->getOrientation() );
     355    }
     356
     357    /*void CommonController::spin() 
    344358    {
    345359        this->moveToTargetPosition();
    346         this->getControllableEntity()->rotateRoll(8.0f);
    347     }
    348     void CommonController::turn180()
    349     {
    350         Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);
    351 
    352         this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x);
    353         this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y);
    354 
    355         this->getControllableEntity()->moveFrontBack(SPEED);
     360        this->getControllableEntity() ->rotateRoll( 8.0f );
     361    }
     362    void CommonController::turn180() 
     363    {
     364        Vector2 coord = get2DViewdirection( this->getControllableEntity() ->getPosition() , this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT, this->getControllableEntity() ->getOrientation()  * WorldEntity::UP, this->targetPosition_ );
     365
     366        this->getControllableEntity() ->rotateYaw( -2.0f * sgn( coord.x )* coord.x*coord.x );
     367        this->getControllableEntity() ->rotatePitch( 2.0f * sgn( coord.y )* coord.y*coord.y );
     368
     369        this->getControllableEntity() ->moveFrontBack( SPEED );
    356370    }*/
    357371
     
    359373
    360374    //copy the Roll orientation of given Quaternion.
    361     void CommonController::copyOrientation(const Quaternion& orient, float dt)
     375    void CommonController::copyOrientation( const Quaternion& orient, float dt )
    362376    {
    363377        //roll angle difference in radian
    364         float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians());
    365         while(diff>math::twoPi) diff-=math::twoPi;
    366         while(diff<-math::twoPi) diff+=math::twoPi;
    367         this->getControllableEntity()->rotateRoll(diff*ROTATEFACTOR * dt);
    368     }
    369     void CommonController::copyTargetOrientation(float dt)
    370     {
    371         if (bHasTargetOrientation_)
     378        float diff=orient.getRoll( false ).valueRadians() -( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() );
     379        while( diff>math::twoPi )diff-=math::twoPi;
     380        while( diff<-math::twoPi )diff+=math::twoPi;
     381        this->getControllableEntity() ->rotateRoll( diff*ROTATEFACTOR * dt );
     382    }
     383    void CommonController::copyTargetOrientation( float dt )
     384    {
     385        if ( bHasTargetOrientation_ )
    372386        {   
    373             copyOrientation(targetOrientation_, dt);
    374         }
    375     }
    376 
    377 
    378 
    379 
    380     void CommonController::moveToTargetPosition(float dt)
    381     {
    382         this->moveToPosition(this->targetPosition_, dt);
    383     }
    384     void CommonController::moveToPosition(const Vector3& target, float dt)
     387            copyOrientation( targetOrientation_, dt );
     388        }
     389    }
     390
     391
     392
     393
     394    void CommonController::moveToTargetPosition( float dt )
     395    {
     396        this->moveToPosition( this->targetPosition_, dt );
     397    }
     398    void CommonController::moveToPosition( const Vector3& target, float dt )
    385399    {
    386400        float factor = 1;
    387         if (!this->getControllableEntity())
     401        if ( !this->getControllableEntity() )
    388402            return;
    389         if (this->rank_ == Rank::DIVISIONLEADER)
     403        if ( this->rank_ == Rank::DIVISIONLEADER )
    390404            factor = 0.8;
    391         if (this->rank_ == Rank::SECTIONLEADER)
     405        if ( this->rank_ == Rank::SECTIONLEADER )
    392406            factor = 0.9;
    393407       
    394         //100 is (so far) the smallest tolerance (empirically found) that can be reached,
     408        //100 is ( so far )the smallest tolerance ( empirically found )that can be reached,
    395409        //with smaller distance spaceships can't reach position and go circles around it instead
    396410        int tolerance = 60;
     
    398412        ControllableEntity* entity = this->getControllableEntity();
    399413        Vector2 coord = get2DViewCoordinates
    400             (entity->getPosition(),
    401             entity->getOrientation() * WorldEntity::FRONT,
    402             entity->getOrientation() * WorldEntity::UP,
    403             target);
    404 
    405         float distance = (target - this->getControllableEntity()->getPosition()).length();
     414            ( entity->getPosition() ,
     415            entity->getOrientation()  * WorldEntity::FRONT,
     416            entity->getOrientation()  * WorldEntity::UP,
     417            target );
     418
     419        float distance = ( target - this->getControllableEntity() ->getPosition() ).length();
    406420
    407421        //rotates should be in range [-1,+1], clamp cuts off all that is not
    408         float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
    409         float rotateY = clamp(coord.y * 10, -1.0f, 1.0f);
     422        float rotateX = clamp( coord.x * 10, -1.0f, 1.0f );
     423        float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
    410424
    411425       
    412         if (distance > tolerance)
     426        if ( distance > tolerance )
    413427        {
    414428            //Yaw and Pitch are enough to start facing the target
    415             this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt);
    416             this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt);
     429            this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt );
     430            this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt );
    417431
    418432            //300 works, maybe less is better
    419             if (distance < 400)
     433            if ( distance < 400 )
    420434            {
    421435                //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory
     
    423437                //Wingmen won't face same direction as Leaders, but when Leaders start moving
    424438                //Yaw and Pitch will adapt.
    425                 if (bHasTargetOrientation_)
    426                 {
    427                     copyTargetOrientation(dt);
    428                 }
    429             }
    430 
    431             this->getControllableEntity()->moveFrontBack(1.2f*SPEED*factor * dt);
     439                if ( bHasTargetOrientation_ )
     440                {
     441                    copyTargetOrientation( dt );
     442                }
     443            }
     444
     445            this->getControllableEntity() ->moveFrontBack( 1.2f*SPEED*factor * dt );
    432446        }
    433447        else
     
    437451        }
    438452    }
    439     float CommonController::randomInRange(float a, float b)
    440     {
    441         float random = rnd(1.0f);
     453    float CommonController::randomInRange( float a, float b )
     454    {
     455        float random = rnd( 1.0f );
    442456        float diff = b - a;
    443457        float r = random * diff;
    444458        return a + r;
    445459    }
    446     void CommonController::attack()
    447     {
    448         if ( !this->getControllableEntity() )
     460    void CommonController::attack() 
     461    {
     462        if ( !this->getControllableEntity()  )
    449463            return;
    450464        if ( this->target_ )
    451465        {
    452466            this->positionOfTarget_ = getPredictedPosition(
    453                 this->getControllableEntity()->getWorldPosition(),
     467                this->getControllableEntity() ->getWorldPosition() ,
    454468                hardcoded_projectile_speed,
    455                 this->target_->getWorldPosition(),
    456                 this->target_->getVelocity()
     469                this->target_->getWorldPosition() ,
     470                this->target_->getVelocity()  
    457471                );
    458             Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
     472            Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();
    459473            float diffLength = diffVector.length();
    460             if (diffLength < 100)
     474            if ( diffLength < 100 )
    461475            {
    462476                Vector3* targetPosition;
    463477                targetPosition = new Vector3 (
    464                     //randomInRange(200, 300),
     478                    //randomInRange( 200, 300 ),
    465479                    0,
    466                     //randomInRange(-300, -200),
     480                    //randomInRange( -300, -200 ),
    467481                    0,
    468                     randomInRange(-300, -400)
     482                    randomInRange( -300, -400 )
    469483                    );
    470                 Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);
    471                 Vector3 target = rotationToTarget * (*targetPosition);
     484                Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT ).getRotationTo( diffVector );
     485                Vector3 target = rotationToTarget * ( *targetPosition );
    472486                moveToPoint(
    473487                    target,
    474                     randomInRange(45, 180)
     488                    randomInRange( 45, 180 )
    475489                    );
    476490                executingMoveToPoint_ = true;
     
    479493            this->bShooting_ = true;
    480494            this->positionOfTarget_ = getPredictedPosition(
    481                 this->getControllableEntity()->getWorldPosition(),
     495                this->getControllableEntity() ->getWorldPosition() ,
    482496                hardcoded_projectile_speed,
    483                 this->target_->getWorldPosition(),
    484                 this->target_->getVelocity()
     497                this->target_->getWorldPosition() ,
     498                this->target_->getVelocity()  
    485499                );
    486500            this->targetPosition_ = positionOfTarget_;
     
    492506        }
    493507    }
    494     void CommonController::scissors()
    495     {
    496         if ( !this->getControllableEntity() )
     508    void CommonController::scissors() 
     509    {
     510        if ( !this->getControllableEntity()  )
    497511            return;
    498512        if ( this->target_ )
    499513        {
    500514            this->positionOfTarget_ = getPredictedPosition(
    501                 this->getControllableEntity()->getWorldPosition(),
     515                this->getControllableEntity() ->getWorldPosition() ,
    502516                hardcoded_projectile_speed,
    503                 this->target_->getWorldPosition(),
    504                 this->target_->getVelocity()
     517                this->target_->getWorldPosition() ,
     518                this->target_->getVelocity()  
    505519                );
    506             Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
     520            Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();
    507521            float diffLength = diffVector.length();
    508             Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT;
    509             float opponentDotProduct = diffVector.dotProduct(opponentForwardVector);           
     522            Vector3 targetForwardVector = this->target_->getOrientation() * WorldEntity::FRONT;
     523            float targetDotProduct = diffVector.dotProduct( targetForwardVector );           
    510524           
    511             int f = (int) rnd(100.0f);
    512             f = (f % 2 == 0 ? 1 : -1);
    513 
    514             if(!this->executingMoveToPoint_)
     525            int f = ( int )rnd( 100.0f );
     526            f = ( f % 2 == 0 ? 1 : -1 );
     527
     528            if( !this->executingMoveToPoint_ )
    515529            {
    516530                Vector3* targetPosition;
     
    518532                {
    519533                    targetPosition = new Vector3 (
    520                         //f * randomInRange(200, 300),
    521                         0,
    522                         //f * randomInRange(-300, -200),
    523                         0,
    524                         //randomInRange(-300, -400)
     534                        //f * randomInRange( 200, 300 ),
     535                        0,
     536                        //f * randomInRange( -300, -200 ),
     537                        0,
     538                        //randomInRange( -300, -400 )
    525539                        0
    526540                        );
     
    528542                else
    529543                {
    530                     if ( opponentDotProduct < 0 )
     544                    if ( targetDotProduct < 0 )
    531545                    {
    532546                        targetPosition = new Vector3 (
    533                         //f * randomInRange(200, 300),
    534                         0,
    535                         //f * randomInRange(-300, -200),
    536                         0,
    537                         //randomInRange(-300, -400)
     547                        //f * randomInRange( 200, 300 ),
     548                        0,
     549                        //f * randomInRange( -300, -200 ),
     550                        0,
     551                        //randomInRange( -300, -400 )
    538552                        -300
    539553                        );
     
    542556                    {
    543557                        targetPosition = new Vector3 (
    544                         //f * randomInRange(200, 300),
    545                         0,
    546                         //f * randomInRange(-300, -200),
    547                         0,
    548                         //randomInRange(-300, -400)
     558                        //f * randomInRange( 200, 300 ),
     559                        0,
     560                        //f * randomInRange( -300, -200 ),
     561                        0,
     562                        //randomInRange( -300, -400 )
    549563                        300
    550564                        );
    551565                    }
    552566                }
    553                 Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);
    554                 Vector3 target = rotationToTarget * (*targetPosition);
     567                Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT ).getRotationTo( diffVector );
     568                Vector3 target = rotationToTarget * ( *targetPosition );
    555569                moveToPoint(
    556570                    target,
    557                     randomInRange(45, 180)
     571                    randomInRange( 45, 180 )
    558572                    );
    559573                executingMoveToPoint_ = true; 
     
    566580        }
    567581    }
    568     void CommonController::gunsD()
    569     {
    570         if ( !this->getControllableEntity() )
     582    void CommonController::gunsD() 
     583    {
     584        if ( !this->getControllableEntity()  )
    571585            return;
    572586        if ( this->target_ )
    573587        {
    574588            this->positionOfTarget_ = getPredictedPosition(
    575                 this->getControllableEntity()->getWorldPosition(),
     589                this->getControllableEntity() ->getWorldPosition() ,
    576590                hardcoded_projectile_speed,
    577                 this->target_->getWorldPosition(),
    578                 this->target_->getVelocity()
     591                this->target_->getWorldPosition() ,
     592                this->target_->getVelocity()  
    579593                );
    580             Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();
     594            Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();
    581595            float diffLength = diffVector.length();
    582             if(!this->executingMoveToPoint_)
     596            if( !this->executingMoveToPoint_ )
    583597            {
    584598                Vector3* targetPosition;
     
    586600                {
    587601                    targetPosition = new Vector3 (
    588                         //f * randomInRange(200, 300),
    589                         0,
    590                         //f * randomInRange(-300, -200),
    591                         0,
    592                         //randomInRange(-300, -400)
     602                        //f * randomInRange( 200, 300 ),
     603                        0,
     604                        //f * randomInRange( -300, -200 ),
     605                        0,
     606                        //randomInRange( -300, -400 )
    593607                        0
    594608                        );
     
    597611                {
    598612                    targetPosition = new Vector3 (
    599                         //randomInRange(100, 200),
    600                         0,
    601                         //randomInRange(-200, -100),
    602                         0,
    603                         //randomInRange(-400, -600)
     613                        //randomInRange( 100, 200 ),
     614                        0,
     615                        //randomInRange( -200, -100 ),
     616                        0,
     617                        //randomInRange( -400, -600 )
    604618                        500
    605619                        );
     
    608622                {
    609623                    targetPosition = new Vector3 (
    610                         //randomInRange(200, 300),
    611                         0,
    612                         //randomInRange(-300, -200),
    613                         0,
    614                         //randomInRange(-400, -600)
     624                        //randomInRange( 200, 300 ),
     625                        0,
     626                        //randomInRange( -300, -200 ),
     627                        0,
     628                        //randomInRange( -400, -600 )
    615629                        500
    616630                        );
    617631                }
    618                 Quaternion rotationToTarget = (this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);
    619                 Vector3 target = rotationToTarget * (*targetPosition);
     632                Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT ).getRotationTo( diffVector );
     633                Vector3 target = rotationToTarget * ( *targetPosition );
    620634                moveToPoint(
    621635                    target,
    622                     randomInRange(45, 180)
     636                    randomInRange( 45, 180 )
    623637                    );
    624638                executingMoveToPoint_ = true; 
     
    633647    //PRE: relativeTargetPosition is desired position relative to the spaceship,
    634648    //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement
    635     //POST: target orientation and position are set, so that it can be used by MoveAndRoll()
    636     void CommonController::moveToPoint(const Vector3& relativeTargetPosition, float angleRoll)
     649    //POST: target orientation and position are set, so that it can be used by MoveAndRoll() 
     650    void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll )
    637651    {
    638652        ControllableEntity* entity = this->getControllableEntity();
    639         if (!entity)
     653        if ( !entity )
    640654            return;
    641655        Quaternion orient = entity->getWorldOrientation();
    642         Quaternion rotation = Quaternion(Degree(angleRoll), Vector3::UNIT_Z);
     656        Quaternion rotation = Quaternion( Degree( angleRoll ), Vector3::UNIT_Z );
    643657
    644658        Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition();
    645         setTargetPosition(target);
     659        setTargetPosition( target );
    646660        orient = orient * rotation;
    647         this->setTargetOrientation(orient);
     661        this->setTargetOrientation( orient );
    648662       
    649663    }
     
    657671    //otherwise returns true
    658672    //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec
    659     bool CommonController::moveAndRoll(float dt)
     673    bool CommonController::moveAndRoll( float dt )
    660674    {
    661675        float factor = 1;
    662         if (!this->getControllableEntity())
     676        if ( !this->getControllableEntity() )
    663677            return false;
    664         if (this->rank_ == Rank::DIVISIONLEADER)
     678        if ( this->rank_ == Rank::DIVISIONLEADER )
    665679            factor = 0.8;
    666         if (this->rank_ == Rank::SECTIONLEADER)
     680        if ( this->rank_ == Rank::SECTIONLEADER )
    667681            factor = 0.9;
    668682        int tolerance = 60;
    669683       
    670684        ControllableEntity* entity = this->getControllableEntity();
    671         if (!entity)
     685        if ( !entity )
    672686            return true;
    673687        Vector2 coord = get2DViewCoordinates
    674             (entity->getPosition(),
    675             entity->getOrientation() * WorldEntity::FRONT,
    676             entity->getOrientation() * WorldEntity::UP,
    677             targetPosition_);
    678 
    679         float distance = (targetPosition_ - this->getControllableEntity()->getPosition()).length();
     688            ( entity->getPosition() ,
     689            entity->getOrientation()  * WorldEntity::FRONT,
     690            entity->getOrientation()  * WorldEntity::UP,
     691            targetPosition_ );
     692
     693        float distance = ( targetPosition_ - this->getControllableEntity() ->getPosition() ).length();
    680694
    681695        //rotates should be in range [-1,+1], clamp cuts off all that is not
    682         float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
    683         float rotateY = clamp(coord.y * 10, -1.0f, 1.0f);
     696        float rotateX = clamp( coord.x * 10, -1.0f, 1.0f );
     697        float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
    684698
    685699       
    686         if (distance > tolerance)
     700        if ( distance > tolerance )
    687701        {
    688702            //Yaw and Pitch are enough to start facing the target
    689             this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt);
    690             this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt);
     703            this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt );
     704            this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt );
    691705           
    692706            //Roll
    693             if (bHasTargetOrientation_)
    694             {
    695                 copyTargetOrientation(dt);
     707            if ( bHasTargetOrientation_ )
     708            {
     709                copyTargetOrientation( dt );
    696710            }
    697711         
    698712            //Move
    699             this->getControllableEntity()->moveFrontBack(1.2f * SPEED * factor * dt);
     713            this->getControllableEntity() ->moveFrontBack( 1.2f * SPEED * factor * dt );
    700714            //if still moving, return false
    701715            return false;
     
    709723    }
    710724
    711     float CommonController::squaredDistanceToTarget() const
    712     {
    713         if ( !this->getControllableEntity() )
     725    float CommonController::squaredDistanceToTarget()  const
     726    {
     727        if ( !this->getControllableEntity()  )
    714728            return 0;
    715729        if ( !this->target_ )
    716             return ( this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) );
     730            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) );
    717731        else
    718             return ( this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) );
     732            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->target_->getPosition() ) );
    719733    }
    720734   
    721     bool CommonController::isLookingAtTarget(float angle) const
    722     {
    723         if (!this->getControllableEntity())
     735    bool CommonController::isLookingAtTarget( float angle )const
     736    {
     737        if ( !this->getControllableEntity() )
    724738            return false;
    725739
    726         return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
    727     }
    728 
    729     bool CommonController::canFire()
     740        return ( getAngle( this->getControllableEntity() ->getPosition() , this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT, this->targetPosition_ ) < angle );
     741    }
     742
     743    bool CommonController::canFire() 
    730744    {
    731745        float squaredDistance = squaredDistanceToTarget();
    732         if ( this->bShooting_ && squaredDistance < 9000000 && squaredDistance > 10000 && this->isLookingAtTarget(math::pi /(0.0002f*squaredDistance)) )
     746        if ( this->bShooting_ && squaredDistance < 9000000 && squaredDistance > 10000 && this->isLookingAtTarget( math::pi /( 0.0002f*squaredDistance )) )
    733747        {
    734748            return true;
     
    740754
    741755    }
    742     void CommonController::doFire()
    743     {
    744         if (!this->target_ || !this->getControllableEntity())
     756    void CommonController::doFire() 
     757    {
     758        if ( !this->target_ || !this->getControllableEntity() )
    745759            return;
    746760        static const float hardcoded_projectile_speed = 750;
    747761
    748         this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity());
    749         this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO);
    750 
    751         Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
    752 
    753         if (pawn)
    754             //pawn->setAimPosition(this->getControllableEntity()->getWorldPosition() + 4000*(this->getControllableEntity()->getOrientation() * WorldEntity::FRONT));
    755             pawn->setAimPosition(this->targetPosition_);
     762        this->targetPosition_ = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() );
     763        this->bHasTargetPosition_ = ( this->targetPosition_ != Vector3::ZERO );
     764
     765        Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
     766
     767        if ( pawn )
     768            //pawn->setAimPosition( this->getControllableEntity() ->getWorldPosition()  + 4000*( this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT ));
     769            pawn->setAimPosition( this->targetPosition_ );
    756770   
    757         this->getControllableEntity()->fire(0);
     771        this->getControllableEntity() ->fire( 0 );
    758772    }
    759773   
Note: See TracChangeset for help on using the changeset viewer.