- Timestamp:
- May 6, 2014, 11:07:07 AM (11 years ago)
- Location:
- code/branches/turretFS14/src/modules/objects/controllers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
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.