Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/orxonox/worldentities/MobileEntity.cc @ 12250

Last change on this file since 12250 was 12028, checked in by merholzl, 6 years ago

added scriptable controller

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