Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/worldentities/MobileEntity.cc @ 3182

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

Found out that that Debug.h is officially included in CoreIncludes.h ;)

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