Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 10, 2014, 4:06:03 PM (11 years ago)
Author:
muemart
Message:

Limit turret's rotation (another method…), work a bit on the controller, and make a (ugly?) hack to allow attaching dynamic objects

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/turretFS14/src/modules/objects/Turret.cc

    r10021 r10031  
    4444    {
    4545        RegisterObject(Turret);
    46         this->startOrientInv_ = Quaternion::IDENTITY;
     46        this->startOrient_ = Quaternion::IDENTITY;
     47        this->startDir_ = Vector3::ZERO;
     48        this->localZ_ = Vector3::UNIT_Z;
     49        this->localY_ = Vector3::UNIT_Y;
     50        this->localX_ = Vector3::UNIT_X;
    4751        this->maxPitch_ = 0;
    4852        this->maxYaw_ = 0;
     53        this->attackRadius_ = 200;
    4954        this->gotOrient_ = false;
    5055        this->rotationThrust_ = 50;
     
    6368
    6469    void Turret::rotatePitch(const Vector2& value)
    65     {
    66         if (this->maxPitch_ == 0)
    67         {
    68             return;
    69         }
    70         if (this->maxPitch_ >= 180) //no need to check, if the limit too big
    71         {
    72             this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
    73             return;
    74         }
    75 
    76         Quaternion drot = startOrientInv_ * this->getOrientation();
    77 
    78         Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180);
    79         Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
    80         Ogre::Real lowerBound = offset - this->maxPitch_;
    81         Ogre::Real upperBound = offset + this->maxPitch_;
    82         if (lowerBound < -180) //Avoid wrapping around of the boundaries
    83         {
    84             lowerBound += this->maxPitch_;
    85             upperBound += this->maxPitch_;
    86             val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
    87         }
    88         else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
    89         {
    90             lowerBound -= this->maxPitch_;
    91             upperBound -= this->maxPitch_;
    92             val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
    93         }
    94         if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0))
     70    {   
     71        //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?)
     72        /*
     73        Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
     74        Vector3 currentDirProjected = currentDir;
     75        currentDirProjected.x = 0;
     76        Vector3 startDirProjected = this->startDir_;
     77        startDirProjected.x = 0;     
     78        Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees();
     79        //orxout() << "Pitch: " << angle << endl;   
     80        //if(angle < this->maxPitch_ || (currentDirProjected.y <= 0 && value.x > 0) || (currentDirProjected.y > 0 && value.x < 0) )
    9581        {
    9682            this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
    9783        }
    98         return;
     84        */
     85        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
    9986    }
    10087
    10188    void Turret::rotateYaw(const Vector2& value)
    10289    {
    103         if (this->maxPitch_ == 0)
    104         {
    105             return;
    106         }
    107         if (this->maxPitch_ >= 180) //no need to check, if the limit too big
     90        //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?)
     91        /*
     92        Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
     93        Vector3 currentDirProjected = currentDir;
     94        currentDirProjected.y = 0;
     95        Vector3 startDirProjected = this->startDir_;
     96        startDirProjected.y = 0;
     97        Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees();
     98        orxout() << "Yaw: " << angle << endl;
     99        if(angle < this->maxYaw_ || (currentDirProjected.x <= 0 && value.x < 0) || (currentDirProjected.x > 0 && value.x > 0))
    108100        {
    109101            this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
    110             return;
    111102        }
    112 
    113         Quaternion drot = startOrientInv_ * this->getOrientation();
    114 
    115         Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180);
    116         Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
    117         Ogre::Real lowerBound = offset - this->maxPitch_;
    118         Ogre::Real upperBound = offset + this->maxPitch_;
    119         if (lowerBound < -180) //Avoid wrapping around of the boundaries
    120         {
    121             lowerBound += this->maxPitch_;
    122             upperBound += this->maxPitch_;
    123             val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
    124         }
    125         else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
    126         {
    127             lowerBound -= this->maxPitch_;
    128             upperBound -= this->maxPitch_;
    129             val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
    130         }
    131         if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0))
    132         {
    133            this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
    134         }
    135         return;
     103        */
     104        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
    136105    }
    137106
    138107    void Turret::rotateRoll(const Vector2& value)
    139108    {
    140         return; //Standard turrets don't roll
     109        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f);
     110    }
     111
     112    bool Turret::isInRange(Vector3 position)
     113    {
     114        Vector3 distance = position - this->getPosition();
     115        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
     116        {
     117            return false;
     118        }
     119
     120        Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_);
     121        Vector3 dirProjected = dir;
     122        dirProjected.x = 0;
     123        Vector3 startDirProjected = this->startDir_;
     124        startDirProjected.x = 0;
     125        Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
     126        if(angle > this->maxPitch_)
     127        {
     128            return false;
     129        }
     130
     131        dirProjected = dir;
     132        dirProjected.y = 0;
     133        startDirProjected = this->startDir_;
     134        startDirProjected.y = 0;
     135        angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
     136        if(angle > this->maxYaw_)
     137        {
     138            return false;
     139        }
     140        return true;
    141141    }
    142142
     
    145145        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
    146146        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
     147        XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode);
    147148        SUPER(Turret, XMLPort, xmlelement, mode);
    148149    }
     
    154155        if(!gotOrient_)
    155156        {
    156             startOrientInv_ = this->getOrientation().Inverse();
    157             gotOrient_ = true;
     157            this->startOrient_ = this->getOrientation();
     158            this->localX_ = this->startOrient_ * this->localX_;
     159            this->localX_.normalise();
     160            this->localY_ = this->startOrient_ * this->localY_;
     161            this->localY_.normalise();
     162            this->localZ_ = this->startOrient_ * this->localZ_;
     163            this->localZ_.normalise();
     164
     165            //startDir should always be (0,0,-1)
     166            this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
     167
     168            this->gotOrient_ = true;
    158169        }
    159         Quaternion drot = startOrientInv_ * this->getOrientation();
    160         orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl;
    161        
     170
    162171        this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
    163172        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
     
    165174    }
    166175
    167 
    168     Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)
    169     {
    170         if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }
    171         val -= lowerBound; //adjust to 0
    172         Ogre::Real rangeSize = upperBound - lowerBound;
    173         if (rangeSize == 0){ return upperBound; } //avoid dividing by 0
    174         return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;
    175     }
    176 
    177176}
Note: See TracChangeset for help on using the changeset viewer.