Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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