Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/MobileEntity.cc @ 2426

Last change on this file since 2426 was 2426, checked in by rgrieder, 15 years ago
  • Handled translate, rotate, yaw, pitch, roll, lookAt and setDirection internally in WE. That only leaves setPosition and setOrientation to be pure virtual.
  • Removed loads of redundant and now obsolete code in MobileEntity and StaticEntity
  • Removed OgreSceneNode.h from WorldEntity in debug builds. getPosition, getOrientation and getScale3D are only inline in release mode. That should speed up dev builds a lot (the include is about 8300 line without Vector3, Quaternion, etc.)

Important:

  • Fixed a bug or introduced one: the lookAt(…) function in WE had default TransformSpace "TS_LOCAL", but that makes no sense the way I see it. Consider a SpaceShip and you would like it to face an asteroid. Then TS_LOCAL would yield weird results. The XMLPort attribute "lookAt" used the default value. That's where the bug might have been fixed or (I don't believe so) introduced.
  • Property svn:eol-style set to native
File size: 8.2 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 "OrxonoxStableHeaders.h"
30#include "MobileEntity.h"
31
32#include <OgreSceneNode.h>
33#include "BulletDynamics/Dynamics/btRigidBody.h"
34
35#include "util/Debug.h"
36#include "util/MathConvert.h"
37#include "util/Exception.h"
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40
41#include "objects/Scene.h"
42
43namespace orxonox
44{
45    MobileEntity::MobileEntity(BaseObject* creator) : WorldEntity(creator)
46    {
47        RegisterObject(MobileEntity);
48
49        this->linearAcceleration_  = Vector3::ZERO;
50        this->linearVelocity_      = Vector3::ZERO;
51        this->angularAcceleration_ = Vector3::ZERO;
52        this->angularVelocity_     = Vector3::ZERO;
53
54        this->registerVariables();
55    }
56
57    MobileEntity::~MobileEntity()
58    {
59    }
60
61    void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(MobileEntity, XMLPort, xmlelement, mode);
64
65        XMLPortParamTemplate(MobileEntity, "velocity",     setVelocity,     getVelocity,     xmlelement, mode, const Vector3&);
66        XMLPortParamTemplate(MobileEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);
67        XMLPortParam(MobileEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode);
68    }
69
70    void MobileEntity::registerVariables()
71    {
72    }
73
74    void MobileEntity::tick(float dt)
75    {
76        if (this->isActive())
77        {
78            // Check whether Bullet doesn't do the physics for us
79            if (!this->isDynamic())
80            {
81                // Linear part
82                this->linearVelocity_.x += this->linearAcceleration_.x * dt;
83                this->linearVelocity_.y += this->linearAcceleration_.y * dt;
84                this->linearVelocity_.z += this->linearAcceleration_.z * dt;
85                linearVelocityChanged(true);
86                this->node_->translate(this->linearVelocity_ * dt);
87                positionChanged(true);
88
89                // Angular part
90                // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3
91                this->angularVelocity_.x += angularAcceleration_.x * dt;
92                this->angularVelocity_.y += angularAcceleration_.y * dt;
93                this->angularVelocity_.z += angularAcceleration_.z * dt;
94                angularVelocityChanged(true);
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                orientationChanged(true);
103            }
104        }
105    }
106
107    void MobileEntity::setPosition(const Vector3& position)
108    {
109        if (this->isDynamic())
110        {
111            btTransform transf = this->physicalBody_->getWorldTransform();
112            transf.setOrigin(btVector3(position.x, position.y, position.z));
113            this->physicalBody_->setWorldTransform(transf);
114        }
115
116        this->node_->setPosition(position);
117        positionChanged(false);
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        orientationChanged(false);
131    }
132
133    void MobileEntity::setVelocity(const Vector3& velocity)
134    {
135        if (this->isDynamic())
136            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
137
138        this->linearVelocity_ = velocity;
139        linearVelocityChanged(false);
140    }
141
142    void MobileEntity::setAngularVelocity(const Vector3& velocity)
143    {
144        if (this->isDynamic())
145            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
146
147        this->angularVelocity_ = velocity;
148        angularVelocityChanged(false);
149    }
150
151    void MobileEntity::setAcceleration(const Vector3& acceleration)
152    {
153        if (this->isDynamic())
154            this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass()));
155
156        this->linearAcceleration_ = acceleration;
157    }
158
159    void MobileEntity::setAngularAcceleration(const Vector3& acceleration)
160    {
161        if (this->isDynamic())
162        {
163            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
164            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
165        }
166
167        this->angularAcceleration_ = acceleration;
168    }
169
170    bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
171    {
172        if (type == WorldEntity::Static)
173        {
174            ThrowException(PhysicsViolation, "Cannot tell a MobileEntity to have static collision type");
175            return false;
176        }
177        else
178            return true;
179    }
180
181    void MobileEntity::setWorldTransform(const btTransform& worldTrans)
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()));
185        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
186        this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x();
187        this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y();
188        this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z();
189        this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x();
190        this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y();
191        this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z();
192        linearVelocityChanged(true);
193        angularVelocityChanged(true);
194        positionChanged(true);
195        orientationChanged(true);
196    }
197
198    void MobileEntity::getWorldTransform(btTransform& worldTrans) const
199    {
200        // We use a kinematic body
201        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
202        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
203        if (this->isDynamic())
204        {
205            // This function gets called only once for dynamic objects to set the initial conditions
206            // We have to set the velocities too.
207            this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z));
208            this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z));
209        }
210    }
211}
Note: See TracBrowser for help on using the repository browser.