Changeset 8706 for code/trunk/src/orxonox/worldentities/pawns/SpaceShip.cc
- Timestamp:
- Jun 14, 2011, 8:53:28 PM (13 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/worldentities/pawns/SpaceShip.cc
r7860 r8706 35 35 #include "core/Template.h" 36 36 #include "core/XMLPort.h" 37 #include "tools/Shader.h" 38 #include "util/Debug.h" // TODO: Needed? 39 #include "util/Math.h" 40 41 #include "graphics/Camera.h" 37 42 #include "items/Engine.h" 43 44 #include "CameraManager.h" 45 #include "Scene.h" 38 46 39 47 namespace orxonox … … 42 50 CreateFactory(SpaceShip); 43 51 44 SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator) 52 SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator), boostBlur_(NULL) 45 53 { 46 54 RegisterObject(SpaceShip); … … 53 61 this->localAngularAcceleration_.setValue(0, 0, 0); 54 62 this->bBoost_ = false; 55 this->bPermanentBoost_ = false;56 63 this->steering_ = Vector3::ZERO; 57 this->engine_ = 0;58 64 59 65 this->boostPower_ = 10.0f; … … 64 70 this->bBoostCooldown_ = false; 65 71 72 this->lift_ = 1.0f; // factor of the lift, standard is 1 73 this->stallSpeed_ = 220.0f; // max speed where lift is added 74 66 75 this->bInvertYAxis_ = false; 67 76 … … 74 83 this->enableCollisionCallback(); 75 84 85 this->engineTicksNotDone = 0; 76 86 this->setConfigValues(); 77 87 this->registerVariables(); 88 89 this->cameraOriginalPosition_ = Vector3::UNIT_SCALE; 90 this->cameraOriginalOrientation_ = Quaternion::IDENTITY; 91 92 this->shakeFrequency_ = 15; 93 this->shakeAmplitude_ = 5; 94 this->shakeDt_ = 0; 78 95 } 79 96 80 97 SpaceShip::~SpaceShip() 81 98 { 82 if (this->isInitialized() && this->engine_) 83 this->engine_->destroy(); 99 if (this->isInitialized()) 100 { 101 this->removeAllEngines(); 102 103 if (this->boostBlur_) 104 this->boostBlur_->destroy(); 105 } 84 106 } 85 107 … … 88 110 SUPER(SpaceShip, XMLPort, xmlelement, mode); 89 111 90 XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode);112 //XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); 91 113 XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); 92 114 XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); … … 96 118 XMLPortParamVariable(SpaceShip, "boostRate", boostRate_, xmlelement, mode); 97 119 XMLPortParamVariable(SpaceShip, "boostCooldownDuration", boostCooldownDuration_, xmlelement, mode); 120 XMLPortParamVariable(SpaceShip, "shakeFrequency", shakeFrequency_, xmlelement, mode); 121 XMLPortParamVariable(SpaceShip, "shakeAmplitude", shakeAmplitude_, xmlelement, mode); 122 XMLPortParamVariable(SpaceShip, "lift", lift_, xmlelement, mode); 123 XMLPortParamVariable(SpaceShip, "stallSpeed", stallSpeed_, xmlelement, mode); 124 125 XMLPortObject(SpaceShip, Engine, "engines", addEngine, getEngine, xmlelement, mode); 98 126 } 99 127 … … 103 131 registerVariable(this->auxilaryThrust_, VariableDirection::ToClient); 104 132 registerVariable(this->rotationThrust_, VariableDirection::ToClient); 133 // TODO: Synchronization of boost needed? 134 registerVariable(this->boostPower_, VariableDirection::ToClient); 135 registerVariable(this->boostPowerRate_, VariableDirection::ToClient); 136 registerVariable(this->boostRate_, VariableDirection::ToClient); 137 registerVariable(this->boostCooldownDuration_, VariableDirection::ToClient); 138 registerVariable(this->shakeFrequency_, VariableDirection::ToClient); 139 registerVariable(this->shakeAmplitude_, VariableDirection::ToClient); 140 registerVariable(this->lift_, VariableDirection::ToClient); 141 registerVariable(this->stallSpeed_, VariableDirection::ToClient); 105 142 } 106 143 … … 108 145 { 109 146 SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down)."); 147 148 SetConfigValueExternal(bEnableMotionBlur_, "GraphicsSettings", "enableMotionBlur", true) 149 .description("Enable or disable the motion blur effect when moving very fast") 150 .callback(this, &SpaceShip::changedEnableMotionBlur); 151 SetConfigValueExternal(blurStrength_, "GraphicsSettings", "blurStrength", 3.0f) 152 .description("Defines the strength of the motion blur effect"); 110 153 } 111 154 … … 128 171 if (this->hasLocalController()) 129 172 { 130 /* 131 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); 132 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); 133 if (this->localLinearAcceleration_.z() > 0) 134 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); 135 else 136 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); 137 this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); 138 this->localLinearAcceleration_.setValue(0, 0, 0); 139 */ 173 // Handle mouse look 140 174 if (!this->isInMouseLook()) 141 175 { … … 143 177 this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); 144 178 } 145 146 179 this->localAngularAcceleration_.setValue(0, 0, 0); 147 180 181 // Charge boostPower 148 182 if(!this->bBoostCooldown_ && this->boostPower_ < this->initialBoostPower_) 149 183 { 150 184 this->boostPower_ += this->boostPowerRate_*dt; 151 185 } 186 // Use boostPower 152 187 if(this->bBoost_) 153 188 { … … 155 190 if(this->boostPower_ <= 0.0f) 156 191 { 157 this->b Boost_ = false;192 this->boost(false); 158 193 this->bBoostCooldown_ = true; 159 194 this->timer_.setTimer(this->boostCooldownDuration_, false, createExecutor(createFunctor(&SpaceShip::boostCooledDown, this))); 160 195 } 161 } 196 197 this->shakeCamera(dt); 198 } 199 200 // Enable Blur depending on settings 201 if (this->bEnableMotionBlur_ && !this->boostBlur_ && this->hasLocalController() && this->hasHumanController()) 202 { 203 this->boostBlur_ = new Shader(this->getScene()->getSceneManager()); 204 this->boostBlur_->setCompositorName("Radial Blur"); 205 } 206 207 if (this->boostBlur_) // && this->maxSpeedFront_ != 0 && this->boostFactor_ != 1) 208 { 209 // TODO: this->maxSpeedFront_ gets fastest engine 210 float blur = this->blurStrength_ * clamp((-this->getLocalVelocity().z - 0.0f /*this->maxSpeedFront_*/) / ((150.0f /*boostFactor_*/ - 1) * 1.5f /*this->maxSpeedFront_*/), 0.0f, 1.0f); 211 212 // Show and hide blur effect depending on state of booster 213 if(this->bBoost_) 214 this->boostBlur_->setVisible(blur > 0); 215 else 216 this->boostBlur_->setVisible(false); 217 218 this->boostBlur_->setParameter(0, 0, "sampleStrength", blur); 219 } 220 } 221 } 222 223 void SpaceShip::moveFrontBack(const Vector2& value) 224 { 225 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); 226 this->steering_.z -= value.x; 227 } 228 229 void SpaceShip::moveRightLeft(const Vector2& value) 230 { 231 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); 232 this->steering_.x += value.x; 233 } 234 235 void SpaceShip::moveUpDown(const Vector2& value) 236 { 237 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); 238 this->steering_.y += value.x; 239 } 240 241 void SpaceShip::rotateYaw(const Vector2& value) 242 { 243 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); 244 245 Pawn::rotateYaw(value); 246 247 //This function call adds a lift to the ship when it is rotating to make it's movement more "realistic" and enhance the feeling. 248 if (abs(this-> getLocalVelocity().z) < stallSpeed_) {this->moveRightLeft(-lift_ / 5 * value * sqrt(abs(this-> getLocalVelocity().z)));} 249 } 250 251 void SpaceShip::rotatePitch(const Vector2& value) 252 { 253 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); 254 255 Pawn::rotatePitch(value); 256 257 //This function call adds a lift to the ship when it is pitching to make it's movement more "realistic" and enhance the feeling. 258 if (abs(this-> getLocalVelocity().z) < stallSpeed_) {this->moveUpDown(lift_ / 5 * value * sqrt(abs(this-> getLocalVelocity().z)));} 259 } 260 261 void SpaceShip::rotateRoll(const Vector2& value) 262 { 263 this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); 264 265 Pawn::rotateRoll(value); 266 } 267 268 void SpaceShip::fire() 269 { 270 } 271 272 /** 273 @brief 274 Starts or stops boosting. 275 @param bBoost 276 Whether to start or stop boosting. 277 */ 278 void SpaceShip::boost(bool bBoost) 279 { 280 if(bBoost && !this->bBoostCooldown_) 281 { 282 this->bBoost_ = true; 283 Camera* camera = CameraManager::getInstance().getActiveCamera(); 284 this->cameraOriginalPosition_ = camera->getPosition(); 285 this->cameraOriginalOrientation_ = camera->getOrientation(); 286 } 287 if(!bBoost) 288 { 289 this->bBoost_ = false; 290 this->resetCamera(); 162 291 } 163 292 } … … 167 296 this->bBoostCooldown_ = false; 168 297 } 169 170 void SpaceShip::moveFrontBack(const Vector2& value) 171 { 172 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); 173 this->steering_.z = -value.x; 174 } 175 176 void SpaceShip::moveRightLeft(const Vector2& value) 177 { 178 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); 179 this->steering_.x = value.x; 180 } 181 182 void SpaceShip::moveUpDown(const Vector2& value) 183 { 184 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); 185 this->steering_.y = value.x; 186 } 187 188 void SpaceShip::rotateYaw(const Vector2& value) 189 { 190 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x); 191 192 Pawn::rotateYaw(value); 193 } 194 195 void SpaceShip::rotatePitch(const Vector2& value) 196 { 197 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); 198 199 Pawn::rotatePitch(value); 200 } 201 202 void SpaceShip::rotateRoll(const Vector2& value) 203 { 204 this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); 205 206 Pawn::rotateRoll(value); 207 } 208 209 // TODO: something seems to call this function every tick, could probably handled a little more efficiently! 210 void SpaceShip::setBoost(bool bBoost) 211 { 212 if(bBoost == this->bBoost_) 298 299 void SpaceShip::shakeCamera(float dt) 300 { 301 //make sure the ship is only shaking if it's moving 302 if (this->getVelocity().squaredLength() > 80.0f) 303 { 304 this->shakeDt_ += dt; 305 306 float frequency = this->shakeFrequency_ * (this->getVelocity().squaredLength()); 307 308 if (this->shakeDt_ >= 1.0f/frequency) 309 { 310 this->shakeDt_ -= 1.0f/frequency; 311 } 312 313 Degree angle = Degree(sin(this->shakeDt_ *2.0f* math::pi * frequency) * this->shakeAmplitude_); 314 315 //COUT(0) << "Angle: " << angle << std::endl; 316 Camera* camera = this->getCamera(); 317 318 //Shaking Camera effect 319 if (camera != 0) 320 { 321 camera->setOrientation(Vector3::UNIT_X, angle); 322 } 323 } 324 } 325 326 void SpaceShip::resetCamera() 327 { 328 Camera *camera = this->getCamera(); 329 330 if (camera == 0) 331 { 332 COUT(2) << "Failed to reset camera!"; 213 333 return; 214 215 if(bBoost) 216 this->boost(); 334 } 335 336 this->shakeDt_ = 0; 337 camera->setPosition(this->cameraOriginalPosition_); 338 camera->setOrientation(this->cameraOriginalOrientation_); 339 } 340 341 void SpaceShip::backupCamera() 342 { 343 Camera* camera = CameraManager::getInstance().getActiveCamera(); 344 if(camera != NULL) 345 { 346 this->cameraOriginalPosition_ = camera->getPosition(); 347 this->cameraOriginalOrientation_ = camera->getOrientation(); 348 } 349 } 350 351 void SpaceShip::addEngine(orxonox::Engine* engine) 352 { 353 //COUT(0)<<"Adding an Engine: " << engine << endl; 354 this->engineList_.push_back(engine); 355 engine->addToSpaceShip(this); 356 this->resetEngineTicks(); 357 } 358 359 bool SpaceShip::hasEngine(Engine* engine) 360 { 361 for(unsigned int i=0; i<this->engineList_.size(); i++) 362 { 363 if(this->engineList_[i]==engine) 364 return true; 365 } 366 return false; 367 } 368 369 Engine* SpaceShip::getEngine(unsigned int i) 370 { 371 if(this->engineList_.size()>=i) 372 return 0; 217 373 else 218 { 219 this->bBoost_ = false; 220 } 221 } 222 223 void SpaceShip::fire() 224 { 225 } 226 227 void SpaceShip::boost() 228 { 229 if(!this->bBoostCooldown_) 230 this->bBoost_ = true; 231 } 232 233 void SpaceShip::loadEngineTemplate() 234 { 235 if (!this->enginetemplate_.empty()) 236 { 237 Template* temp = Template::getTemplate(this->enginetemplate_); 238 239 if (temp) 240 { 241 Identifier* identifier = temp->getBaseclassIdentifier(); 242 243 if (identifier) 244 { 245 BaseObject* object = identifier->fabricate(this); 246 this->engine_ = orxonox_cast<Engine*>(object); 247 248 if (this->engine_) 249 { 250 this->engine_->addTemplate(temp); 251 this->engine_->addToSpaceShip(this); 252 } 253 else 254 { 255 object->destroy(); 256 } 257 } 258 } 259 } 260 } 261 262 void SpaceShip::setEngine(Engine* engine) 263 { 264 this->engine_ = engine; 265 if (engine && engine->getShip() != this) 266 engine->addToSpaceShip(this); 374 return this->engineList_[i]; 375 } 376 377 void SpaceShip::removeAllEngines() 378 { 379 while(this->engineList_.size()) 380 this->engineList_.back()->destroy(); 381 } 382 383 void SpaceShip::removeEngine(Engine* engine) 384 { 385 for(std::vector<Engine*>::iterator it=this->engineList_.begin(); it!=this->engineList_.end(); ++it) 386 { 387 if(*it==engine) 388 { 389 this->engineList_.erase(it); 390 return; 391 } 392 } 393 } 394 395 void SpaceShip::setSpeedFactor(float factor) 396 { 397 for(unsigned int i=0; i<this->engineList_.size(); i++) 398 this->engineList_[i]->setSpeedFactor(factor); 399 } 400 float SpaceShip::getSpeedFactor() // Calculate mean SpeedFactor. 401 { 402 float ret = 0; unsigned int i = 0; 403 for(; i<this->engineList_.size(); i++) 404 ret += this->engineList_[i]->getSpeedFactor(); 405 ret /= (float)i; 406 return ret; 407 } 408 float SpaceShip::getMaxSpeedFront() 409 { 410 float ret=0; 411 for(unsigned int i=0; i<this->engineList_.size(); i++) 412 { 413 if(this->engineList_[i]->getMaxSpeedFront() > ret) 414 ret = this->engineList_[i]->getMaxSpeedFront(); 415 } 416 return ret; 417 } 418 419 float SpaceShip::getBoostFactor() 420 { 421 float ret = 0; unsigned int i=0; 422 for(; i<this->engineList_.size(); i++) 423 ret += this->engineList_[i]->getBoostFactor(); 424 ret /= (float)i; 425 return ret; 267 426 } 268 427 … … 270 429 { 271 430 std::vector<PickupCarrier*>* list = new std::vector<PickupCarrier*>(); 272 list->push_back(this->engine_); 431 for(unsigned int i=0; i<this->engineList_.size(); i++) 432 list->push_back(this->engineList_[i]); 273 433 return list; 274 434 } 435 436 void SpaceShip::changedEnableMotionBlur() 437 { 438 if (!this->bEnableMotionBlur_) 439 { 440 this->boostBlur_->destroy(); 441 this->boostBlur_ = 0; 442 } 443 } 444 275 445 }
Note: See TracChangeset
for help on using the changeset viewer.