Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Clarified use of different physical bodies. The "collisionType" XML attribute of WE specifies the following:
"none": There is not physical body at all. Physics disabled.
"static": It is a StaticEntity with physics. Any other derived class of WE issues an exception by choosing this collision type.
"kinematic" or "dynamic": MovableEntity with physics. StaticEntity issues an exception when choosing one of these two.

Added two new Exceptions: ParseError and PhysicsViolation.

  • Property svn:eol-style set to native
File size: 12.8 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Martin Stypinski
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "MovableEntity.h"
31
32#include "util/Exception.h"
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35
36#include "objects/Scene.h"
37
38namespace orxonox
39{
40    MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)
41    {
42        RegisterObject(MovableEntity);
43
44        this->registerVariables();
45    }
46
47    MovableEntity::~MovableEntity()
48    {
49    }
50
51    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
52    {
53        SUPER(MovableEntity, XMLPort, xmlelement, mode);
54    }
55
56    void MovableEntity::registerVariables()
57    {
58    }
59
60    //void MovableEntity::setPosition(const Vector3& position)
61    //{
62    //    //if (isDynamic() && bAddedToPhysicalWorld_)
63    //    //{
64    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
65    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
66    //    //}
67    //    this->node_->setPosition(position);
68    //    positionChanged();
69    //}
70
71    //void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
72    //{
73    //    //if (isDynamic() && bAddedToPhysicalWorld_)
74    //    //{
75    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
76    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
77    //    //}
78    //    this->node_->translate(distance, relativeTo);
79    //    positionChanged();
80    //}
81
82    //void MovableEntity::setOrientation(const Quaternion& orientation)
83    //{
84    //    //if (isDynamic() && bAddedToPhysicalWorld_)
85    //    //{
86    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
87    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
88    //    //}
89    //    this->node_->setOrientation(orientation);
90    //    orientationChanged();
91    //}
92
93    //void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
94    //{
95    //    //if (isDynamic() && bAddedToPhysicalWorld_)
96    //    //{
97    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
98    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
99    //    //}
100    //    this->node_->rotate(rotation, relativeTo);
101    //    orientationChanged();
102    //}
103
104    //void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
105    //{
106    //    //if (isDynamic() && bAddedToPhysicalWorld_)
107    //    //{
108    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
109    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
110    //    //}
111    //    this->node_->yaw(angle, relativeTo);
112    //    orientationChanged();
113    //}
114
115    //void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
116    //{
117    //    //if (isDynamic() && bAddedToPhysicalWorld_)
118    //    //{
119    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
120    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
121    //    //}
122    //    this->node_->pitch(angle, relativeTo);
123    //    orientationChanged();
124    //}
125
126    //void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
127    //{
128    //    //if (isDynamic() && bAddedToPhysicalWorld_)
129    //    //{
130    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
131    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
132    //    //}
133    //    this->node_->roll(angle, relativeTo);
134    //    orientationChanged();
135    //}
136
137    //void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
138    //{
139    //    //if (isDynamic() && bAddedToPhysicalWorld_)
140    //    //{
141    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
142    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
143    //    //}
144    //    this->node_->lookAt(target, relativeTo, localDirectionVector);
145    //    orientationChanged();
146    //}
147
148    //void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
149    //{
150    //    //if (isDynamic() && bAddedToPhysicalWorld_)
151    //    //{
152    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
153    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
154    //    //}
155    //    this->node_->setDirection(direction, relativeTo, localDirectionVector);
156    //    orientationChanged();
157    //}
158
159    void MovableEntity::setPosition(const Vector3& position)
160    {
161        if (this->isDynamic())
162        {
163            btTransform transf = this->physicalBody_->getWorldTransform();
164            transf.setOrigin(btVector3(position.x, position.y, position.z));
165            this->physicalBody_->setWorldTransform(transf);
166        }
167        else
168        {
169            // no physics, we do it ourselves
170            this->node_->setPosition(position);
171        }
172    }
173
174    void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
175    {
176        if (this->isDynamic())
177        {
178            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \
179                                                          to any other space than TS_LOCAL.");
180            this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z));
181        }
182        else
183        {
184            // no physics, we do it ourselves
185            this->node_->translate(distance, relativeTo);
186        }
187    }
188
189    void MovableEntity::setOrientation(const Quaternion& orientation)
190    {
191        if (this->isDynamic())
192        {
193            btTransform transf = this->physicalBody_->getWorldTransform();
194            transf.setRotation(btQuaternion(orientation.w, orientation.x, orientation.y, orientation.z));
195            this->physicalBody_->setWorldTransform(transf);
196        }
197        else
198        {
199            // no physics, we do it ourselves
200            this->node_->setOrientation(orientation);
201        }
202    }
203
204    void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
205    {
206        if (this->isDynamic())
207        {
208            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \
209                                                          to any other space than TS_LOCAL.");
210            btTransform transf = this->physicalBody_->getWorldTransform();
211            this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.w, rotation.x, rotation.y, rotation.z)));
212        }
213        else
214        {
215            // no physics, we do it ourselves
216            this->node_->rotate(rotation, relativeTo);
217        }
218    }
219
220    void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
221    {
222        if (this->isDynamic())
223        {
224            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \
225                                                          to any other space than TS_LOCAL.");
226            btTransform transf = this->physicalBody_->getWorldTransform();
227            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
228            this->physicalBody_->setWorldTransform(transf * rotation);
229        }
230        else
231        {
232            // no physics, we do it ourselves
233            this->node_->yaw(angle, relativeTo);
234        }
235    }
236
237    void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
238    {
239        if (this->isDynamic())
240        {
241            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \
242                                                          to any other space than TS_LOCAL.");
243            btTransform transf = this->physicalBody_->getWorldTransform();
244            btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f));
245            this->physicalBody_->setWorldTransform(transf * rotation);
246        }
247        else
248        {
249            // no physics, we do it ourselves
250            this->node_->pitch(angle, relativeTo);
251        }
252    }
253
254    void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
255    {
256        if (this->isDynamic())
257        {
258            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \
259                                                          to any other space than TS_LOCAL.");
260            btTransform transf = this->physicalBody_->getWorldTransform();
261            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
262            this->physicalBody_->setWorldTransform(transf * rotation);
263        }
264        else
265        {
266            // no physics, we do it ourselves
267            this->node_->roll(angle, relativeTo);
268        }
269    }
270
271    void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
272    {
273        if (this->isDynamic())
274        {
275            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
276            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
277                                                          to any other space than TS_LOCAL.");
278            //btTransform transf = this->physicalBody_->getWorldTransform();
279            //this->physicalBody_->setWorldTransform(transf);
280        }
281        else
282        {
283            // no physics, we do it ourselves
284            this->node_->lookAt(target, relativeTo, localDirectionVector);
285        }
286    }
287
288    void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
289    {
290        if (this->isDynamic())
291        {
292            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
293            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
294                                                          to any other space than TS_LOCAL.");
295            //btTransform transf = this->physicalBody_->getWorldTransform();
296            //this->physicalBody_->setWorldTransform(transf);
297        }
298        else
299        {
300            // no physics, we do it ourselves
301            this->node_->setDirection(direction, relativeTo, localDirectionVector);
302        }
303    }
304
305    bool MovableEntity::isCollisionTypeLegal(WorldEntity::CollisionType type)
306    {
307        if (type == WorldEntity::Static)
308        {
309            ThrowException(PhysicsViolation, "Cannot tell a MovableEntity to have static collision type");
310            return false;
311        }
312        else
313            return true;
314    }
315
316    void MovableEntity::setWorldTransform(const btTransform& worldTrans)
317    {
318        // We use a dynamic body. So we translate our node accordingly.
319        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
320        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
321        const btVector3& velocity = this->physicalBody_->getLinearVelocity();
322        internalSetVelocity(Vector3(velocity.x(), velocity.y(), velocity.z()));
323        positionChanged();
324        orientationChanged();
325    }
326
327    void MovableEntity::getWorldTransform(btTransform& worldTrans) const
328    {
329        // We use a kinematic body
330        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
331        worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
332    }
333}
Note: See TracBrowser for help on using the repository browser.