Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 3, 2014, 2:57:05 PM (10 years ago)
Author:
muemart
Message:

Turret: Limit turret's rotation (not really precise though) & implement xml stuff

Location:
code/branches/turretFS14/src/modules/objects
Files:
4 edited

Legend:

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

    r10008 r10018  
    4545    {
    4646        RegisterObject(Turret);
    47         this->controller_ = new WaypointPatrolController(this->getContext());
    48         gotOrient_ = false;
     47        this->startOrientInv_ = Quaternion::IDENTITY;
     48        this->maxPitch_ = 0;
     49        this->maxYaw_ = 0;
     50        this->gotOrient_ = false;
    4951    }
    5052
     
    6062    void Turret::rotatePitch(const Vector2& value)
    6163    {
    62         Radian currentPitch = this->getOrientation().getPitch();
    63         Radian startPitch = startOrient_.getPitch();
    64         Radian limit = Radian((Degree)45);
    65         Radian upperBoundary = startPitch+limit;
    66         if(upperBoundary > Radian(Degree(360)))
     64        if (this->maxPitch_ == 0)
    6765        {
    68             upperBoundary -= Radian(Degree(360));
     66            return;
    6967        }
    70         Radian lowerBoundary = startPitch-limit;
    71         if(lowerBoundary < Radian(Degree(0)))
     68        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
    7269        {
    73             lowerBoundary += Radian(Degree(360));
     70           SpaceShip::rotatePitch(value);
     71           return;
    7472        }
    75         //orxout() << "Pitch:\t" << currentPitch << "\t" << startPitch << endl;
    76        
    77         if(currentPitch > upperBoundary && value.x > 0 ||
    78            currentPitch < lowerBoundary && value.x < 0)
     73
     74        Quaternion drot = startOrientInv_ * this->getOrientation();
     75
     76        Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180);
     77        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
     78        Ogre::Real lowerBound = offset - this->maxPitch_;
     79        Ogre::Real upperBound = offset + this->maxPitch_;
     80        if (lowerBound < -180) //Avoid wrapping around of the boundaries
    7981        {
    80             //return;
     82            lowerBound += this->maxPitch_;
     83            upperBound += this->maxPitch_;
     84            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
    8185        }
    82        
    83         SpaceShip::rotatePitch(value);
     86        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
     87        {
     88            lowerBound -= this->maxPitch_;
     89            upperBound -= this->maxPitch_;
     90            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
     91        }
     92        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0))
     93        {
     94            SpaceShip::rotatePitch(value);
     95        }
     96        return;
    8497    }
    8598
    8699    void Turret::rotateYaw(const Vector2& value)
    87100    {
     101        if (this->maxPitch_ == 0)
     102        {
     103            return;
     104        }
     105        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
     106        {
     107           SpaceShip::rotateYaw(value);
     108           return;
     109        }
    88110
    89         Radian currentYaw = this->getOrientation().getYaw();
    90         Radian startYaw = startOrient_.getYaw();
    91         Radian limit = Radian(Degree(45));
    92         Radian upperBoundary = startYaw + limit;
    93         Radian lowerBoundary = startYaw - limit;
    94         /*if(upperBoundary >= Radian(Degree(180)));
     111        Quaternion drot = startOrientInv_ * this->getOrientation();
     112
     113        Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180);
     114        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
     115        Ogre::Real lowerBound = offset - this->maxPitch_;
     116        Ogre::Real upperBound = offset + this->maxPitch_;
     117        if (lowerBound < -180) //Avoid wrapping around of the boundaries
    95118        {
    96             upperBoundary -= Radian(Degree(180));
    97             lowerBoundary -= Radian(Degree(180));
    98             currentYaw -= Radian(Degree(180));
     119            lowerBound += this->maxPitch_;
     120            upperBound += this->maxPitch_;
     121            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
    99122        }
    100         if(lowerBoundary <= Radian(Degree(-180)))
     123        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
    101124        {
    102             lowerBoundary += Radian(Degree(180));
    103             upperBoundary += Radian(Degree(180));
    104             currentYaw += Radian(Degree(180));
    105         }*/
    106         //orxout() << "Yaw:\t" << (Degree)currentYaw << "\t" << (Degree)upperBoundary << "\t" << (Degree)lowerBoundary << endl;
    107         //if((currentYaw > upperBoundary && value.x > 0) ||
    108         //   (currentYaw < lowerBoundary && value.x < 0))
    109         if((currentYaw < Radian(1) && value.x < 0) || (currentYaw > Radian(3) && value.x>0))
     125            lowerBound -= this->maxPitch_;
     126            upperBound -= this->maxPitch_;
     127            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
     128        }
     129        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0))
    110130        {
    111             //return;
     131            SpaceShip::rotateYaw(value);
    112132        }
    113         SpaceShip::rotateYaw(value);
     133        return;
    114134    }
    115135
    116136    void Turret::rotateRoll(const Vector2& value)
    117137    {
    118         SpaceShip::rotateRoll(value);
    119     }
    120 
    121     void Turret::setAlertnessRadius(float value)
    122     {
    123         this->controller_->setAlertnessRadius(value);
    124     }
    125     float Turret::getAlertnessRadius()
    126     {
    127         return this->controller_->getAlertnessRadius();
     138        return; //Standard turrets don't roll
    128139    }
    129140
    130141    void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    131142    {
     143        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
     144        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
    132145        SUPER(Turret, XMLPort, xmlelement, mode);
    133         XMLPortParam(Turret, "alertnessRadius", setAlertnessRadius, getAlertnessRadius, xmlelement, mode).defaultValues("400");
    134146    }
    135147
    136148    void Turret::tick(float dt)
    137149    {
    138         orxout() << "Pitch: " << this->getOrientation().getPitch() << "\tYaw: " << this->getOrientation().getYaw() << "\tRoll: " << this->getOrientation().getRoll() << endl;
    139150        if(!gotOrient_)
    140151        {
    141             startOrient_ = this->getOrientation();
     152            startOrientInv_ = this->getOrientation().Inverse();
    142153            gotOrient_ = true;
    143154        }
     155        Quaternion drot = startOrientInv_ * this->getOrientation();
     156        orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl;
    144157        SUPER(Turret, tick, dt);
    145158    }
    146159
     160
     161    Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)
     162    {
     163        if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }
     164        val -= lowerBound; //adjust to 0
     165        Ogre::Real rangeSize = upperBound - lowerBound;
     166        if (rangeSize == 0){ return upperBound; } //avoid dividing by 0
     167        return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;
     168    }
     169
    147170}
  • code/branches/turretFS14/src/modules/objects/Turret.h

    r10008 r10018  
    4040
    4141#include "worldentities/pawns/SpaceShip.h"
     42#include "controllers/TurretController.h"
    4243
    4344namespace orxonox
     
    5354            virtual void rotateRoll(const Vector2& value);
    5455
    55             void setAlertnessRadius(float value);
    56             float getAlertnessRadius();
    57 
    5856            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    5957            virtual void tick(float dt);
    6058
     59            inline void setMaxPitch(Ogre::Real pitch)
     60                {this->maxPitch_ = pitch;}
    6161
    62         protected:
    63             WaypointPatrolController* controller_;
     62            inline Ogre::Real getMaxPitch()
     63                {return this->maxPitch_;}
     64
     65            inline void setMaxYaw(Ogre::Real yaw)
     66                {this->maxYaw_ = yaw;}
     67
     68            inline Ogre::Real getMaxYaw()
     69                {return this->maxYaw_;}
     70
    6471        private:
    6572            bool gotOrient_;
    66             Quaternion startOrient_;
     73            Ogre::Real maxPitch_;
     74            Ogre::Real maxYaw_;
     75            Quaternion startOrientInv_;
     76
     77            Ogre::Real boundBetween(float val, float lowerBound, float upperBound);
    6778    };
    6879}
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc

    r10008 r10018  
    3636        {
    3737                RegisterObject(TurretController);
     38                counter = 0;
     39                flag = false;
    3840        }
    3941
     
    4547        void TurretController::tick(float dt)
    4648        {
    47                 this->getControllableEntity()->rotateYaw(10*dt);
     49                counter += dt;
     50                if(counter >= 10)
     51                {
     52                        counter = 0;
     53                        flag = !flag;
     54                        orxout() << "Direction change" << endl;
     55                }
     56                if(flag)
     57                {
     58                        this->getControllableEntity()->rotatePitch(10*dt);
     59                        //this->getControllableEntity()->rotateYaw(10*dt);
     60                }
     61                else
     62                {
     63                        this->getControllableEntity()->rotatePitch(-10*dt);
     64                        //this->getControllableEntity()->rotateYaw(-10*dt);
     65                }
    4866        }
    4967 }
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.h

    r10008 r10018  
    4242
    4343                        virtual void tick(float dt);
     44
     45                private:
     46                        float counter;
     47                        bool flag;
    4448        };
    4549 }
Note: See TracChangeset for help on using the changeset viewer.