Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc @ 2374

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

Trying to synchronise phyiscs over the network.

  • Removed derivation of CollisionShape from WorldEntity (BaseObject instead).
  • Property svn:eol-style set to native
File size: 13.0 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 "MovableEntity.h"
31
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    MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)
45    {
46        RegisterObject(MovableEntity);
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    MovableEntity::~MovableEntity()
57    {
58    }
59
60    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
61    {
62        SUPER(MovableEntity, XMLPort, xmlelement, mode);
63
64        XMLPortParamTemplate(MovableEntity, "velocity",     setVelocity,     getVelocity,     xmlelement, mode, const Vector3&);
65        XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);
66        XMLPortParam(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode);
67    }
68
69    void MovableEntity::registerVariables()
70    {
71    }
72
73    void MovableEntity::tick(float dt)
74    {
75        if (this->isActive())
76        {
77            // Check whether Bullet doesn't do the physics for us
78            if (!this->isDynamic())
79            {
80                // Linear part
81                this->linearVelocity_.x += this->linearAcceleration_.x * dt;
82                this->linearVelocity_.y += this->linearAcceleration_.y * dt;
83                this->linearVelocity_.z += this->linearAcceleration_.z * dt;
84                linearVelocityChanged(true);
85                this->node_->translate(this->linearVelocity_ * dt);
86                positionChanged(true);
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                angularVelocityChanged(true);
94                // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method.
95                float mult = dt * 0.5;
96                // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions!
97                Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult);
98                newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation();
99                newOrientation.normalise();
100                this->node_->setOrientation(newOrientation);
101                orientationChanged(true);
102            }
103        }
104    }
105
106    void MovableEntity::setPosition(const Vector3& position)
107    {
108        if (this->isDynamic())
109        {
110            //if (this->isPhysicsRunning())
111            //    return;
112            btTransform transf = this->physicalBody_->getWorldTransform();
113            transf.setOrigin(btVector3(position.x, position.y, position.z));
114            this->physicalBody_->setWorldTransform(transf);
115        }
116
117        //COUT(0) << "setting position: " << position << std::endl;
118        this->node_->setPosition(position);
119        positionChanged(false);
120    }
121
122    void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
123    {
124        if (this->isDynamic())
125        {
126            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \
127                                                          to any other space than TS_LOCAL.");
128            this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z));
129        }
130
131        this->node_->translate(distance, relativeTo);
132        positionChanged(false);
133    }
134
135    void MovableEntity::setOrientation(const Quaternion& orientation)
136    {
137        if (this->isDynamic())
138        {
139            btTransform transf = this->physicalBody_->getWorldTransform();
140            transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w));
141            this->physicalBody_->setWorldTransform(transf);
142        }
143
144        this->node_->setOrientation(orientation);
145        orientationChanged(false);
146    }
147
148    void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
149    {
150        if (this->isDynamic())
151        {
152            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \
153                                                          to any other space than TS_LOCAL.");
154            btTransform transf = this->physicalBody_->getWorldTransform();
155            this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w)));
156        }
157
158        this->node_->rotate(rotation, relativeTo);
159        orientationChanged(false);
160    }
161
162    void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
163    {
164        if (this->isDynamic())
165        {
166            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \
167                                                          to any other space than TS_LOCAL.");
168            btTransform transf = this->physicalBody_->getWorldTransform();
169            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
170            this->physicalBody_->setWorldTransform(transf * rotation);
171        }
172
173        this->node_->yaw(angle, relativeTo);
174        orientationChanged(false);
175    }
176
177    void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
178    {
179        if (this->isDynamic())
180        {
181            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \
182                                                          to any other space than TS_LOCAL.");
183            btTransform transf = this->physicalBody_->getWorldTransform();
184            btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f));
185            this->physicalBody_->setWorldTransform(transf * rotation);
186        }
187
188        this->node_->pitch(angle, relativeTo);
189        orientationChanged(false);
190    }
191
192    void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
193    {
194        if (this->isDynamic())
195        {
196            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \
197                                                          to any other space than TS_LOCAL.");
198            btTransform transf = this->physicalBody_->getWorldTransform();
199            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
200            this->physicalBody_->setWorldTransform(transf * rotation);
201        }
202
203        this->node_->roll(angle, relativeTo);
204        orientationChanged(false);
205    }
206
207    void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
208    {
209        if (this->isDynamic())
210        {
211            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
212            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
213                                                          to any other space than TS_LOCAL.");
214            //btTransform transf = this->physicalBody_->getWorldTransform();
215            //this->physicalBody_->setWorldTransform(transf);
216        }
217
218        this->node_->lookAt(target, relativeTo, localDirectionVector);
219        orientationChanged(false);
220    }
221
222    void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
223    {
224        if (this->isDynamic())
225        {
226            ThrowException(NotImplemented, "ControllableEntity::setDirection() is not yet supported for physical objects.");
227            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
228                                                          to any other space than TS_LOCAL.");
229            //btTransform transf = this->physicalBody_->getWorldTransform();
230            //this->physicalBody_->setWorldTransform(transf);
231        }
232
233        this->node_->setDirection(direction, relativeTo, localDirectionVector);
234        orientationChanged(false);
235    }
236
237    void MovableEntity::setVelocity(const Vector3& velocity)
238    {
239        if (this->isDynamic())
240            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
241
242        this->linearVelocity_ = velocity;
243        linearVelocityChanged(false);
244    }
245
246    void MovableEntity::setAngularVelocity(const Vector3& velocity)
247    {
248        if (this->isDynamic())
249            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
250
251        this->angularVelocity_ = velocity;
252        angularVelocityChanged(false);
253    }
254
255    void MovableEntity::setAcceleration(const Vector3& acceleration)
256    {
257        if (this->isDynamic())
258            this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass()));
259
260        this->linearAcceleration_ = acceleration;
261    }
262
263    void MovableEntity::setAngularAcceleration(const Vector3& acceleration)
264    {
265        if (this->isDynamic())
266        {
267            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
268            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
269        }
270
271        this->angularAcceleration_ = acceleration;
272    }
273
274    bool MovableEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
275    {
276        if (type == WorldEntity::Static)
277        {
278            ThrowException(PhysicsViolation, "Cannot tell a MovableEntity to have static collision type");
279            return false;
280        }
281        else
282            return true;
283    }
284
285    void MovableEntity::setWorldTransform(const btTransform& worldTrans)
286    {
287        // We use a dynamic body. So we translate our node accordingly.
288        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
289        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
290        COUT(0) << "setting world transform: " << omni_cast<std::string>(node_->getPosition()) << std::endl;
291        this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x();
292        this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y();
293        this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z();
294        this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x();
295        this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y();
296        this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z();
297        linearVelocityChanged(true);
298        angularVelocityChanged(true);
299        positionChanged(true);
300        orientationChanged(true);
301    }
302
303    void MovableEntity::getWorldTransform(btTransform& worldTrans) const
304    {
305        // We use a kinematic body
306        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
307        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
308        if (this->isDynamic())
309        {
310            // This function gets called only once for dynamic objects to set the initial conditions
311            // We have to set the velocities too.
312            this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z));
313            this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z));
314        }
315    }
316}
Note: See TracBrowser for help on using the repository browser.