- Timestamp:
- Dec 10, 2008, 1:38:17 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc
r2306 r2374 33 33 34 34 #include "util/Debug.h" 35 #include "util/MathConvert.h" 35 36 #include "util/Exception.h" 36 37 #include "core/CoreIncludes.h" … … 45 46 RegisterObject(MovableEntity); 46 47 47 this->velocity_ = Vector3::ZERO; 48 this->linearAcceleration_ = Vector3::ZERO; 49 this->linearVelocity_ = Vector3::ZERO; 50 this->angularAcceleration_ = Vector3::ZERO; 51 this->angularVelocity_ = Vector3::ZERO; 48 52 49 53 this->registerVariables(); … … 58 62 SUPER(MovableEntity, XMLPort, xmlelement, mode); 59 63 60 XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&); 64 XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&); 65 XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&); 66 XMLPortParam(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode); 61 67 } 62 68 … … 65 71 } 66 72 73 void MovableEntity::tick(float dt) 74 { 75 if (this->isActive()) 76 { 77 // Check whether Bullet doesn't do the physics for us 78 if (!this->isDynamic()) 79 { 80 // Linear part 81 this->linearVelocity_.x += this->linearAcceleration_.x * dt; 82 this->linearVelocity_.y += this->linearAcceleration_.y * dt; 83 this->linearVelocity_.z += this->linearAcceleration_.z * dt; 84 linearVelocityChanged(true); 85 this->node_->translate(this->linearVelocity_ * dt); 86 positionChanged(true); 87 88 // Angular part 89 // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3 90 this->angularVelocity_.x += angularAcceleration_.x * dt; 91 this->angularVelocity_.y += angularAcceleration_.y * dt; 92 this->angularVelocity_.z += angularAcceleration_.z * dt; 93 angularVelocityChanged(true); 94 // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method. 95 float mult = dt * 0.5; 96 // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions! 97 Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult); 98 newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation(); 99 newOrientation.normalise(); 100 this->node_->setOrientation(newOrientation); 101 orientationChanged(true); 102 } 103 } 104 } 105 67 106 void MovableEntity::setPosition(const Vector3& position) 68 107 { 69 108 if (this->isDynamic()) 70 109 { 110 //if (this->isPhysicsRunning()) 111 // return; 71 112 btTransform transf = this->physicalBody_->getWorldTransform(); 72 113 transf.setOrigin(btVector3(position.x, position.y, position.z)); … … 74 115 } 75 116 117 //COUT(0) << "setting position: " << position << std::endl; 76 118 this->node_->setPosition(position); 77 positionChanged( );119 positionChanged(false); 78 120 } 79 121 … … 88 130 89 131 this->node_->translate(distance, relativeTo); 90 positionChanged( );132 positionChanged(false); 91 133 } 92 134 … … 96 138 { 97 139 btTransform transf = this->physicalBody_->getWorldTransform(); 98 transf.setRotation(btQuaternion(orientation. w, orientation.x, orientation.y, orientation.z));140 transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w)); 99 141 this->physicalBody_->setWorldTransform(transf); 100 142 } 101 143 102 144 this->node_->setOrientation(orientation); 103 orientationChanged( );145 orientationChanged(false); 104 146 } 105 147 … … 111 153 to any other space than TS_LOCAL."); 112 154 btTransform transf = this->physicalBody_->getWorldTransform(); 113 this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation. w, rotation.x, rotation.y, rotation.z)));155 this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w))); 114 156 } 115 157 116 158 this->node_->rotate(rotation, relativeTo); 117 orientationChanged( );159 orientationChanged(false); 118 160 } 119 161 … … 130 172 131 173 this->node_->yaw(angle, relativeTo); 132 orientationChanged( );174 orientationChanged(false); 133 175 } 134 176 … … 145 187 146 188 this->node_->pitch(angle, relativeTo); 147 orientationChanged( );189 orientationChanged(false); 148 190 } 149 191 … … 160 202 161 203 this->node_->roll(angle, relativeTo); 162 orientationChanged( );204 orientationChanged(false); 163 205 } 164 206 … … 175 217 176 218 this->node_->lookAt(target, relativeTo, localDirectionVector); 177 orientationChanged( );219 orientationChanged(false); 178 220 } 179 221 … … 190 232 191 233 this->node_->setDirection(direction, relativeTo, localDirectionVector); 192 orientationChanged( );234 orientationChanged(false); 193 235 } 194 236 … … 196 238 { 197 239 if (this->isDynamic()) 198 {199 240 this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); 200 } 201 202 this->velocity_ = velocity; 203 velocityChanged(); 241 242 this->linearVelocity_ = velocity; 243 linearVelocityChanged(false); 244 } 245 246 void MovableEntity::setAngularVelocity(const Vector3& velocity) 247 { 248 if (this->isDynamic()) 249 this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z)); 250 251 this->angularVelocity_ = velocity; 252 angularVelocityChanged(false); 253 } 254 255 void MovableEntity::setAcceleration(const Vector3& acceleration) 256 { 257 if (this->isDynamic()) 258 this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass())); 259 260 this->linearAcceleration_ = acceleration; 261 } 262 263 void MovableEntity::setAngularAcceleration(const Vector3& acceleration) 264 { 265 if (this->isDynamic()) 266 { 267 btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal()); 268 this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia); 269 } 270 271 this->angularAcceleration_ = acceleration; 204 272 } 205 273 … … 220 288 this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z())); 221 289 this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z())); 222 const btVector3& velocity = this->physicalBody_->getLinearVelocity(); 223 this->velocity_.x = velocity.x(); 224 this->velocity_.y = velocity.y(); 225 this->velocity_.z = velocity.z(); 226 velocityChanged(); 227 positionChanged(); 228 orientationChanged(); 290 COUT(0) << "setting world transform: " << omni_cast<std::string>(node_->getPosition()) << std::endl; 291 this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x(); 292 this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y(); 293 this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z(); 294 this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x(); 295 this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y(); 296 this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z(); 297 linearVelocityChanged(true); 298 angularVelocityChanged(true); 299 positionChanged(true); 300 orientationChanged(true); 229 301 } 230 302 231 303 void MovableEntity::getWorldTransform(btTransform& worldTrans) const 232 304 { 233 // We use a kinematic body 305 // We use a kinematic body 234 306 worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z)); 235 worldTrans.setRotation(btQuaternion(node_->getOrientation(). w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));307 worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w)); 236 308 if (this->isDynamic()) 237 309 { 238 310 // This function gets called only once for dynamic objects to set the initial conditions 239 // We have to set the velocity too. 240 this->physicalBody_->setLinearVelocity(btVector3(velocity_.x, velocity_.y, velocity_.z)); 311 // We have to set the velocities too. 312 this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z)); 313 this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z)); 241 314 } 242 315 }
Note: See TracChangeset
for help on using the changeset viewer.