Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2303 was 2303, checked in by rgrieder, 15 years ago
  • Added support for attaching physical WorldEntities to each other. Currently you can only add static objects to kinematic/dynamic/static other objects. Future plans involve attaching kinematic objects to static/kinematic ones. All other combinations don't make sense at all.
  • Added CollisionShape, SphereCollisionShape and CompoundCollisionShape

Usage for collision shapes (example):

<LinearEntity collisionType="kinematic">

<attached>

<Model position="0,0,0" scale=10 mesh="ast1.mesh" />
<StaticEntity position="0,10,0" collisionType="static"> # Everything else than "static" fails!

<collisionShapes>

<SphereCollisionShape radius=40 position="10,10,-10"/>
<CompoundCollisionShape position="4,4,4"> # You can also make compound shapes directly

<SphereCollisionShape radius=10/>

</CompoundCollisionShape>

</collisionShapes>

</StaticEntity>

</attached>

</LinearEntity>

  • Property svn:eol-style set to native
File size: 9.1 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 "BulletDynamics/Dynamics/btRigidBody.h"
33
34#include "util/Exception.h"
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37
38#include "objects/Scene.h"
39
40namespace orxonox
41{
42    MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)
43    {
44        RegisterObject(MovableEntity);
45
46        this->velocity_ = Vector3::ZERO;
47
48        this->registerVariables();
49    }
50
51    MovableEntity::~MovableEntity()
52    {
53    }
54
55    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
56    {
57        SUPER(MovableEntity, XMLPort, xmlelement, mode);
58
59        XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&);
60    }
61
62    void MovableEntity::registerVariables()
63    {
64    }
65
66    void MovableEntity::setPosition(const Vector3& position)
67    {
68        if (this->isDynamic())
69        {
70            btTransform transf = this->physicalBody_->getWorldTransform();
71            transf.setOrigin(btVector3(position.x, position.y, position.z));
72            this->physicalBody_->setWorldTransform(transf);
73        }
74
75        this->node_->setPosition(position);
76        positionChanged();
77    }
78
79    void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
80    {
81        if (this->isDynamic())
82        {
83            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \
84                                                          to any other space than TS_LOCAL.");
85            this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z));
86        }
87
88        this->node_->translate(distance, relativeTo);
89        positionChanged();
90    }
91
92    void MovableEntity::setOrientation(const Quaternion& orientation)
93    {
94        if (this->isDynamic())
95        {
96            btTransform transf = this->physicalBody_->getWorldTransform();
97            transf.setRotation(btQuaternion(orientation.w, orientation.x, orientation.y, orientation.z));
98            this->physicalBody_->setWorldTransform(transf);
99        }
100
101        this->node_->setOrientation(orientation);
102        orientationChanged();
103    }
104
105    void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
106    {
107        if (this->isDynamic())
108        {
109            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \
110                                                          to any other space than TS_LOCAL.");
111            btTransform transf = this->physicalBody_->getWorldTransform();
112            this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.w, rotation.x, rotation.y, rotation.z)));
113        }
114
115        this->node_->rotate(rotation, relativeTo);
116        orientationChanged();
117    }
118
119    void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
120    {
121        if (this->isDynamic())
122        {
123            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \
124                                                          to any other space than TS_LOCAL.");
125            btTransform transf = this->physicalBody_->getWorldTransform();
126            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
127            this->physicalBody_->setWorldTransform(transf * rotation);
128        }
129
130        this->node_->yaw(angle, relativeTo);
131        orientationChanged();
132    }
133
134    void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
135    {
136        if (this->isDynamic())
137        {
138            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \
139                                                          to any other space than TS_LOCAL.");
140            btTransform transf = this->physicalBody_->getWorldTransform();
141            btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f));
142            this->physicalBody_->setWorldTransform(transf * rotation);
143        }
144
145        this->node_->pitch(angle, relativeTo);
146        orientationChanged();
147    }
148
149    void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
150    {
151        if (this->isDynamic())
152        {
153            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \
154                                                          to any other space than TS_LOCAL.");
155            btTransform transf = this->physicalBody_->getWorldTransform();
156            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
157            this->physicalBody_->setWorldTransform(transf * rotation);
158        }
159
160        this->node_->roll(angle, relativeTo);
161        orientationChanged();
162    }
163
164    void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
165    {
166        if (this->isDynamic())
167        {
168            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
169            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
170                                                          to any other space than TS_LOCAL.");
171            //btTransform transf = this->physicalBody_->getWorldTransform();
172            //this->physicalBody_->setWorldTransform(transf);
173        }
174
175        this->node_->lookAt(target, relativeTo, localDirectionVector);
176        orientationChanged();
177    }
178
179    void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
180    {
181        if (this->isDynamic())
182        {
183            ThrowException(NotImplemented, "ControllableEntity::setDirection() is not yet supported for physical objects.");
184            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
185                                                          to any other space than TS_LOCAL.");
186            //btTransform transf = this->physicalBody_->getWorldTransform();
187            //this->physicalBody_->setWorldTransform(transf);
188        }
189
190        this->node_->setDirection(direction, relativeTo, localDirectionVector);
191        orientationChanged();
192    }
193
194    void MovableEntity::setVelocity(const Vector3& velocity)
195    {
196        if (this->isDynamic())
197        {
198            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
199        }
200
201        this->velocity_ = velocity;
202        velocityChanged();
203    }
204
205    bool MovableEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
206    {
207        if (type == WorldEntity::Static)
208        {
209            ThrowException(PhysicsViolation, "Cannot tell a MovableEntity to have static collision type");
210            return false;
211        }
212        else
213            return true;
214    }
215
216    void MovableEntity::setWorldTransform(const btTransform& worldTrans)
217    {
218        // We use a dynamic body. So we translate our node accordingly.
219        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
220        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
221        const btVector3& velocity = this->physicalBody_->getLinearVelocity();
222        this->velocity_ = Vector3(velocity.x(), velocity.y(), velocity.z());
223        velocityChanged();
224        positionChanged();
225        orientationChanged();
226    }
227
228    void MovableEntity::getWorldTransform(btTransform& worldTrans) const
229    {
230        // We use a kinematic body
231        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
232        worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
233        if (this->isDynamic())
234        {
235            // This function gets called only once for dynamic objects to set the initial conditions
236            // We have to set the velocity too.
237            this->physicalBody_->setLinearVelocity(btVector3(velocity_.x, velocity_.y, velocity_.z));
238        }
239    }
240}
Note: See TracBrowser for help on using the repository browser.