Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 10, 2014, 4:06:03 PM (10 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

Location:
code/branches/turretFS14
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/turretFS14/data/levels/templates/spaceshipAssff.oxt

    r9939 r10031  
    4848      <BlinkingBillboard position="17,-1.5,0" material="Examples/Flare" colour="1.0, 0.5, 0.3" amplitude=0.1 frequency=0.5 quadratic=1 />
    4949      <BlinkingBillboard position="-17,-1.5,0" material="Examples/Flare" colour="0.5, 1.0, 0.3" amplitude=0.1 frequency=0.5 phase=180 quadratic=1 />
     50
     51              <Turret position="0,10,0" pitch="90" yaw="0" roll="0" maxPitch=90 maxYaw=90 attackRadius=2000>
     52            <templates>
     53              <Template link=spaceshipturrettest />
     54            </templates>
     55            <controller>
     56              <TurretController team=10 />
     57            </controller>
     58        </Turret>
     59
    5060    </attached>
    5161    <collisionShapes>
  • code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt

    r10021 r10031  
    22  <Turret position="0,0,0" collisionType="dynamic" angularDamping=0.999999 mass=100>
    33    <attached>
    4         <Model position="0,0,0" pitch="90" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
     4        <Model position="0,0,0" pitch="-90" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
    55    </attached>
    66    <collisionShapes>
  • code/branches/turretFS14/data/levels/turretTest.oxw

    r10018 r10031  
    3434
    3535
    36 <StaticEntity position="0,0,0" yaw=0 pitch=0>
     36<MovableEntity position="0,0,0" yaw=0 pitch=0>
    3737    <attached>
    3838        <Model position="0,0,0" mesh="turretSocketFront.mesh" scale3D="10,10,10" />
     
    4242        <Model position="0,0,0" mesh="turretSocketRight.mesh" scale3D="10,10,10" />
    4343
    44         <Turret position="0,10,0" pitch="-90" yaw="0" roll="0" maxPitch=30 maxYaw=30>
     44        <!-- <Turret position="0,10,0" pitch="90" yaw="0" roll="0" maxPitch=90 maxYaw=90 attackRadius=2000>
    4545            <templates>
    4646              <Template link=spaceshipturrettest />
     
    4949              <TurretController team=10 />
    5050            </controller>
    51         </Turret>
     51        </Turret> -->
    5252
    53         <Model mesh="sphere.mesh" position="25,0,0" scale=1 />
     53        <Model mesh="sphere.mesh" position="20,0,0" scale=1/>
    5454
    55         <Model mesh="sphere.mesh" position="0,25,0" scale=1 />
     55        <Model mesh="sphere.mesh" position="0,25,0" scale=1/>
    5656
    57         <Model mesh="sphere.mesh" position="0,0,25" scale=1 />
     57        <Model mesh="sphere.mesh" position="0,0,30" scale=1/>
    5858
    5959    </attached>
    60 </StaticEntity>
     60</MovableEntity>
    6161
    6262
  • 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}
  • code/branches/turretFS14/src/modules/objects/Turret.h

    r10021 r10031  
    3737
    3838#include "objects/ObjectsPrereqs.h"
    39 #include "OgreQuaternion.h"
    40 
    41 #include "worldentities/pawns/SpaceShip.h"
     39#include "worldentities/pawns/Pawn.h"
    4240
    4341namespace orxonox
     
    5654            virtual void tick(float dt);
    5755
     56            virtual bool isInRange(Vector3 position);
     57
    5858            inline void setMaxPitch(Ogre::Real pitch)
    5959                {this->maxPitch_ = pitch;}
     
    6868                {return this->maxYaw_;}
    6969
     70            inline void setAttackRadius(float radius)
     71                {this->attackRadius_ = radius;}
     72
     73            inline float getAttackRadius()
     74                {return this->attackRadius_;}
     75
     76
    7077        private:
    7178            bool gotOrient_;
    7279            Ogre::Real maxPitch_;
    7380            Ogre::Real maxYaw_;
    74             Quaternion startOrientInv_;
     81            float attackRadius_;
     82            Quaternion startOrient_;
     83            Vector3 startDir_;
     84            Vector3 localZ_;
     85            Vector3 localY_;
     86            Vector3 localX_;
    7587            float rotationThrust_;
    7688
    7789            btVector3 localAngularAcceleration_;
    78 
    79             Ogre::Real boundBetween(float val, float lowerBound, float upperBound);
    8090    };
    8191}
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc

    r10018 r10031  
    2727 */
    2828
    29  #include "TurretController.h"
     29#include "TurretController.h"
     30#include "worldentities/pawns/Pawn.h"
     31#include "Turret.h"
    3032
    3133 namespace orxonox
     
    3638        {
    3739                RegisterObject(TurretController);
    38                 counter = 0;
    39                 flag = false;
    4040        }
    4141
     
    4545        }
    4646
     47        void TurretController::searchTarget()
     48        {
     49        Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity());
     50        if(target_ && turret->isInRange(target_->getPosition()))
     51        {
     52                return;
     53        }
     54        else
     55        {
     56                this->forgetTarget();
     57        }
     58
     59
     60        ControllableEntity* parent = orxonox_cast<ControllableEntity*>(turret->getParent());
     61        if(parent)
     62        {
     63                Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget());
     64                if(parenttarget && turret->isInRange(parenttarget->getPosition()))
     65                {
     66                        this->setTarget(parenttarget);
     67                        turret->setTarget(parenttarget);
     68                        return;
     69                }
     70        }
     71
     72                for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
     73        {
     74                Pawn* entity = orxonox_cast<Pawn*>(*it);
     75            if (ArtificialController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
     76                continue;
     77
     78            if(turret->isInRange(entity->getPosition()))
     79            {
     80                this->setTarget(entity);
     81                turret->setTarget(entity);
     82                break;
     83            }
     84        }               
     85        }
     86
    4787        void TurretController::tick(float dt)
    4888        {
    49                 counter += dt;
    50                 if(counter >= 10)
     89        if (!this->isActive() || !this->getControllableEntity())
     90            return;
     91                this->searchTarget();
     92                this->getControllableEntity()->rotatePitch(0.2);
     93                /*if(target_)
    5194                {
    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                 }
     95                        this->aimAtTarget();
     96                        //It says move, but really it only turns
     97                        this->moveToTargetPosition();
     98                        if(this->isLookingAtTarget(Degree(5).valueRadians()))
     99                        {
     100                                orxout() << 42 << endl;
     101                        }
     102                }*/
    66103        }
    67104 }
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.h

    r10018 r10031  
    4444
    4545                private:
    46                         float counter;
    47                         bool flag;
     46                        void searchTarget();
    4847        };
    4948 }
  • code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc

    r9667 r10031  
    414414            else if (this->isDynamic())
    415415            {
    416                 orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
    417                 return false;
     416                //orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
     417                //return false;
    418418            }
    419419            else if (this->isKinematic() && newParent->isDynamic())
Note: See TracChangeset for help on using the changeset viewer.