[2072] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
[2304] | 23 | * Reto Grieder |
---|
[2072] | 24 | * Co-authors: |
---|
[2201] | 25 | * Martin Stypinski |
---|
[2072] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "MovableEntity.h" |
---|
| 31 | |
---|
[2303] | 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 33 | |
---|
[2306] | 34 | #include "util/Debug.h" |
---|
[2374] | 35 | #include "util/MathConvert.h" |
---|
[2292] | 36 | #include "util/Exception.h" |
---|
[2072] | 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | |
---|
[2292] | 40 | #include "objects/Scene.h" |
---|
| 41 | |
---|
[2072] | 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(MovableEntity); |
---|
| 47 | |
---|
[2374] | 48 | this->linearAcceleration_ = Vector3::ZERO; |
---|
| 49 | this->linearVelocity_ = Vector3::ZERO; |
---|
| 50 | this->angularAcceleration_ = Vector3::ZERO; |
---|
| 51 | this->angularVelocity_ = Vector3::ZERO; |
---|
[2300] | 52 | |
---|
[2072] | 53 | this->registerVariables(); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | MovableEntity::~MovableEntity() |
---|
| 57 | { |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 61 | { |
---|
| 62 | SUPER(MovableEntity, XMLPort, xmlelement, mode); |
---|
[2300] | 63 | |
---|
[2374] | 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); |
---|
[2072] | 67 | } |
---|
| 68 | |
---|
| 69 | void MovableEntity::registerVariables() |
---|
| 70 | { |
---|
| 71 | } |
---|
[2201] | 72 | |
---|
[2374] | 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 | |
---|
[2292] | 106 | void MovableEntity::setPosition(const Vector3& position) |
---|
| 107 | { |
---|
| 108 | if (this->isDynamic()) |
---|
| 109 | { |
---|
[2374] | 110 | //if (this->isPhysicsRunning()) |
---|
| 111 | // return; |
---|
[2292] | 112 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 113 | transf.setOrigin(btVector3(position.x, position.y, position.z)); |
---|
| 114 | this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 115 | } |
---|
[2300] | 116 | |
---|
[2374] | 117 | //COUT(0) << "setting position: " << position << std::endl; |
---|
[2300] | 118 | this->node_->setPosition(position); |
---|
[2374] | 119 | positionChanged(false); |
---|
[2201] | 120 | } |
---|
| 121 | |
---|
[2292] | 122 | void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo) |
---|
| 123 | { |
---|
| 124 | if (this->isDynamic()) |
---|
| 125 | { |
---|
| 126 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \ |
---|
| 127 | to any other space than TS_LOCAL."); |
---|
| 128 | this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z)); |
---|
[2201] | 129 | } |
---|
[2300] | 130 | |
---|
| 131 | this->node_->translate(distance, relativeTo); |
---|
[2374] | 132 | positionChanged(false); |
---|
[2201] | 133 | } |
---|
| 134 | |
---|
[2292] | 135 | void MovableEntity::setOrientation(const Quaternion& orientation) |
---|
| 136 | { |
---|
| 137 | if (this->isDynamic()) |
---|
| 138 | { |
---|
| 139 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
[2374] | 140 | transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w)); |
---|
[2292] | 141 | this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 142 | } |
---|
[2300] | 143 | |
---|
| 144 | this->node_->setOrientation(orientation); |
---|
[2374] | 145 | orientationChanged(false); |
---|
[2201] | 146 | } |
---|
| 147 | |
---|
[2292] | 148 | void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) |
---|
| 149 | { |
---|
| 150 | if (this->isDynamic()) |
---|
| 151 | { |
---|
| 152 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \ |
---|
| 153 | to any other space than TS_LOCAL."); |
---|
| 154 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
[2374] | 155 | this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w))); |
---|
[2201] | 156 | } |
---|
[2300] | 157 | |
---|
| 158 | this->node_->rotate(rotation, relativeTo); |
---|
[2374] | 159 | orientationChanged(false); |
---|
[2201] | 160 | } |
---|
| 161 | |
---|
[2292] | 162 | void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 163 | { |
---|
| 164 | if (this->isDynamic()) |
---|
| 165 | { |
---|
| 166 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \ |
---|
| 167 | to any other space than TS_LOCAL."); |
---|
| 168 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 169 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
| 170 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
[2201] | 171 | } |
---|
[2300] | 172 | |
---|
| 173 | this->node_->yaw(angle, relativeTo); |
---|
[2374] | 174 | orientationChanged(false); |
---|
[2201] | 175 | } |
---|
| 176 | |
---|
[2292] | 177 | void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 178 | { |
---|
| 179 | if (this->isDynamic()) |
---|
| 180 | { |
---|
| 181 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \ |
---|
| 182 | to any other space than TS_LOCAL."); |
---|
| 183 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 184 | btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f)); |
---|
| 185 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
[2201] | 186 | } |
---|
[2300] | 187 | |
---|
| 188 | this->node_->pitch(angle, relativeTo); |
---|
[2374] | 189 | orientationChanged(false); |
---|
[2201] | 190 | } |
---|
| 191 | |
---|
[2292] | 192 | void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
| 193 | { |
---|
| 194 | if (this->isDynamic()) |
---|
| 195 | { |
---|
| 196 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \ |
---|
| 197 | to any other space than TS_LOCAL."); |
---|
| 198 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 199 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
| 200 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
[2201] | 201 | } |
---|
[2300] | 202 | |
---|
| 203 | this->node_->roll(angle, relativeTo); |
---|
[2374] | 204 | orientationChanged(false); |
---|
[2201] | 205 | } |
---|
| 206 | |
---|
[2292] | 207 | void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
| 208 | { |
---|
| 209 | if (this->isDynamic()) |
---|
| 210 | { |
---|
| 211 | ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects."); |
---|
| 212 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
| 213 | to any other space than TS_LOCAL."); |
---|
| 214 | //btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 215 | //this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 216 | } |
---|
[2300] | 217 | |
---|
| 218 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
[2374] | 219 | orientationChanged(false); |
---|
[2201] | 220 | } |
---|
| 221 | |
---|
[2292] | 222 | void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
| 223 | { |
---|
| 224 | if (this->isDynamic()) |
---|
| 225 | { |
---|
[2300] | 226 | ThrowException(NotImplemented, "ControllableEntity::setDirection() is not yet supported for physical objects."); |
---|
[2292] | 227 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
| 228 | to any other space than TS_LOCAL."); |
---|
| 229 | //btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 230 | //this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 231 | } |
---|
[2300] | 232 | |
---|
| 233 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
[2374] | 234 | orientationChanged(false); |
---|
[2300] | 235 | } |
---|
| 236 | |
---|
| 237 | void MovableEntity::setVelocity(const Vector3& velocity) |
---|
| 238 | { |
---|
| 239 | if (this->isDynamic()) |
---|
[2374] | 240 | this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); |
---|
| 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()) |
---|
[2292] | 266 | { |
---|
[2374] | 267 | btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal()); |
---|
| 268 | this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia); |
---|
[2292] | 269 | } |
---|
[2300] | 270 | |
---|
[2374] | 271 | this->angularAcceleration_ = acceleration; |
---|
[2201] | 272 | } |
---|
| 273 | |
---|
[2300] | 274 | bool MovableEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2298] | 275 | { |
---|
| 276 | if (type == WorldEntity::Static) |
---|
| 277 | { |
---|
| 278 | ThrowException(PhysicsViolation, "Cannot tell a MovableEntity to have static collision type"); |
---|
| 279 | return false; |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | return true; |
---|
| 283 | } |
---|
| 284 | |
---|
[2292] | 285 | void MovableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
| 286 | { |
---|
| 287 | // We use a dynamic body. So we translate our node accordingly. |
---|
| 288 | this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z())); |
---|
| 289 | this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z())); |
---|
[2374] | 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); |
---|
[2292] | 301 | } |
---|
| 302 | |
---|
| 303 | void MovableEntity::getWorldTransform(btTransform& worldTrans) const |
---|
| 304 | { |
---|
[2374] | 305 | // We use a kinematic body |
---|
[2292] | 306 | worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z)); |
---|
[2374] | 307 | worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w)); |
---|
[2300] | 308 | if (this->isDynamic()) |
---|
| 309 | { |
---|
| 310 | // This function gets called only once for dynamic objects to set the initial conditions |
---|
[2374] | 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)); |
---|
[2300] | 314 | } |
---|
[2292] | 315 | } |
---|
[2072] | 316 | } |
---|