Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/MobileEntity.cc @ 2469

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

Resolved the issue with the collision shape synchronisation.

  • Property svn:eol-style set to native
File size: 7.8 KB
RevLine 
[2072]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:
[2304]23 *      Reto Grieder
[2072]24 *   Co-authors:
[2201]25 *      Martin Stypinski
[2072]26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
[2408]30#include "MobileEntity.h"
[2072]31
[2426]32#include <OgreSceneNode.h>
[2303]33#include "BulletDynamics/Dynamics/btRigidBody.h"
34
[2306]35#include "util/Debug.h"
[2374]36#include "util/MathConvert.h"
[2292]37#include "util/Exception.h"
[2072]38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40
[2292]41#include "objects/Scene.h"
42
[2072]43namespace orxonox
44{
[2408]45    MobileEntity::MobileEntity(BaseObject* creator) : WorldEntity(creator)
[2072]46    {
[2408]47        RegisterObject(MobileEntity);
[2072]48
[2374]49        this->linearAcceleration_  = Vector3::ZERO;
50        this->linearVelocity_      = Vector3::ZERO;
51        this->angularAcceleration_ = Vector3::ZERO;
52        this->angularVelocity_     = Vector3::ZERO;
[2469]53
54        this->registerVariables();
[2072]55    }
56
[2408]57    MobileEntity::~MobileEntity()
[2072]58    {
59    }
60
[2408]61    void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[2072]62    {
[2408]63        SUPER(MobileEntity, XMLPort, xmlelement, mode);
[2300]64
[2408]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);
[2072]68    }
69
[2408]70    void MobileEntity::tick(float dt)
[2374]71    {
72        if (this->isActive())
73        {
74            // Check whether Bullet doesn't do the physics for us
75            if (!this->isDynamic())
76            {
77                // Linear part
78                this->linearVelocity_.x += this->linearAcceleration_.x * dt;
79                this->linearVelocity_.y += this->linearAcceleration_.y * dt;
80                this->linearVelocity_.z += this->linearAcceleration_.z * dt;
81                this->node_->translate(this->linearVelocity_ * dt);
82
83                // Angular part
84                // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3
85                this->angularVelocity_.x += angularAcceleration_.x * dt;
86                this->angularVelocity_.y += angularAcceleration_.y * dt;
87                this->angularVelocity_.z += angularAcceleration_.z * dt;
88                // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method.
89                float mult = dt * 0.5;
90                // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions!
91                Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult);
92                newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation();
93                newOrientation.normalise();
94                this->node_->setOrientation(newOrientation);
95            }
96        }
97    }
98
[2408]99    void MobileEntity::setPosition(const Vector3& position)
[2292]100    {
101        if (this->isDynamic())
102        {
103            btTransform transf = this->physicalBody_->getWorldTransform();
104            transf.setOrigin(btVector3(position.x, position.y, position.z));
105            this->physicalBody_->setWorldTransform(transf);
[2201]106        }
[2300]107
108        this->node_->setPosition(position);
[2201]109    }
110
[2408]111    void MobileEntity::setOrientation(const Quaternion& orientation)
[2292]112    {
113        if (this->isDynamic())
114        {
115            btTransform transf = this->physicalBody_->getWorldTransform();
[2374]116            transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w));
[2292]117            this->physicalBody_->setWorldTransform(transf);
[2201]118        }
[2300]119
120        this->node_->setOrientation(orientation);
[2201]121    }
122
[2408]123    void MobileEntity::setVelocity(const Vector3& velocity)
[2300]124    {
125        if (this->isDynamic())
[2374]126            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
127
128        this->linearVelocity_ = velocity;
129    }
130
[2408]131    void MobileEntity::setAngularVelocity(const Vector3& velocity)
[2374]132    {
133        if (this->isDynamic())
134            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
135
136        this->angularVelocity_ = velocity;
137    }
138
[2408]139    void MobileEntity::setAcceleration(const Vector3& acceleration)
[2374]140    {
141        if (this->isDynamic())
142            this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass()));
143
144        this->linearAcceleration_ = acceleration;
145    }
146
[2408]147    void MobileEntity::setAngularAcceleration(const Vector3& acceleration)
[2374]148    {
149        if (this->isDynamic())
[2292]150        {
[2374]151            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
152            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
[2292]153        }
[2300]154
[2374]155        this->angularAcceleration_ = acceleration;
[2201]156    }
157
[2408]158    bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
[2298]159    {
160        if (type == WorldEntity::Static)
161        {
[2454]162            CCOUT(1) << "Error: Cannot tell a MobileEntity to have static collision type! Ignoring." << std::endl;
163            assert(false); // Only in debug mode
[2298]164            return false;
165        }
166        else
167            return true;
168    }
169
[2408]170    void MobileEntity::setWorldTransform(const btTransform& worldTrans)
[2292]171    {
172        // We use a dynamic body. So we translate our node accordingly.
173        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
174        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
[2374]175        this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x();
176        this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y();
177        this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z();
178        this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x();
179        this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y();
180        this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z();
[2292]181    }
182
[2408]183    void MobileEntity::getWorldTransform(btTransform& worldTrans) const
[2292]184    {
[2374]185        // We use a kinematic body
[2292]186        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
[2374]187        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
[2300]188        if (this->isDynamic())
189        {
190            // This function gets called only once for dynamic objects to set the initial conditions
[2374]191            // We have to set the velocities too.
192            this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z));
193            this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z));
[2300]194        }
[2292]195    }
[2072]196}
Note: See TracBrowser for help on using the repository browser.