Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/MobileEntity.cc @ 3180

Last change on this file since 3180 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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