Changeset 10044 for code/branches/turretFS14/src/modules
- Timestamp:
- May 6, 2014, 11:07:07 AM (11 years ago)
- Location:
- code/branches/turretFS14/src/modules/objects
- Files:
-
- 4 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 } -
code/branches/turretFS14/src/modules/objects/Turret.h
r10039 r10044 50 50 virtual void rotateYaw(const Vector2& value); 51 51 virtual void rotateRoll(const Vector2& value); 52 virtual bool isInRange(const Vector3 &position); 53 virtual void aimAtPosition(const Vector3 &position); 52 54 53 55 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 54 56 virtual void tick(float dt); 55 57 58 inline void setAttackRadius(float radius) 59 { this->attackRadius_ = radius; } 60 61 inline void setMaxPitch(float pitch) 62 { this->maxPitch_ = pitch; } 63 64 inline void setMaxYaw(float yaw) 65 { this->maxYaw_ = yaw; } 66 67 inline float getAttackRadius() const 68 { return this->attackRadius_; } 69 70 inline float getMaxPitch() const 71 { return this->maxPitch_; } 72 73 inline float getMaxYaw() const 74 { return this->maxYaw_; } 75 76 protected: 77 Vector3 startDir_; 78 Vector3 localZ_; 79 Vector3 localZStart_; 80 Vector3 localY_; 81 Vector3 localYStart_; 82 Vector3 localX_; 83 Vector3 localXStart_; 56 84 57 85 private: 86 bool once_; 87 88 float attackRadius_; 89 Ogre::Real maxPitch_; 90 Ogre::Real maxYaw_; 58 91 float rotationThrust_; 59 92 60 btVector3 localAngularAcceleration_;93 Quaternion rotation_; 61 94 }; 62 95 } -
code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
r10042 r10044 38 38 { 39 39 RegisterObject(TurretController); 40 this->startOrient_ = Quaternion::IDENTITY; 41 this->startDir_ = Vector3::ZERO; 42 this->localZ_ = Vector3::UNIT_Z; 43 this->localY_ = Vector3::UNIT_Y; 44 this->localX_ = Vector3::UNIT_X; 45 this->attackRadius_ = 200; 46 this->maxPitch_ = 90; 47 this->maxYaw_ = 90; 48 this->gotOrient_ = false; 40 41 this->once_ = false; 42 49 43 } 50 44 … … 57 51 { 58 52 Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity()); 59 if(target_ && t his->isInRange(target_->getWorldPosition()))53 if(target_ && turret->isInRange(target_->getWorldPosition())) 60 54 { 61 55 return; … … 64 58 { 65 59 this->forgetTarget(); 60 turret->setTarget(0); 66 61 } 67 62 … … 71 66 { 72 67 Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget()); 73 if(parenttarget && t his->isInRange(parenttarget->getWorldPosition()))68 if(parenttarget && turret->isInRange(parenttarget->getWorldPosition())) 74 69 { 75 70 this->setTarget(parenttarget); … … 82 77 { 83 78 Pawn* entity = orxonox_cast<Pawn*>(*it); 84 if ( ArtificialController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))79 if (this->FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype())) 85 80 continue; 86 81 87 if(t his->isInRange(entity->getWorldPosition()))82 if(turret->isInRange(entity->getWorldPosition())) 88 83 { 89 84 this->setTarget(entity); … … 94 89 } 95 90 96 bool TurretController::isInRange(const Vector3 &position)97 {98 //Check distance99 Vector3 distance = position - this->getControllableEntity()->getWorldPosition();100 if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))101 {102 return false;103 }104 105 //Check pitch106 Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_);107 Vector3 dirProjected = dir;108 dirProjected.x = 0;109 Vector3 startDirProjected = this->startDir_;110 startDirProjected.x = 0;111 Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees();112 if(angle > this->maxPitch_)113 {114 return false;115 }116 117 //Check yaw118 dirProjected = dir;119 dirProjected.y = 0;120 startDirProjected = this->startDir_;121 startDirProjected.y = 0;122 angle = startDirProjected.angleBetween(dirProjected).valueDegrees();123 if(angle > this->maxYaw_)124 {125 return false;126 }127 return true;128 }129 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 instantaneous139 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 91 bool TurretController::isLookingAtTargetNew(float angle) const 151 92 { … … 155 96 void TurretController::tick(float dt) 156 97 { 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 } 98 if (!this->isActive() || !this->getControllableEntity()) 99 return; 185 100 186 101 102 if(!this->once_) 103 { 104 if(this->getTeam() != -1) 105 { 106 orxout(internal_warning) << "Turret: Team already set, may result in undesired behaviour" << endl; 107 } 108 else 109 { 110 //Make sure the turret is in the same team as the parent 111 ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent()); 112 if(parent) 113 { 114 Controller* parentcontroller = parent->getController(); 115 if(parentcontroller) 116 { 117 this->setTeam(parentcontroller->getTeam()); 118 } 119 else 120 { 121 this->setTeam(parent->getTeam()); 122 } 123 this->getControllableEntity()->setTeam(parent->getTeam()); 124 } 125 } 126 this->once_ = true; 127 } 187 128 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 } 129 this->searchTarget(); 130 if(target_) 131 { 132 Turret* turret = orxonox_cast<Turret*> (this->getControllableEntity()); 133 this->aimAtTarget(); 134 turret->aimAtPosition(target_->getWorldPosition()); 135 if(this->isLookingAtTargetNew(Degree(5).valueRadians())) 136 { 137 this->getControllableEntity()->fire(0); 138 orxout() << 42 << endl; 139 } 140 } 203 141 } 204 142 } -
code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
r10042 r10044 44 44 45 45 private: 46 bool gotOrient_;47 float attackRadius_;48 Ogre::Real maxPitch_;49 Ogre::Real maxYaw_;50 Quaternion startOrient_;51 Vector3 startDir_;52 Vector3 localZ_;53 Vector3 localZStart_;54 Vector3 localY_;55 Vector3 localYStart_;56 Vector3 localX_;57 Vector3 localXStart_;58 46 59 void aimAtPositionRot(const Vector3 &position);60 void aimAtTargetRot();61 47 void searchTarget(); 62 bool isInRange(const Vector3 &position);63 48 bool isLookingAtTargetNew(float angle) const; 49 50 bool once_; 64 51 }; 65 52 }
Note: See TracChangeset
for help on using the changeset viewer.