Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10764


Ignore:
Timestamp:
Nov 4, 2015, 4:07:46 PM (8 years ago)
Author:
gania
Message:

fixed canFire() function

File:
1 edited

Legend:

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

    r10763 r10764  
    259259    bool CommonController::canFire()
    260260    {
     261       
     262        float tolerance = 50.0f;
     263       
    261264        //check pointers
    262         if (!this->getControllableEntity() || !this->target_)
     265        if (!this->getControllableEntity() || !this->target_ || !this->target_->getControllableEntity())
    263266            return false;
    264 
     267       
    265268        //check if this points in the direction of target_
    266269
    267270        Vector3 myPosition = this->getControllableEntity()->getWorldPosition();
    268         Vector3 targetPosition = this->target_->getWorldPosition();
     271        Vector3 targetPosition = this->target_->getControllableEntity()->getWorldPosition();
     272       
    269273        Vector3 differenceVector = targetPosition - myPosition;
    270         float scalarProduct = differenceVector.dotProduct(WorldEntity::FRONT);
    271         Vector3 projection = scalarProduct * WorldEntity::FRONT;
    272         if ((differenceVector - projection).length() > 50)
     274        float differenceLength = differenceVector.length();
     275       
     276        Vector3 myDirection = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT;
     277       
     278        float angle = getAngle (myPosition, myDirection, targetPosition);
     279        float heightLength = sin(angle) * differenceLength;
     280
     281        if (heightLength > tolerance)
    273282            return false;
    274283       
    275284
     285
    276286        //check if there are allies on the way
    277         Vector3 allyPosition, allyDifference, allyProjection;
    278         float allyScalarProduct;
     287        Vector3 allyPosition, allyDifference;
     288        float allyDifferenceLength, allyAngle, allyHeightLength;
     289
    279290        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
    280291        {
     
    284295            {
    285296                allyPosition = it->getControllableEntity()->getWorldPosition();
     297
    286298                allyDifference = allyPosition - myPosition;
    287                 allyScalarProduct = allyDifference.dotProduct(WorldEntity::FRONT);
    288                 allyProjection = allyScalarProduct * WorldEntity::FRONT;
    289                 if (allyScalarProduct < 0 || allyScalarProduct > scalarProduct)
     299                allyDifferenceLength = allyDifference.length();
     300
     301                allyAngle = getAngle (myPosition, myDirection, allyPosition);
     302
     303                allyHeightLength = sin(allyAngle) * allyDifferenceLength;
     304
     305                if (allyAngle > math::pi /2)
    290306                    continue;
    291                 if ((allyDifference - allyProjection).length() < 50)
     307                if (allyHeightLength <= tolerance)
    292308                    return false;
    293309            }
Note: See TracChangeset for help on using the changeset viewer.