[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 | |
---|
[2408] | 29 | #include "MobileEntity.h" |
---|
[2072] | 30 | |
---|
[2426] | 31 | #include <OgreSceneNode.h> |
---|
[3196] | 32 | #include <BulletDynamics/Dynamics/btRigidBody.h> |
---|
[2303] | 33 | |
---|
[2072] | 34 | #include "core/CoreIncludes.h" |
---|
| 35 | #include "core/XMLPort.h" |
---|
[5735] | 36 | #include "Scene.h" |
---|
[2292] | 37 | |
---|
[2072] | 38 | namespace orxonox |
---|
| 39 | { |
---|
[2408] | 40 | MobileEntity::MobileEntity(BaseObject* creator) : WorldEntity(creator) |
---|
[2072] | 41 | { |
---|
[2408] | 42 | RegisterObject(MobileEntity); |
---|
[2072] | 43 | |
---|
[2374] | 44 | this->linearAcceleration_ = Vector3::ZERO; |
---|
| 45 | this->linearVelocity_ = Vector3::ZERO; |
---|
| 46 | this->angularAcceleration_ = Vector3::ZERO; |
---|
| 47 | this->angularVelocity_ = Vector3::ZERO; |
---|
[2469] | 48 | |
---|
| 49 | this->registerVariables(); |
---|
[2072] | 50 | } |
---|
| 51 | |
---|
[2408] | 52 | MobileEntity::~MobileEntity() |
---|
[2072] | 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
[2408] | 56 | void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2072] | 57 | { |
---|
[2408] | 58 | SUPER(MobileEntity, XMLPort, xmlelement, mode); |
---|
[2300] | 59 | |
---|
[2408] | 60 | XMLPortParamTemplate(MobileEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&); |
---|
[2491] | 61 | |
---|
| 62 | Vector3 rotationAxis(this->getRotationAxis()); |
---|
| 63 | Degree rotationRate = this->getRotationRate(); |
---|
| 64 | XMLPortParamVariable(MobileEntity, "rotationaxis", rotationAxis, xmlelement, mode); |
---|
| 65 | XMLPortParamVariable(MobileEntity, "rotationrate", rotationRate, xmlelement, mode); |
---|
| 66 | if (mode == XMLPort::LoadObject) |
---|
| 67 | { |
---|
| 68 | if (rotationAxis == Vector3::ZERO) |
---|
| 69 | this->setAngularVelocity(Vector3::ZERO); |
---|
| 70 | else |
---|
| 71 | this->setAngularVelocity(rotationAxis.normalisedCopy() * rotationRate.valueRadians()); |
---|
| 72 | } |
---|
[2072] | 73 | } |
---|
| 74 | |
---|
[2408] | 75 | void MobileEntity::tick(float dt) |
---|
[2374] | 76 | { |
---|
| 77 | if (this->isActive()) |
---|
| 78 | { |
---|
| 79 | // Check whether Bullet doesn't do the physics for us |
---|
| 80 | if (!this->isDynamic()) |
---|
| 81 | { |
---|
| 82 | // Linear part |
---|
| 83 | this->linearVelocity_.x += this->linearAcceleration_.x * dt; |
---|
| 84 | this->linearVelocity_.y += this->linearAcceleration_.y * dt; |
---|
| 85 | this->linearVelocity_.z += this->linearAcceleration_.z * dt; |
---|
| 86 | this->node_->translate(this->linearVelocity_ * dt); |
---|
| 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 | // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method. |
---|
[3196] | 94 | float mult = dt * 0.5f; |
---|
[2374] | 95 | // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions! |
---|
| 96 | Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult); |
---|
| 97 | newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation(); |
---|
| 98 | newOrientation.normalise(); |
---|
| 99 | this->node_->setOrientation(newOrientation); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[2408] | 104 | void MobileEntity::setPosition(const Vector3& position) |
---|
[2292] | 105 | { |
---|
| 106 | if (this->isDynamic()) |
---|
| 107 | { |
---|
| 108 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
| 109 | transf.setOrigin(btVector3(position.x, position.y, position.z)); |
---|
| 110 | this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 111 | } |
---|
[2300] | 112 | |
---|
| 113 | this->node_->setPosition(position); |
---|
[2201] | 114 | } |
---|
| 115 | |
---|
[2408] | 116 | void MobileEntity::setOrientation(const Quaternion& orientation) |
---|
[2292] | 117 | { |
---|
| 118 | if (this->isDynamic()) |
---|
| 119 | { |
---|
| 120 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
[2374] | 121 | transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w)); |
---|
[2292] | 122 | this->physicalBody_->setWorldTransform(transf); |
---|
[2201] | 123 | } |
---|
[2300] | 124 | |
---|
| 125 | this->node_->setOrientation(orientation); |
---|
[2201] | 126 | } |
---|
| 127 | |
---|
[2408] | 128 | void MobileEntity::setVelocity(const Vector3& velocity) |
---|
[2300] | 129 | { |
---|
| 130 | if (this->isDynamic()) |
---|
[2374] | 131 | this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); |
---|
| 132 | |
---|
| 133 | this->linearVelocity_ = velocity; |
---|
| 134 | } |
---|
| 135 | |
---|
[2408] | 136 | void MobileEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2374] | 137 | { |
---|
| 138 | if (this->isDynamic()) |
---|
| 139 | this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z)); |
---|
| 140 | |
---|
| 141 | this->angularVelocity_ = velocity; |
---|
| 142 | } |
---|
| 143 | |
---|
[2408] | 144 | void MobileEntity::setAcceleration(const Vector3& acceleration) |
---|
[2374] | 145 | { |
---|
| 146 | if (this->isDynamic()) |
---|
| 147 | this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass())); |
---|
| 148 | |
---|
| 149 | this->linearAcceleration_ = acceleration; |
---|
| 150 | } |
---|
| 151 | |
---|
[2408] | 152 | void MobileEntity::setAngularAcceleration(const Vector3& acceleration) |
---|
[2374] | 153 | { |
---|
| 154 | if (this->isDynamic()) |
---|
[2292] | 155 | { |
---|
[2374] | 156 | btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal()); |
---|
| 157 | this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia); |
---|
[2292] | 158 | } |
---|
[2300] | 159 | |
---|
[2374] | 160 | this->angularAcceleration_ = acceleration; |
---|
[2201] | 161 | } |
---|
| 162 | |
---|
[3033] | 163 | void MobileEntity::applyCentralForce(const Vector3& force) |
---|
| 164 | { |
---|
| 165 | if (this->isDynamic()) |
---|
| 166 | this->physicalBody_->applyCentralForce(btVector3(force.x * this->getMass(), force.y * this->getMass(), force.z * this->getMass())); |
---|
| 167 | } |
---|
| 168 | |
---|
[2408] | 169 | bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
[2298] | 170 | { |
---|
| 171 | if (type == WorldEntity::Static) |
---|
| 172 | { |
---|
[2454] | 173 | CCOUT(1) << "Error: Cannot tell a MobileEntity to have static collision type! Ignoring." << std::endl; |
---|
| 174 | assert(false); // Only in debug mode |
---|
[2298] | 175 | return false; |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | return true; |
---|
| 179 | } |
---|
| 180 | |
---|
[2408] | 181 | void MobileEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2292] | 182 | { |
---|
| 183 | // We use a dynamic body. So we translate our node accordingly. |
---|
| 184 | this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z())); |
---|
[2780] | 185 | btQuaternion temp(worldTrans.getRotation()); |
---|
| 186 | this->node_->setOrientation(Quaternion(temp.w(), temp.x(), temp.y(), temp.z())); |
---|
[2374] | 187 | this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x(); |
---|
| 188 | this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y(); |
---|
| 189 | this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z(); |
---|
| 190 | this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x(); |
---|
| 191 | this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y(); |
---|
| 192 | this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z(); |
---|
[2292] | 193 | } |
---|
| 194 | |
---|
[2408] | 195 | void MobileEntity::getWorldTransform(btTransform& worldTrans) const |
---|
[2292] | 196 | { |
---|
[2374] | 197 | // We use a kinematic body |
---|
[2292] | 198 | worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z)); |
---|
[2374] | 199 | worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w)); |
---|
[2300] | 200 | if (this->isDynamic()) |
---|
| 201 | { |
---|
| 202 | // This function gets called only once for dynamic objects to set the initial conditions |
---|
[2374] | 203 | // We have to set the velocities too. |
---|
| 204 | this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z)); |
---|
| 205 | this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z)); |
---|
[2300] | 206 | } |
---|
[2292] | 207 | } |
---|
[2072] | 208 | } |
---|