- Timestamp:
- Dec 15, 2008, 12:53:05 AM (15 years ago)
- Location:
- code/branches/presentation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation
- Property svn:mergeinfo changed
-
code/branches/presentation/src/orxonox/objects/worldentities/pawns/SpaceShip.cc
r2371 r2459 30 30 #include "SpaceShip.h" 31 31 32 #include "BulletDynamics/Dynamics/btRigidBody.h" 33 34 #include "util/Math.h" 35 #include "util/Exception.h" 32 36 #include "core/CoreIncludes.h" 33 37 #include "core/ConfigValueIncludes.h" 34 38 #include "core/XMLPort.h" 35 #include "util/Math.h"36 39 37 40 namespace orxonox 38 41 { 42 const float orientationGain = 100; 39 43 CreateFactory(SpaceShip); 40 44 … … 43 47 RegisterObject(SpaceShip); 44 48 45 this->zeroDegree_ = 0; 49 this->primaryThrust_ = 100; 50 this->auxilaryThrust_ = 30; 51 this->rotationThrust_ = 10; 46 52 47 this->maxSpeed_ = 0; 48 this->maxSecondarySpeed_ = 0; 49 this->maxRotation_ = 0; 50 this->translationAcceleration_ = 0; 51 this->rotationAcceleration_ = 0; 52 this->translationDamping_ = 0; 53 54 this->yawRotation_ = 0; 55 this->pitchRotation_ = 0; 56 this->rollRotation_ = 0; 53 this->localLinearAcceleration_.setValue(0, 0, 0); 54 this->localAngularAcceleration_.setValue(0, 0, 0); 57 55 58 56 this->bInvertYAxis_ = false; 59 57 60 58 this->setDestroyWhenPlayerLeft(true); 59 60 // SpaceShip is always a physical object per default 61 // Be aware of this call: The collision type legality check will not reach derived classes! 62 this->setCollisionType(WorldEntity::Dynamic); 61 63 62 64 this->setConfigValues(); … … 72 74 SUPER(SpaceShip, XMLPort, xmlelement, mode); 73 75 74 XMLPortParam(SpaceShip, "maxspeed", setMaxSpeed, getMaxSpeed, xmlelement, mode); 75 XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode); 76 XMLPortParam(SpaceShip, "maxrotation", setMaxRotation, getMaxRotation, xmlelement, mode); 77 XMLPortParam(SpaceShip, "transacc", setTransAcc, getTransAcc, xmlelement, mode); 78 XMLPortParam(SpaceShip, "rotacc", setRotAcc, getRotAcc, xmlelement, mode); 79 XMLPortParam(SpaceShip, "transdamp", setTransDamp, getTransDamp, xmlelement, mode); 76 XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); 77 XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); 78 XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); 80 79 } 81 80 82 81 void SpaceShip::registerVariables() 83 82 { 84 registerVariable(this->maxSpeed_, variableDirection::toclient); 85 registerVariable(this->maxSecondarySpeed_, variableDirection::toclient); 86 registerVariable(this->maxRotation_, variableDirection::toclient); 87 registerVariable(this->translationAcceleration_, variableDirection::toclient); 88 registerVariable(this->rotationAcceleration_, variableDirection::toclient); 89 registerVariable(this->translationDamping_, variableDirection::toclient); 83 registerVariable(this->primaryThrust_, variableDirection::toclient); 84 registerVariable(this->auxilaryThrust_, variableDirection::toclient); 85 registerVariable(this->rotationThrust_, variableDirection::toclient); 90 86 } 91 87 … … 95 91 } 96 92 93 bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const 94 { 95 if (type != WorldEntity::Dynamic) 96 { 97 CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; 98 assert(false); // Only in debug mode 99 return false; 100 } 101 else 102 return true; 103 } 104 97 105 void SpaceShip::tick(float dt) 98 106 { 99 if (this->isLocallyControlled())100 {101 // #####################################102 // ############# STEERING ##############103 // #####################################104 105 Vector3 velocity = this->getVelocity();106 if (velocity.x > this->maxSecondarySpeed_)107 velocity.x = this->maxSecondarySpeed_;108 if (velocity.x < -this->maxSecondarySpeed_)109 velocity.x = -this->maxSecondarySpeed_;110 if (velocity.y > this->maxSecondarySpeed_)111 velocity.y = this->maxSecondarySpeed_;112 if (velocity.y < -this->maxSecondarySpeed_)113 velocity.y = -this->maxSecondarySpeed_;114 if (velocity.z > this->maxSecondarySpeed_)115 velocity.z = this->maxSecondarySpeed_;116 if (velocity.z < -this->maxSpeed_)117 velocity.z = -this->maxSpeed_;118 119 // normalize velocity and acceleration120 for (size_t dimension = 0; dimension < 3; ++dimension)121 {122 if (this->acceleration_[dimension] == 0)123 {124 if (velocity[dimension] > 0)125 {126 velocity[dimension] -= (this->translationDamping_ * dt);127 if (velocity[dimension] < 0)128 velocity[dimension] = 0;129 }130 else if (velocity[dimension] < 0)131 {132 velocity[dimension] += (this->translationDamping_ * dt);133 if (velocity[dimension] > 0)134 velocity[dimension] = 0;135 }136 }137 }138 139 this->setVelocity(velocity);140 }141 142 143 107 SUPER(SpaceShip, tick, dt); 144 145 108 146 109 if (this->isLocallyControlled()) 147 110 { 148 this->yaw(this->yawRotation_ * dt); 149 if (this->bInvertYAxis_) 150 this->pitch(Degree(-this->pitchRotation_ * dt)); 111 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); 112 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); 113 if (this->localLinearAcceleration_.z() > 0) 114 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); 151 115 else 152 this->pitch(Degree( this->pitchRotation_ * dt)); 153 this->roll(this->rollRotation_ * dt); 116 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); 117 this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); 118 this->localLinearAcceleration_.setValue(0, 0, 0); 154 119 155 this->acceleration_.x = 0; 156 this->acceleration_.y = 0; 157 this->acceleration_.z = 0; 158 159 this->yawRotation_ = this->zeroDegree_; 160 this->pitchRotation_ = this->zeroDegree_; 161 this->rollRotation_ = this->zeroDegree_; 120 this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; 121 this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); 122 this->localAngularAcceleration_.setValue(0, 0, 0); 162 123 } 163 124 } … … 165 126 void SpaceShip::moveFrontBack(const Vector2& value) 166 127 { 167 this-> acceleration_.z = -this->translationAcceleration_ * value.x;128 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); 168 129 } 169 130 170 131 void SpaceShip::moveRightLeft(const Vector2& value) 171 132 { 172 this-> acceleration_.x = this->translationAcceleration_ * value.x;133 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); 173 134 } 174 135 175 136 void SpaceShip::moveUpDown(const Vector2& value) 176 137 { 177 this-> acceleration_.y = this->translationAcceleration_ * value.x;138 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); 178 139 } 179 140 180 141 void SpaceShip::rotateYaw(const Vector2& value) 181 142 { 182 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 183 if (temp > this->maxRotation_) 184 temp = this->maxRotation_; 185 if (temp < -this->maxRotation_) 186 temp = -this->maxRotation_; 187 this->yawRotation_ = Degree(temp); 143 this->localAngularAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); 188 144 } 189 145 190 146 void SpaceShip::rotatePitch(const Vector2& value) 191 147 { 192 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 193 if (temp > this->maxRotation_) 194 temp = this->maxRotation_; 195 if (temp < -this->maxRotation_) 196 temp = -this->maxRotation_; 197 this->pitchRotation_ = Degree(temp); 148 this->localAngularAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); 198 149 } 199 150 200 151 void SpaceShip::rotateRoll(const Vector2& value) 201 152 { 202 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 203 if (temp > this->maxRotation_) 204 temp = this->maxRotation_; 205 if (temp < -this->maxRotation_) 206 temp = -this->maxRotation_; 207 this->rollRotation_ = Degree(temp); 153 this->localAngularAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); 208 154 } 209 155
Note: See TracChangeset
for help on using the changeset viewer.