Changeset 10044 for code/branches/turretFS14/src/modules/objects/Turret.cc
- Timestamp:
- May 6, 2014, 11:07:07 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/turretFS14/src/modules/objects/Turret.cc
r10042 r10044 21 21 * 22 22 * Author: 23 * Marian Runo 23 * Marian Runo, Martin Mueller 24 24 * Co-authors: 25 25 * ... … … 30 30 #include "core/CoreIncludes.h" 31 31 #include "core/XMLPort.h" 32 #include "BulletDynamics/Dynamics/btRigidBody.h"33 32 34 33 namespace orxonox … … 45 44 RegisterObject(Turret); 46 45 this->rotationThrust_ = 50; 47 48 this->localAngularAcceleration_.setValue(0, 0, 0); 46 this->startDir_ = Vector3::ZERO; 47 this->localZ_ = Vector3::UNIT_Z; 48 this->localY_ = Vector3::UNIT_Y; 49 this->localX_ = Vector3::UNIT_X; 50 this->attackRadius_ = 200; 51 this->maxPitch_ = 90; 52 this->maxYaw_ = 90; 53 this->once_ = false; 54 this->rotation_ = Quaternion::IDENTITY; 55 56 this->setRadarVisibility(false); 49 57 } 50 58 … … 57 65 } 58 66 67 68 bool Turret::isInRange(const Vector3 &position) 69 { 70 //Check distance 71 Vector3 distance = position - this->getWorldPosition(); 72 if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_)) 73 { 74 return false; 75 } 76 77 //Check pitch 78 Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_); 79 Vector3 dirProjected = dir; 80 dirProjected.x = 0; 81 Vector3 startDirProjected = this->startDir_; 82 startDirProjected.x = 0; 83 Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); 84 if(angle > this->maxPitch_) 85 { 86 return false; 87 } 88 89 //Check yaw 90 dirProjected = dir; 91 dirProjected.y = 0; 92 startDirProjected = this->startDir_; 93 startDirProjected.y = 0; 94 angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); 95 if(angle > this->maxYaw_) 96 { 97 return false; 98 } 99 return true; 100 } 101 102 void Turret::aimAtPosition(const Vector3& position) 103 { 104 Vector3 currDir = this->getWorldOrientation() * WorldEntity::FRONT; 105 Vector3 targetDir = position - this->getWorldPosition(); 106 107 this->rotation_ = currDir.getRotationTo(targetDir); 108 109 } 59 110 60 111 void Turret::rotatePitch(const Vector2& value) … … 74 125 } 75 126 */ 76 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);77 127 } 78 128 … … 93 143 } 94 144 */ 95 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);96 145 } 97 146 98 147 void Turret::rotateRoll(const Vector2& value) 99 148 { 100 this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f);101 149 } 102 150 … … 104 152 { 105 153 SUPER(Turret, XMLPort, xmlelement, mode); 154 155 XMLPortParamVariable(Turret, "rotationThrust", rotationThrust_, xmlelement, mode); 156 XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode); 157 XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode); 158 XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode); 106 159 } 107 160 … … 110 163 SUPER(Turret, tick, dt); 111 164 112 this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; 113 this->localAngularAcceleration_ = physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_; 114 115 //physics don't work when attached :( 116 //this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); 117 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); 121 122 this->localAngularAcceleration_.setValue(0, 0, 0); 165 166 if(!this->once_) 167 { 168 169 Quaternion startOrient = this->getOrientation(); 170 this->localXStart_ = startOrient * this->localX_; 171 this->localXStart_.normalise(); 172 this->localX_ = this->localXStart_; 173 this->localYStart_ = startOrient * this->localY_; 174 this->localYStart_.normalise(); 175 this->localY_ = this->localYStart_; 176 this->localZStart_ = startOrient * this->localZ_; 177 this->localZStart_.normalise(); 178 this->localZ_ = this->localZStart_; 179 180 //startDir should always be (0,0,-1) 181 this->startDir_ = getTransformedVector(startOrient * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); 182 183 this->once_ = true; 184 185 } 186 187 //Adjust local axes to parent's rotation 188 WorldEntity* parent = this->getParent(); 189 if(parent) 190 { 191 Quaternion parentrot = parent->getWorldOrientation(); 192 this->localX_ = parentrot * this->localXStart_; 193 this->localY_ = parentrot * this->localYStart_; 194 this->localZ_ = parentrot * this->localZStart_; 195 } 196 197 //rotate 198 if(this->rotation_ != Quaternion::IDENTITY) 199 { 200 //Don't make the rotation instantaneous 201 Quaternion drot = Quaternion::Slerp(dt*this->rotationThrust_/20.f, Quaternion::IDENTITY, this->rotation_); 202 this->rotate(drot, WorldEntity::World); 203 this->rotation_ = Quaternion::IDENTITY; 204 } 205 123 206 } 124 207 }
Note: See TracChangeset
for help on using the changeset viewer.