- Timestamp:
- Dec 7, 2015, 4:06:37 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/campaignHS15/src/orxonox/controllers/FlyingController.cc
r10923 r10946 9 9 * modify it under the terms of the GNU General Public License 10 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or ( at your option)any later version.11 * of the License, or (at your option)any later version. 12 12 * 13 13 * This program is distributed in the hope that it will be useful, … … 27 27 * 28 28 */ 29 #include <OgreMatrix3.h> //for Quaternion manipulations 30 31 #include "util/Math.h" 32 #include "core/XMLPort.h" 29 33 #include "controllers/FlyingController.h" 30 #include "core/XMLPort.h" 31 #include "worldentities/pawns/SpaceShip.h" 32 #include "util/Math.h" 33 #include <OgreMatrix3.h> 34 35 #include "worldentities/pawns/SpaceShip.h" //for boosting 34 36 35 37 namespace orxonox … … 60 62 FormationMode::Value value; 61 63 62 if ( valUpper == "WALL")64 if (valUpper == "WALL") 63 65 value = FormationMode::WALL; 64 else if ( valUpper == "FINGER4")66 else if (valUpper == "FINGER4") 65 67 value = FormationMode::FINGER4; 66 else if ( valUpper == "DIAMOND")68 else if (valUpper == "DIAMOND") 67 69 value = FormationMode::DIAMOND; 68 70 else 69 ThrowException(ParseError, std::string( "Attempting to set an unknown FormationMode: '")+ val + "'.");71 ThrowException(ParseError, std::string("Attempting to set an unknown FormationMode: '")+ val + "'."); 70 72 this->setFormationMode(value); 71 73 } 72 74 std::string FlyingController::getFormationModeXML() const 73 75 { 74 switch ( this->formationMode_)76 switch (this->formationMode_) 75 77 { 76 78 case FormationMode::WALL: … … 88 90 this->bHasTargetPosition_ = false; 89 91 } 92 /** 93 @brief 94 if distance to targetPosition is smaller than this->tolerance_, no moving should be made, otherwise 95 find amount of yaw and pitch that have to be applied, so that ship looks at targetPosition, then 96 ship is moved forward towards targetPosition. Also target orientation is being applied. 97 */ 90 98 void FlyingController::moveToPosition(const Vector3& targetPosition, float dt) 91 99 { … … 94 102 ControllableEntity* entity = this->getControllableEntity(); 95 103 96 float distance = ( targetPosition - entity->getPosition() ).length(); 97 98 if ( distance >= this->tolerance_ ) 99 { 104 float distance = (targetPosition - entity->getPosition()).length(); 105 106 if (distance >= this->tolerance_) 107 { 108 //function that calculates how much yaw and pitch are to be applied 100 109 Vector2 coord = get2DViewCoordinates 101 ( 110 (entity->getPosition() , 102 111 entity->getOrientation() * WorldEntity::FRONT, 103 112 entity->getOrientation() * WorldEntity::UP, 104 targetPosition ); 105 float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); 106 float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); 107 entity->rotateYaw( ROTATEFACTOR * rotateX * dt ); 108 entity->rotatePitch( ROTATEFACTOR * rotateY * dt ); 109 113 targetPosition); 114 //limit yaw and pitch by [-1,1] 115 float rotateX = -clamp(coord.x * 10, -1.0f, 1.0f); 116 float rotateY = clamp(coord.y * 10, -1.0f, 1.0f); 117 118 if (!entity) 119 return; 120 121 //apply yaw and pitch 122 entity->rotateYaw(ROTATEFACTOR * rotateX * dt); 123 entity->rotatePitch(ROTATEFACTOR * rotateY * dt); 124 125 if (!entity) 126 return; 127 128 //only move either if ship looks at target with a certain tolerance, or if ship is far enough for it to be ok to move in a curve. 110 129 if (distance > this->tolerance_*1.5f || (rotateX > -0.03 && rotateX < 0.03 && rotateY > -0.03 && rotateY < 0.03)) 111 entity->moveFrontBack( SPEED * dt ); 112 copyTargetOrientation(dt); 130 entity->moveFrontBack(SPEED * dt); 131 //roll 132 copyTargetOrientation(dt); 113 133 } 114 134 else … … 117 137 } 118 138 } 119 139 /** 140 @brief 141 fly towards a preset targetPosition_ 142 */ 120 143 void FlyingController::moveToTargetPosition(float dt) 121 144 { 122 145 this->moveToPosition (this->targetPosition_, dt); 123 146 } 147 /** 148 @brief 149 roll ship so that it has same roll as orient 150 */ 124 151 void FlyingController::copyOrientation(const Quaternion& orient, float dt) 125 152 { … … 155 182 } 156 183 } 157 184 /** 185 @brief 186 roll ship so that it has same roll as a preset targetOrientation_ 187 */ 158 188 void FlyingController::copyTargetOrientation(float dt) 159 189 { … … 163 193 } 164 194 } 165 195 /** 196 @brief 197 set Vector to fly to 198 */ 166 199 void FlyingController::setTargetPosition(const Vector3& target) 167 200 { … … 169 202 this->bHasTargetPosition_ = true; 170 203 } 171 204 /** 205 @brief 206 set orientation to apply 207 */ 172 208 void FlyingController::setTargetOrientation(const Quaternion& orient) 173 209 { … … 175 211 this->bHasTargetOrientation_=true; 176 212 } 177 213 /** 214 @brief 215 set orientation to apply 216 */ 178 217 void FlyingController::setTargetOrientation(ControllableEntity* target) 179 218 { … … 181 220 this->setTargetOrientation(target->getOrientation()); 182 221 } 222 /** 223 @brief 224 boost if you can 225 */ 183 226 void FlyingController::boostControl() 184 227 { … … 187 230 SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); 188 231 if(ship == NULL) return; 189 if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() 232 if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower()) //upper limit ->boost 190 233 { 191 234 this->getControllableEntity()->boost(true); … … 196 239 } 197 240 } 241 /** 242 @brief 243 keep this ship in a formation with its division 244 */ 198 245 void FlyingController::keepFormation(const ControllableEntity* leaderEntity, Vector3& targetRelativePosition) 199 246 { 247 if (!this->getControllableEntity()) 248 return; 200 249 ControllableEntity* myEntity = this->getControllableEntity(); 201 250 Vector3 myPosition = myEntity->getWorldPosition(); … … 212 261 return; 213 262 } 263 //calculate where in world coordinates this ship should fly 214 264 Vector3 targetAbsolutePosition = 215 265 (leaderPosition + (orient*WorldEntity::FRONT) * (leaderEntity->getVelocity().length()/5) 216 266 + (orient* (targetRelativePosition))); 217 //let ship finish rotating. also don't call copyOrientation to often as it is a slow function.267 //let ship finish rotating. also don't call copyOrientation too often as it is a slow function. Don't know how to do it different 218 268 if (static_cast<int>(rnd(1.0f) * 100) % 3 == 0) 219 269 this->setTargetOrientation (orient); 270 //set a position to fly to 220 271 this->setTargetPosition (targetAbsolutePosition); 272 273 //boost if too far 221 274 if ((targetAbsolutePosition - myPosition).length() > this->tolerance_ * 1.5f) 222 275 { … … 225 278 else 226 279 { 227 this->getControllableEntity()->boost(false); 280 if (!this->getControllableEntity()) 281 return; 282 this->getControllableEntity()->boost(false); 228 283 } 229 284 }
Note: See TracChangeset
for help on using the changeset viewer.