Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3125 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
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:
23 *      Reto Grieder
24 *   Co-authors:
25 *      Martin Stypinski
26 *
27 */
28
29#include "MobileEntity.h"
30
31#include <OgreSceneNode.h>
32#include "BulletDynamics/Dynamics/btRigidBody.h"
33
34#include "util/Debug.h"
35#include "util/MathConvert.h"
36#include "util/Exception.h"
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
40#include "objects/Scene.h"
41
42namespace orxonox
43{
44    MobileEntity::MobileEntity(BaseObject* creator) : WorldEntity(creator)
45    {
46        RegisterObject(MobileEntity);
47
48        this->linearAcceleration_  = Vector3::ZERO;
49        this->linearVelocity_      = Vector3::ZERO;
50        this->angularAcceleration_ = Vector3::ZERO;
51        this->angularVelocity_     = Vector3::ZERO;
52
53        this->registerVariables();
54    }
55
56    MobileEntity::~MobileEntity()
57    {
58    }
59
60    void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
61    {
62        SUPER(MobileEntity, XMLPort, xmlelement, mode);
63
64        XMLPortParamTemplate(MobileEntity, "velocity",     setVelocity,     getVelocity,     xmlelement, mode, const Vector3&);
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        }
77    }
78
79    void MobileEntity::tick(float dt)
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
108    void MobileEntity::setPosition(const Vector3& position)
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);
115        }
116
117        this->node_->setPosition(position);
118    }
119
120    void MobileEntity::setOrientation(const Quaternion& orientation)
121    {
122        if (this->isDynamic())
123        {
124            btTransform transf = this->physicalBody_->getWorldTransform();
125            transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w));
126            this->physicalBody_->setWorldTransform(transf);
127        }
128
129        this->node_->setOrientation(orientation);
130    }
131
132    void MobileEntity::setVelocity(const Vector3& velocity)
133    {
134        if (this->isDynamic())
135            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
136
137        this->linearVelocity_ = velocity;
138    }
139
140    void MobileEntity::setAngularVelocity(const Vector3& velocity)
141    {
142        if (this->isDynamic())
143            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
144
145        this->angularVelocity_ = velocity;
146    }
147
148    void MobileEntity::setAcceleration(const Vector3& acceleration)
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
156    void MobileEntity::setAngularAcceleration(const Vector3& acceleration)
157    {
158        if (this->isDynamic())
159        {
160            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
161            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
162        }
163
164        this->angularAcceleration_ = acceleration;
165    }
166
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
173    bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
174    {
175        if (type == WorldEntity::Static)
176        {
177            CCOUT(1) << "Error: Cannot tell a MobileEntity to have static collision type! Ignoring." << std::endl;
178            assert(false); // Only in debug mode
179            return false;
180        }
181        else
182            return true;
183    }
184
185    void MobileEntity::setWorldTransform(const btTransform& worldTrans)
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()));
189        btQuaternion temp(worldTrans.getRotation());
190        this->node_->setOrientation(Quaternion(temp.w(), temp.x(), temp.y(), temp.z()));
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();
197    }
198
199    void MobileEntity::getWorldTransform(btTransform& worldTrans) const
200    {
201        // We use a kinematic body
202        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
203        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
204        if (this->isDynamic())
205        {
206            // This function gets called only once for dynamic objects to set the initial conditions
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));
210        }
211    }
212}
Note: See TracBrowser for help on using the repository browser.