Changeset 8727 for code/trunk/src/orxonox/items/Engine.cc
- Timestamp:
- Jul 3, 2011, 5:42:19 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/items/Engine.cc
r8706 r8727 29 29 #include "Engine.h" 30 30 31 #include "util/Math.h"32 31 #include "core/CoreIncludes.h" 33 32 #include "core/ConfigValueIncludes.h" 33 #include "core/Template.h" 34 34 #include "core/XMLPort.h" 35 #include "util/Math.h" 36 35 37 #include "Scene.h" 36 38 #include "worldentities/pawns/SpaceShip.h" 37 #include "core/Template.h"38 39 39 40 namespace orxonox … … 41 42 CreateFactory(Engine); 42 43 44 /** 45 @brief 46 Constructor. Registers and initializes the object. 47 */ 43 48 Engine::Engine(BaseObject* creator) : Item(creator) 44 49 { … … 47 52 this->ship_ = 0; 48 53 this->shipID_ = OBJECTID_UNKNOWN; 49 this->relativePosition_ = Vector3(0,0,0); 50 51 this->boostFactor_ = 1.5; 52 this->speedFactor_ = 1.0; 53 54 this->maxSpeedFront_ = 0.0; 55 this->maxSpeedBack_ = 0.0; 56 this->maxSpeedLeftRight_ = 0.0; 57 this->maxSpeedUpDown_ = 0.0; 58 59 this->accelerationFront_ = 0.0; 60 this->accelerationBrake_ = 0.0; 61 this->accelerationBack_ = 0.0; 62 this->accelerationLeftRight_ = 0.0; 63 this->accelerationUpDown_ = 0.0; 64 65 this->speedAdd_ = 0.0; 66 this->speedMultiply_ = 1.0; 54 this->relativePosition_ = Vector3::ZERO; 55 56 this->boostFactor_ = 1.5f; 57 58 this->maxSpeedFront_ = 0.0f; 59 this->maxSpeedBack_ = 0.0f; 60 this->maxSpeedLeftRight_ = 0.0f; 61 this->maxSpeedUpDown_ = 0.0f; 62 63 this->accelerationFront_ = 0.0f; 64 this->accelerationBrake_ = 0.0f; 65 this->accelerationBack_ = 0.0f; 66 this->accelerationLeftRight_ = 0.0f; 67 this->accelerationUpDown_ = 0.0f; 68 69 this->speedAdd_ = 0.0f; 70 this->speedMultiply_ = 1.0f; 67 71 68 72 this->setConfigValues(); … … 70 74 } 71 75 76 /** 77 @brief 78 Destructor. Destroys the object and removes it from the SpaceShip. 79 */ 72 80 Engine::~Engine() 73 81 { 74 82 if (this->isInitialized()) 75 83 { 84 // Remove the engine from the ShapeShip. 76 85 if (this->ship_ && this->ship_->hasEngine(this)) 77 86 this->ship_->removeEngine(this); … … 108 117 registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID)); 109 118 110 registerVariable(this->speedFactor_, VariableDirection::ToClient);111 119 registerVariable(this->boostFactor_, VariableDirection::ToClient); 112 120 … … 138 146 } 139 147 140 void Engine::tick(float dt) 141 { 142 if (!this->ship_) 143 { 144 if (this->shipID_) 148 /** 149 @brief 150 Run the engine for a given time interval. 151 Is called each tick by SpaceShip. 152 @param dt 153 The time since last tick. 154 */ 155 void Engine::run(float dt) 156 { 157 if (this->ship_ == NULL) 158 { 159 if (this->shipID_ != 0) 145 160 { 146 161 this->networkcallback_shipID(); 147 162 148 if ( !this->ship_)163 if (this->ship_ == NULL) 149 164 return; 150 165 } … … 156 171 return; 157 172 158 SUPER(Engine, tick, dt); 159 160 Vector3 direction = this->getDirection(); 161 float directionLength = direction.length(); 162 if (directionLength > 1.0f) 163 direction /= directionLength; // normalize 164 173 // Get the desired steering direction and amount, clipped to length 1 at maximum. 174 Vector3 steering = (this->getSteering().length() > 1.0f ? this->getSteering().normalisedCopy() : this->getSteering()); 175 176 // Get the ships velocity. 165 177 Vector3 velocity = this->ship_->getLocalVelocity(); 166 178 Vector3 acceleration = Vector3::ZERO; 167 179 168 float factor = 1.0f / this->speedFactor_; 169 velocity *= factor; 170 171 if (direction.z < 0) 180 // If there is forward steering action. 181 if (steering.z < 0) 172 182 { 173 183 if (this->maxSpeedFront_ != 0) 174 184 { 175 float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f); 176 acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); 185 float boostfactor = (this->ship_->isBoosting() ? this->boostFactor_ : 1.0f); // Boost factor is 1.0 if not boosting. 186 // Boosting can lead to velocities larger the maximal forward velocity. 187 acceleration.z = steering.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f); 177 188 } 178 189 } 179 else if (direction.z > 0) 180 { 190 // If there is backward steering action. 191 else if (steering.z > 0) 192 { 193 // Either breaking 181 194 if (velocity.z < 0) 182 acceleration.z = direction.z * this->accelerationBrake_; 195 acceleration.z = steering.z * this->accelerationBrake_; 196 // or backward flight. 183 197 else if (this->maxSpeedBack_ != 0) 184 acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);185 } 186 198 acceleration.z = steering.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f); 199 } 200 // If there is left-right steering action. 187 201 if (this->maxSpeedLeftRight_ != 0) 188 202 { 189 if ( direction.x < 0)190 acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);191 else if ( direction.x > 0)192 acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);193 } 194 203 if (steering.x < 0) 204 acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); 205 else if (steering.x > 0) 206 acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f); 207 } 208 // If there is up-down steering action. 195 209 if (this->maxSpeedUpDown_ != 0) 196 210 { 197 if ( direction.y < 0)198 acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);199 else if ( direction.y > 0)200 acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);211 if (steering.y < 0) 212 acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); 213 else if (steering.y > 0) 214 acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f); 201 215 } 202 216 203 217 // NOTE: Bullet always uses global coordinates. 204 218 this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_); 205 206 // Hack to reset a temporary variable "direction" 207 this->ship_->oneEngineTickDone(); 208 if(!this->ship_->hasEngineTicksRemaining()) 209 { 210 this->ship_->setSteeringDirection(Vector3::ZERO); 211 this->ship_->resetEngineTicks(); 212 } 213 } 214 215 void Engine::changedActivity() 216 { 217 SUPER(Engine, changedActivity); 218 } 219 219 } 220 221 /** 222 @brief 223 Adds the Engine to the input SpaceShip. 224 @param ship 225 A pointer to the SpaceShip to which the engine is added. 226 */ 220 227 void Engine::addToSpaceShip(SpaceShip* ship) 221 228 { … … 230 237 } 231 238 232 const Vector3& Engine::getDirection() const 239 /** 240 @brief 241 Get the direction and magnitude of the steering imposed upon the Engine. 242 @return 243 Returns the direction and magnitude of the steering imposed upon the Engine. 244 Is the zero vector if the Engine doesn't belong to a ship. 245 */ 246 const Vector3& Engine::getSteering() const 233 247 { 234 248 if (this->ship_) … … 238 252 } 239 253 240 PickupCarrier* Engine::getCarrierParent(void) const 241 { 242 return this->ship_; 243 } 244 245 const Vector3& Engine::getCarrierPosition(void) const 246 { 247 return this->ship_->getWorldPosition(); 248 } 249 254 /** 255 @brief 256 Load the engine template. 257 Causes all parameters specified by the template to be applied to the Engine. 258 */ 250 259 void Engine::loadEngineTemplate() 251 260 { … … 260 269 } 261 270 } 271 262 272 }
Note: See TracChangeset
for help on using the changeset viewer.