Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/MobileEntity.cc @ 10057

Last change on this file since 10057 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 9.0 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
[2408]29#include "MobileEntity.h"
[2072]30
[2426]31#include <OgreSceneNode.h>
[3196]32#include <BulletDynamics/Dynamics/btRigidBody.h>
[2303]33
[2072]34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
[8727]36
[5735]37#include "Scene.h"
[2292]38
[2072]39namespace orxonox
40{
[9667]41    RegisterClass(MobileEntity);
42
43    MobileEntity::MobileEntity(Context* context) : WorldEntity(context)
[2072]44    {
[2408]45        RegisterObject(MobileEntity);
[2072]46
[2374]47        this->linearAcceleration_  = Vector3::ZERO;
48        this->linearVelocity_      = Vector3::ZERO;
49        this->angularAcceleration_ = Vector3::ZERO;
50        this->angularVelocity_     = Vector3::ZERO;
[2072]51    }
52
[2408]53    MobileEntity::~MobileEntity()
[2072]54    {
55    }
56
[2408]57    void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[2072]58    {
[2408]59        SUPER(MobileEntity, XMLPort, xmlelement, mode);
[2300]60
[2408]61        XMLPortParamTemplate(MobileEntity, "velocity",     setVelocity,     getVelocity,     xmlelement, mode, const Vector3&);
[2491]62
63        Vector3 rotationAxis(this->getRotationAxis());
64        Degree rotationRate = this->getRotationRate();
65        XMLPortParamVariable(MobileEntity, "rotationaxis", rotationAxis, xmlelement, mode);
66        XMLPortParamVariable(MobileEntity, "rotationrate", rotationRate, xmlelement, mode);
67        if (mode == XMLPort::LoadObject)
68        {
69            if (rotationAxis == Vector3::ZERO)
70                this->setAngularVelocity(Vector3::ZERO);
71            else
72                this->setAngularVelocity(rotationAxis.normalisedCopy() * rotationRate.valueRadians());
73        }
[2072]74    }
75
[2408]76    void MobileEntity::tick(float dt)
[2374]77    {
78        if (this->isActive())
79        {
80            // Check whether Bullet doesn't do the physics for us
81            if (!this->isDynamic())
82            {
83                // Linear part
84                this->linearVelocity_.x += this->linearAcceleration_.x * dt;
85                this->linearVelocity_.y += this->linearAcceleration_.y * dt;
86                this->linearVelocity_.z += this->linearAcceleration_.z * dt;
87                this->node_->translate(this->linearVelocity_ * dt);
88
89                // Angular part
90                // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3
91                this->angularVelocity_.x += angularAcceleration_.x * dt;
92                this->angularVelocity_.y += angularAcceleration_.y * dt;
93                this->angularVelocity_.z += angularAcceleration_.z * dt;
94                // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method.
[3196]95                float mult = dt * 0.5f;
[2374]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            }
102        }
103    }
104
[2408]105    void MobileEntity::setPosition(const Vector3& position)
[2292]106    {
107        if (this->isDynamic())
108        {
109            btTransform transf = this->physicalBody_->getWorldTransform();
110            transf.setOrigin(btVector3(position.x, position.y, position.z));
111            this->physicalBody_->setWorldTransform(transf);
[2201]112        }
[2300]113
114        this->node_->setPosition(position);
[2201]115    }
116
[2408]117    void MobileEntity::setOrientation(const Quaternion& orientation)
[2292]118    {
119        if (this->isDynamic())
120        {
121            btTransform transf = this->physicalBody_->getWorldTransform();
[2374]122            transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w));
[2292]123            this->physicalBody_->setWorldTransform(transf);
[2201]124        }
[2300]125
126        this->node_->setOrientation(orientation);
[2201]127    }
128
[2408]129    void MobileEntity::setVelocity(const Vector3& velocity)
[2300]130    {
131        if (this->isDynamic())
[2374]132            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
133
134        this->linearVelocity_ = velocity;
135    }
136
[2408]137    void MobileEntity::setAngularVelocity(const Vector3& velocity)
[2374]138    {
139        if (this->isDynamic())
140            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
141
142        this->angularVelocity_ = velocity;
143    }
144
[2408]145    void MobileEntity::setAcceleration(const Vector3& acceleration)
[2374]146    {
147        if (this->isDynamic())
[8706]148        {
[2374]149            this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass()));
[8706]150        }
[2374]151
[8727]152        // If not bullet-managed (deprecated? SpaceShip doesn't use this anymore for movement) TODO: Find out!
[2374]153        this->linearAcceleration_ = acceleration;
154    }
155
[8727]156    /**
157    @brief
158        Adds the input acceleration at the input position to the MobileEntity.
159    @param acceleration
160        The acceleration to be additionally applied to the MobileEntity.
161    @param relativePosition
162        The position relative to the MobileEntity at which the acceleration is applied.
163    */
[8706]164    void MobileEntity::addAcceleration(const Vector3 &acceleration, const Vector3 &relativePosition)
165    {
166        if(this->isDynamic())
167        {
168            this->physicalBody_->applyForce(this->getMass() * btVector3(acceleration.x, acceleration.y, acceleration.z), btVector3(relativePosition.x, relativePosition.y, relativePosition.z));
169        }
170    }
171
[2408]172    void MobileEntity::setAngularAcceleration(const Vector3& acceleration)
[2374]173    {
174        if (this->isDynamic())
[2292]175        {
[2374]176            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
177            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
[2292]178        }
[2300]179
[2374]180        this->angularAcceleration_ = acceleration;
[2201]181    }
182
[3033]183    void MobileEntity::applyCentralForce(const Vector3& force)
184    {
185        if (this->isDynamic())
186            this->physicalBody_->applyCentralForce(btVector3(force.x * this->getMass(), force.y * this->getMass(), force.z * this->getMass()));
187    }
188
[2408]189    bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const
[2298]190    {
191        if (type == WorldEntity::Static)
192        {
[8858]193            orxout(internal_warning) << "Cannot tell a MobileEntity to have static collision type! Ignoring." << endl;
[2454]194            assert(false); // Only in debug mode
[2298]195            return false;
196        }
197        else
198            return true;
199    }
200
[2408]201    void MobileEntity::setWorldTransform(const btTransform& worldTrans)
[2292]202    {
203        // We use a dynamic body. So we translate our node accordingly.
204        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
[2780]205        btQuaternion temp(worldTrans.getRotation());
206        this->node_->setOrientation(Quaternion(temp.w(), temp.x(), temp.y(), temp.z()));
[2374]207        this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x();
208        this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y();
209        this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z();
210        this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x();
211        this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y();
212        this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z();
[2292]213    }
214
[2408]215    void MobileEntity::getWorldTransform(btTransform& worldTrans) const
[2292]216    {
[2374]217        // We use a kinematic body
[2292]218        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
[2374]219        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
[2300]220        if (this->isDynamic())
221        {
222            // This function gets called only once for dynamic objects to set the initial conditions
[2374]223            // We have to set the velocities too.
224            this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z));
225            this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z));
[2300]226        }
[2292]227    }
[2072]228}
Note: See TracBrowser for help on using the repository browser.