Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10042


Ignore:
Timestamp:
Apr 29, 2014, 11:16:16 AM (10 years ago)
Author:
muemart
Message:

Fix rotation if parent is already rotated. Everything hopefully now works as expected. It's a giant mess right now, so still have to clean it up.

Location:
code/branches/turretFS14
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/turretFS14/data/levels/turretTest.oxw

    r10039 r10042  
    3434
    3535
    36 <SpaceShip position="-100,0,0" yaw=0 pitch=0>
     36<!-- <SpaceShip position="-100,0,0" yaw=0 pitch=0>
    3737    <attached>
    3838        <Model position="0,0,0" mesh="turretSocketFront.mesh" scale3D="10,10,10" />
     
    5858
    5959    </attached>
    60 </SpaceShip>
     60</SpaceShip> -->
     61<StaticEntity position="100,0,0">
     62  <attached>
     63    <Model mesh="sphere.mesh" position="20,0,0" scale=1/>
    6164
    62 <SpaceShip position="100,0,0" yaw=0 pitch=0>
     65    <Model mesh="sphere.mesh" position="0,25,0" scale=1.5/>
     66
     67    <Model mesh="sphere.mesh" position="0,0,30" scale=2/>
     68  </attached>
     69</StaticEntity>
     70<SpaceShip position="100,0,0" yaw=0 pitch=90>
    6371    <attached>
    6472        <Model position="0,0,0" mesh="turretSocketFront.mesh" scale3D="10,10,10" />
     
    7381            </templates>
    7482            <controller>
    75               <TurretController team=9 />
     83              <TurretController team=10 />
    7684            </controller>
    7785        </Turret>
    7886
    79         <Model mesh="sphere.mesh" position="20,0,0" scale=1/>
    8087
    81         <Model mesh="sphere.mesh" position="0,25,0" scale=1/>
    82 
    83         <Model mesh="sphere.mesh" position="0,0,30" scale=1/>
    8488
    8589    </attached>
  • code/branches/turretFS14/src/modules/objects/Turret.cc

    r10039 r10042  
    113113        this->localAngularAcceleration_ = physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_;
    114114
     115        //physics don't work when attached :(
     116        //this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
    115117
    116         pitch(Degree(localAngularAcceleration_.x()/10000), WorldEntity::World);
    117         yaw(Degree(localAngularAcceleration_.y()/10000), WorldEntity::World);
    118         roll(Degree(localAngularAcceleration_.z()/10000), WorldEntity::World);
     118        pitch(Degree(localAngularAcceleration_.x()*dt/1000), WorldEntity::World);
     119        yaw(Degree(localAngularAcceleration_.y()*dt/1000), WorldEntity::World);
     120        roll(Degree(localAngularAcceleration_.z()*dt/1000), WorldEntity::World);
    119121
    120122        this->localAngularAcceleration_.setValue(0, 0, 0);
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc

    r10039 r10042  
    4747        this->maxYaw_ = 90;
    4848        this->gotOrient_ = false;
    49         orxout() << "Constructor " << this << endl;
    5049        }
    5150
     
    9594        }
    9695
    97         bool TurretController::isInRange(Vector3 position)
    98     {
     96        bool TurretController::isInRange(const Vector3 &position)
     97    {
     98        //Check distance
    9999        Vector3 distance = position - this->getControllableEntity()->getWorldPosition();
    100100        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
     
    103103        }
    104104
     105        //Check pitch
    105106        Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_);
    106107        Vector3 dirProjected = dir;
     
    114115        }
    115116
     117        //Check yaw
    116118        dirProjected = dir;
    117119        dirProjected.y = 0;
     
    126128    }
    127129
     130    void TurretController::aimAtPositionRot(const Vector3 &position)
     131    {
     132
     133        Vector3 currDir = this->getControllableEntity()->getWorldOrientation() * WorldEntity::FRONT;
     134        Vector3 targetDir = position - this->getControllableEntity()->getWorldPosition();
     135
     136        Quaternion rot = currDir.getRotationTo(targetDir);
     137
     138        //Don't make the rotation instantaneous
     139        rot = Quaternion::Slerp(0.1, Quaternion::IDENTITY, rot);
     140
     141        this->getControllableEntity()->rotate(rot, WorldEntity::World);
     142    }
     143   
     144
     145    void TurretController::aimAtTargetRot()
     146    {
     147        this->aimAtPositionRot(this->target_->getWorldPosition());
     148    }
     149
     150    bool TurretController::isLookingAtTargetNew(float angle) const
     151    {
     152        return (getAngle(this->getControllableEntity()->getWorldPosition(), this->getControllableEntity()->getWorldOrientation() * WorldEntity::FRONT, this->target_->getWorldPosition()) < angle);
     153    }
     154
    128155        void TurretController::tick(float dt)
    129156        {
    130 
    131 
    132         if(!gotOrient_)
    133         {
    134             this->startOrient_ = this->getControllableEntity()->getOrientation();
    135             this->localXStart_ = this->startOrient_ * this->localX_;
    136             this->localXStart_.normalise();
    137             this->localX_ = this->localXStart_;
    138             this->localYStart_ = this->startOrient_ * this->localY_;
    139             this->localYStart_.normalise();
    140             this->localY_ = this->localYStart_;
    141             this->localZStart_ = this->startOrient_ * this->localZ_;
    142             this->localZStart_.normalise();
    143             this->localZ_ = this->localZStart_;
    144 
    145             //startDir should always be (0,0,-1)
    146             this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
    147 
    148             this->gotOrient_ = true;
    149         }
    150 
    151         //orxout() << "Controller " << this;
    152         //orxout() << "\tControllable Entity " << this->getControllableEntity() << endl;
    153         WorldEntity* parent = this->getControllableEntity()->getParent();
    154         if(parent)
    155         {
    156             Quaternion parentrot = parent->getOrientation();
    157             this->localX_ = parentrot * this->localXStart_;
    158             this->localY_ = parentrot * this->localYStart_;
    159             this->localZ_ = parentrot * this->localZStart_;
    160         }
    161 
    162         orxout() << this->getControllableEntity()->getWorldPosition() << endl;
    163 
    164 
    165         if (!this->isActive() || !this->getControllableEntity())
    166             return;
    167                 this->searchTarget();
    168                 if(target_)
    169                 {
    170                         this->aimAtTarget();
    171                         //this->getControllableEntity()->lookAt(this->targetPosition_);
    172                         //It says move, but really it only turns
    173                         this->moveToTargetPosition();
    174                         if(this->isLookingAtTarget(Degree(5).valueRadians()))
    175                         {
    176                                 orxout() << 42 << endl;
    177                         }
    178                 }
     157                        if(!gotOrient_)
     158                {
     159                    this->startOrient_ = this->getControllableEntity()->getOrientation();
     160                    this->localXStart_ = this->startOrient_ * this->localX_;
     161                    this->localXStart_.normalise();
     162                    this->localX_ = this->localXStart_;
     163                    this->localYStart_ = this->startOrient_ * this->localY_;
     164                    this->localYStart_.normalise();
     165                    this->localY_ = this->localYStart_;
     166                    this->localZStart_ = this->startOrient_ * this->localZ_;
     167                    this->localZStart_.normalise();
     168                    this->localZ_ = this->localZStart_;
     169
     170                    //startDir should always be (0,0,-1)
     171                    this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
     172
     173                    this->gotOrient_ = true;
     174
     175                }
     176
     177                WorldEntity* parent = this->getControllableEntity()->getParent();
     178                if(parent)
     179                {
     180                    Quaternion parentrot = parent->getOrientation();
     181                    this->localX_ = parentrot * this->localXStart_;
     182                    this->localY_ = parentrot * this->localYStart_;
     183                    this->localZ_ = parentrot * this->localZStart_;
     184                }
     185
     186
     187
     188                if (!this->isActive() || !this->getControllableEntity())
     189                    return;
     190
     191                        this->searchTarget();
     192                        if(target_)
     193                        {
     194                                this->aimAtTarget();
     195                                //this->getControllableEntity()->lookAt(this->targetPosition_);
     196                                //It says move, but really it only turns
     197                                this->aimAtTargetRot();
     198                                if(this->isLookingAtTargetNew(Degree(5).valueRadians()))
     199                                {
     200                                        orxout() << 42 << endl;
     201                                }
     202                        }
    179203        }
    180204 }
  • code/branches/turretFS14/src/modules/objects/controllers/TurretController.h

    r10039 r10042  
    5656            Vector3 localX_;
    5757            Vector3 localXStart_;
     58
     59            void aimAtPositionRot(const Vector3 &position);
     60            void aimAtTargetRot();
    5861                        void searchTarget();
    59                         bool isInRange(Vector3 position);
     62                        bool isInRange(const Vector3 &position);
     63                        bool isLookingAtTargetNew(float angle) const;
    6064        };
    6165 }
Note: See TracChangeset for help on using the changeset viewer.