Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/WorldEntity.cc @ 11963

Last change on this file since 11963 was 11083, checked in by muemart, 9 years ago

Fix some clang-tidy warnings.
Also, Serialise.h was doing some C-style casts that ended up being const casts. I moved those const casts as close to the source as possible and changed the loadAndIncrease functions to not do that.

  • Property svn:eol-style set to native
File size: 39.2 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:
23 *      Fabian 'x3n' Landau
[2662]24 *      Reto Grieder (physics)
[2072]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "WorldEntity.h"
31
[3196]32#include <OgreBillboardSet.h>
33#include <OgreCamera.h>
34#include <OgreEntity.h>
35#include <OgreParticleSystem.h>
36#include <OgreSceneManager.h>
[2662]37#include <OgreSceneNode.h>
[3196]38#include <BulletDynamics/Dynamics/btRigidBody.h>
[2072]39
[3196]40#include "util/OrxAssert.h"
41#include "util/Convert.h"
[2662]42#include "util/Exception.h"
[2072]43#include "core/CoreIncludes.h"
44#include "core/XMLPort.h"
[5735]45#include "Scene.h"
46#include "collisionshapes/WorldEntityCollisionShape.h"
[2072]47
48namespace orxonox
49{
50    const Vector3 WorldEntity::FRONT = Vector3::NEGATIVE_UNIT_Z;
51    const Vector3 WorldEntity::BACK  = Vector3::UNIT_Z;
52    const Vector3 WorldEntity::LEFT  = Vector3::NEGATIVE_UNIT_X;
53    const Vector3 WorldEntity::RIGHT = Vector3::UNIT_X;
54    const Vector3 WorldEntity::DOWN  = Vector3::NEGATIVE_UNIT_Y;
55    const Vector3 WorldEntity::UP    = Vector3::UNIT_Y;
56
[3196]57    // Be sure we don't do bad conversions
[11071]58    static_assert((int)Ogre::Node::TS_LOCAL  == (int)WorldEntity::TransformSpace::Local,  "check enum");
59    static_assert((int)Ogre::Node::TS_PARENT == (int)WorldEntity::TransformSpace::Parent, "check enum");
60    static_assert((int)Ogre::Node::TS_WORLD  == (int)WorldEntity::TransformSpace::World,  "check enum");
[3196]61
[10624]62    RegisterAbstractClass(WorldEntity).inheritsFrom<BaseObject>().inheritsFrom<Synchronisable>();
[9667]63
[2662]64    /**
65    @brief
66        Creates a new WorldEntity that may immediately be used.
67        All the default values are being set here.
68    */
[9667]69    WorldEntity::WorldEntity(Context* context) : BaseObject(context), Synchronisable(context)
[2072]70    {
71        RegisterObject(WorldEntity);
72
[2171]73        if (!this->getScene() || !this->getScene()->getRootSceneNode())
74            ThrowException(AbortLoading, "Can't create WorldEntity, no scene or no root-scenenode given.");
[2072]75
76        this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
77
[11071]78        this->parent_ = nullptr;
[2171]79        this->parentID_ = OBJECTID_UNKNOWN;
[3077]80        this->bDeleteWithParent_ = true;
[2072]81
82        this->node_->setPosition(Vector3::ZERO);
83        this->node_->setOrientation(Quaternion::IDENTITY);
[7163]84
[6524]85        // Activity and visibility memory.
86        this->bActiveMem_ = true;
87        this->bVisibleMem_ = true;
[2072]88
[2662]89
90        // Default behaviour does not include physics
[11071]91        this->physicalBody_   = nullptr;
[2662]92        this->bPhysicsActive_ = false;
93        this->bPhysicsActiveSynchronised_    = false;
94        this->bPhysicsActiveBeforeAttaching_ = false;
[9667]95        this->collisionShape_ = new WorldEntityCollisionShape(this->getContext());
96        this->collisionShape_->setWorldEntityOwner(this);
[11071]97        this->collisionType_             = CollisionType::None;
98        this->collisionTypeSynchronised_ = CollisionType::None;
[10288]99        this->mass_                 = 1.0f;
100        this->childrenMass_         = 0;
[2662]101        // Using bullet default values
[10288]102        this->restitution_          = 0;
103        this->angularFactor_        = 1;
104        this->linearDamping_        = 0;
105        this->angularDamping_       = 0;
106        this->friction_             = 0.5;
107        this->ccdMotionThreshold_   = 0.0;
108        this->ccdSweptSphereRadius_ = 0.0;
[2662]109        this->bCollisionCallbackActive_ = false;
110        this->bCollisionResponseActive_ = true;
111
[2072]112        this->registerVariables();
113    }
114
[2662]115    /**
116    @brief
117        Destroys the WorldEntity AND ALL its children with it.
118    */
[2072]119    WorldEntity::~WorldEntity()
120    {
121        if (this->isInitialized())
122        {
[2662]123            if (this->parent_)
124                this->detachFromParent();
125
[7910]126            std::set<WorldEntity*>::iterator it;
127            while ((it = this->children_.begin()) != this->children_.end())
[3077]128            {
[7937]129                WorldEntity* entity = *it;
130
[10624]131                // do this for all children, because even if getDeleteWithParent() returns true a child might still stay active due to strong pointers pointing to it
[7937]132                entity->setPosition(entity->getWorldPosition());
133                this->detach(entity); // detach also erases the element from the children set
[7910]134
[7937]135                if (entity->getDeleteWithParent())
136                    entity->destroy();
[3077]137            }
[2662]138
139            if (this->physicalBody_)
140            {
141                this->deactivatePhysics();
142                delete this->physicalBody_;
143            }
[5929]144            this->collisionShape_->destroy();
[2662]145
[2072]146            this->node_->detachAllObjects();
[2662]147            this->node_->removeAllChildren();
148
149            OrxAssert(this->getScene()->getSceneManager(), "No SceneManager defined in a WorldEntity.");
150            this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName());
[2072]151        }
152    }
153
154    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
155    {
156        SUPER(WorldEntity, XMLPort, xmlelement, mode);
157
[2662]158        XMLPortParamTemplate(WorldEntity, "position",    setPosition,    getPosition,    xmlelement, mode, const Vector3&);
[2072]159        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
[2662]160        XMLPortParamTemplate(WorldEntity, "scale3D",     setScale3D,     getScale3D,     xmlelement, mode, const Vector3&);
161        XMLPortParam        (WorldEntity, "scale",       setScale,       getScale,       xmlelement, mode);
162        XMLPortParamLoadOnly(WorldEntity, "lookat",      lookAt_xmlport,       xmlelement, mode);
163        XMLPortParamLoadOnly(WorldEntity, "direction",   setDirection_xmlport, xmlelement, mode);
164        XMLPortParamLoadOnly(WorldEntity, "yaw",         yaw_xmlport,          xmlelement, mode);
165        XMLPortParamLoadOnly(WorldEntity, "pitch",       pitch_xmlport,        xmlelement, mode);
166        XMLPortParamLoadOnly(WorldEntity, "roll",        roll_xmlport,         xmlelement, mode);
[3077]167        XMLPortParam        (WorldEntity, "deletewithparent", setDeleteWithParent, getDeleteWithParent, xmlelement, mode);
[2072]168
[2662]169        // Physics
170        XMLPortParam(WorldEntity, "collisionType",     setCollisionTypeStr,  getCollisionTypeStr,  xmlelement, mode);
171        XMLPortParam(WorldEntity, "collisionResponse", setCollisionResponse, hasCollisionResponse, xmlelement, mode);
172        XMLPortParam(WorldEntity, "mass",              setMass,              getMass,              xmlelement, mode);
173        XMLPortParam(WorldEntity, "restitution",       setRestitution,       getRestitution,       xmlelement, mode);
174        XMLPortParam(WorldEntity, "angularFactor",     setAngularFactor,     getAngularFactor,     xmlelement, mode);
175        XMLPortParam(WorldEntity, "linearDamping",     setLinearDamping,     getLinearDamping,     xmlelement, mode);
176        XMLPortParam(WorldEntity, "angularDamping",    setAngularDamping,    getAngularDamping,    xmlelement, mode);
177        XMLPortParam(WorldEntity, "friction",          setFriction,          getFriction,          xmlelement, mode);
178
179        // Other attached WorldEntities
[2072]180        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
[2662]181        // Attached collision shapes
182        XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode);
[2072]183    }
184
185    void WorldEntity::registerVariables()
186    {
[5929]187        registerVariable(this->mainStateName_,  VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedMainStateName));
[2072]188
[3280]189        registerVariable(this->bActive_,        VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
190        registerVariable(this->bVisible_,       VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
[2072]191
[11083]192        // Ugly const cast, but is valid because the scale is not actually const
193        registerVariable(const_cast<Vector3&>(this->getScale3D()),    VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged));
[2662]194
195        // Physics stuff
[3280]196        registerVariable(this->mass_,           VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged));
197        registerVariable(this->restitution_,    VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::restitutionChanged));
198        registerVariable(this->angularFactor_,  VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::angularFactorChanged));
199        registerVariable(this->linearDamping_,  VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::linearDampingChanged));
200        registerVariable(this->angularDamping_, VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::angularDampingChanged));
201        registerVariable(this->friction_,       VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::frictionChanged));
[10288]202        registerVariable(this->ccdMotionThreshold_,
203                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::ccdMotionThresholdChanged));
204        registerVariable(this->ccdSweptSphereRadius_,
205                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::ccdSweptSphereRadiusChanged));
[2662]206        registerVariable(this->bCollisionCallbackActive_,
[3280]207                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionCallbackActivityChanged));
[2662]208        registerVariable(this->bCollisionResponseActive_,
[3280]209                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionResponseActivityChanged));
[11071]210        registerVariable(this->collisionTypeSynchronised_,
[3280]211                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged));
[2662]212        registerVariable(this->bPhysicsActiveSynchronised_,
[3280]213                                                VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::physicsActivityChanged));
[2662]214
215        // Attach to parent if necessary
[3280]216        registerVariable(this->parentID_,       VariableDirection::ToClient, new NetworkCallback<WorldEntity>(this, &WorldEntity::networkcallback_parentChanged));
[2072]217    }
[7163]218
[6524]219    /**
220    @brief
221        When the activity is changed, it is changed for all attached objects as well.
222    */
223    void WorldEntity::changedActivity(void)
224    {
225        SUPER(WorldEntity, changedActivity);
[7163]226
[7493]227        if(GameMode::isMaster())
[6524]228        {
[10274]229            // physics is only enabled if the WorldEntity is active
230            if (this->isActive())
231                this->activatePhysics();
232            else
233                this->deactivatePhysics();
234
235            // iterate over all children and change their activity as well
[11071]236            for (WorldEntity* object : this->getAttachedObjects())
[6524]237            {
[7493]238                if(!this->isActive())
239                {
[11071]240                    object->bActiveMem_ = object->isActive();
241                    object->setActive(this->isActive());
[7493]242                }
243                else
244                {
[11071]245                    object->setActive(object->bActiveMem_);
[7493]246                }
[6524]247            }
248        }
249    }
[7163]250
[6524]251    /**
252    @brief
253        When the visibility is changed, it is changed for all attached objects as well.
254    */
255    void WorldEntity::changedVisibility(void)
256    {
257        SUPER(WorldEntity, changedVisibility);
[7163]258
[7493]259        if(GameMode::isMaster())
[6524]260        {
[10274]261            // iterate over all children and change their visibility as well
[11071]262            for (WorldEntity* object : this->getAttachedObjects())
[6524]263            {
[7493]264                if(!this->isVisible())
265                {
[11071]266                    object->bVisibleMem_ = object->isVisible();
267                    object->setVisible(this->isVisible());
[7493]268                }
269                else
270                {
[11071]271                    object->setVisible(object->bVisibleMem_);
[7493]272                }
[6524]273            }
274        }
275    }
[2072]276
[2662]277    /**
278    @brief
279        Network function that object this instance to its correct parent.
280    */
[2851]281    void WorldEntity::networkcallback_parentChanged()
[2072]282    {
[2171]283        if (this->parentID_ != OBJECTID_UNKNOWN)
284        {
[3325]285            WorldEntity* parent = orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
[2171]286            if (parent)
287                this->attachToParent(parent);
288        }
[2072]289    }
290
[2662]291    /**
292    @brief
293        Attaches this object to a parent SceneNode.
[7401]294    @remarks
[2662]295        Only use this method if you know exactly what you're doing!
296        Normally, attaching works internally by attaching WE's.
297    */
298    void WorldEntity::attachToNode(Ogre::SceneNode* node)
[2072]299    {
[2662]300        Ogre::Node* parent = this->node_->getParent();
301        if (parent)
302            parent->removeChild(this->node_);
303        node->addChild(this->node_);
304    }
305
306    /**
307    @brief
308        Detaches this object from a parent SceneNode.
[7401]309    @remarks
[2662]310        Only use this method if you know exactly what you're doing!
311        Normally, attaching works internally by attaching WE's.
312    */
313    void WorldEntity::detachFromNode(Ogre::SceneNode* node)
314    {
315        node->removeChild(this->node_);
316//        this->getScene()->getRootSceneNode()->addChild(this->node_);
317    }
318
319    /**
320    @brief
321        Network callback for the collision type. Only change the type if it was valid.
322    */
323    void WorldEntity::collisionTypeChanged()
324    {
[11071]325        if (this->collisionTypeSynchronised_ != CollisionType::Dynamic &&
326            this->collisionTypeSynchronised_ != CollisionType::Kinematic &&
327            this->collisionTypeSynchronised_ != CollisionType::Static &&
328            this->collisionTypeSynchronised_ != CollisionType::None)
[2662]329        {
[8858]330            orxout(internal_error) << "Error when collsion Type was received over network. Unknown enum value:" << this->collisionTypeSynchronised_ << endl;
[2662]331        }
332        else if (this->collisionTypeSynchronised_ != collisionType_)
333        {
334            if (this->parent_)
[8858]335                orxout(internal_warning) << "Network connection tried to set the collision type of an attached WE. Ignoring." << endl;
[2662]336            else
337                this->setCollisionType(this->collisionTypeSynchronised_);
338        }
339    }
340
341    //! Network callback for this->bPhysicsActive_
342    void WorldEntity::physicsActivityChanged()
343    {
344        if (this->bPhysicsActiveSynchronised_)
345            this->activatePhysics();
[2072]346        else
[2662]347            this->deactivatePhysics();
348    }
349
350    //! Function sets whether Bullet should issue a callback on collisions
351    void WorldEntity::collisionCallbackActivityChanged()
352    {
353        if (this->hasPhysics())
[2072]354        {
[2662]355            if (this->bCollisionCallbackActive_)
356                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() |
357                    btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
358            else
359                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() &
360                    ~btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
[2072]361        }
[2662]362    }
[2072]363
[2662]364    //! Function sets whether Bullet should react itself to a collision
365    void WorldEntity::collisionResponseActivityChanged()
366    {
367        if (this->hasPhysics())
368        {
369            if (this->bCollisionResponseActive_)
370                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() &
371                    ~btCollisionObject::CF_NO_CONTACT_RESPONSE);
372            else
373                this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() |
374                    btCollisionObject::CF_NO_CONTACT_RESPONSE);
375        }
376    }
377
378    /**
379    @brief
380        Attaches a child WorldEntity to this object. This calls notifyBeingAttached()
381        of the child WE.
[7401]382    @note
[2662]383        The collision shape of the child object gets attached nevertheless. That also means
384        that you can change the collision shape of the child and it correctly cascadeds the changes to this instance.
385        Be aware of this implication: When implementing attaching of kinematic objects to others, you have to change
386        this behaviour because you then might not want to merge the collision shapes.
387    */
388    void WorldEntity::attach(WorldEntity* object)
389    {
390        if (object == this)
391        {
[8858]392            orxout(internal_warning) << "Can't attach a WorldEntity to itself." << endl;
[2662]393            return;
394        }
395
396        if (!object->notifyBeingAttached(this))
397            return;
398
399        this->attachNode(object->node_);
[2072]400        this->children_.insert(object);
[2662]401
402        this->attachCollisionShape(object->collisionShape_);
403        // mass
404        this->childrenMass_ += object->getMass();
405        recalculateMassProps();
[2072]406    }
407
[2662]408    /**
409    @brief
410        Function gets called when this object is being attached to a new parent.
411
412        This operation is only allowed if the collision types "like" each other.
413        - You cannot a attach a non physical object to a physical one.
414        - Dynamic object can NOT be attached at all.
415        - It is also not possible to attach a kinematic to a dynamic one.
416        - Attaching of kinematic objects otherwise is not yet supported.
417    */
418    bool WorldEntity::notifyBeingAttached(WorldEntity* newParent)
419    {
420        // check first whether attaching is even allowed
421        if (this->hasPhysics())
422        {
423            if (!newParent->hasPhysics())
424            {
[8858]425                orxout(internal_warning) << " Cannot attach a physical object to a non physical one." << endl;
[2662]426                return false;
427            }
428            else if (this->isDynamic())
429            {
[8858]430                orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
[2662]431                return false;
432            }
433            else if (this->isKinematic() && newParent->isDynamic())
434            {
[8858]435                orxout(internal_warning) << "Cannot attach a kinematic object to a dynamic one." << endl;
[2662]436                return false;
437            }
438            else if (this->isKinematic())
439            {
[8858]440                orxout(internal_warning) << "Cannot attach a kinematic object to a static or kinematic one: Not yet implemented." << endl;
[2662]441                return false;
442            }
443        }
444
445        if (this->isPhysicsActive())
446            this->bPhysicsActiveBeforeAttaching_ = true;
447        this->deactivatePhysics();
448
449        if (this->parent_)
450            this->detachFromParent();
451
452        this->parent_ = newParent;
453        this->parentID_ = newParent->getObjectID();
454
[2851]455        this->parentChanged();
456
[2662]457        // apply transform to collision shape
458        this->collisionShape_->setPosition(this->getPosition());
459        this->collisionShape_->setOrientation(this->getOrientation());
460        // TODO: Scale
[2851]461
[2662]462        return true;
463    }
464
465    /**
466    @brief
467        Detaches a child WorldEntity from this instance.
468    */
[2072]469    void WorldEntity::detach(WorldEntity* object)
470    {
[7910]471        std::set<WorldEntity*>::iterator it = this->children_.find(object);
472        if (it == this->children_.end())
[2662]473        {
[8858]474            orxout(internal_warning) << "Cannot detach an object that is not a child." << endl;
[2662]475            return;
476        }
477
478        // collision shapes
479        this->detachCollisionShape(object->collisionShape_);
480
481        // mass
482        if (object->getMass() > 0.0f)
483        {
484            this->childrenMass_ -= object->getMass();
485            recalculateMassProps();
486        }
487
488        this->detachNode(object->node_);
[7910]489        this->children_.erase(it);
[2072]490
[2662]491        object->notifyDetached();
[2072]492    }
493
[2662]494    /**
495    @brief
496        Function gets called when the object has been detached from its parent.
497    */
498    void WorldEntity::notifyDetached()
[2072]499    {
[11071]500        this->parent_ = nullptr;
[2662]501        this->parentID_ = OBJECTID_UNKNOWN;
502
[2851]503        this->parentChanged();
504
[2662]505        // reset orientation of the collisionShape (cannot be set within a WE usually)
506        this->collisionShape_->setPosition(Vector3::ZERO);
507        this->collisionShape_->setOrientation(Quaternion::IDENTITY);
508        // TODO: Scale
509
510        if (this->bPhysicsActiveBeforeAttaching_)
511        {
512            this->activatePhysics();
513            this->bPhysicsActiveBeforeAttaching_ = false;
514        }
515    }
516
517    //! Returns an attached object (merely for XMLPort).
518    WorldEntity* WorldEntity::getAttachedObject(unsigned int index)
519    {
[2072]520        unsigned int i = 0;
[11071]521        for (WorldEntity* child : this->children_)
[2072]522        {
523            if (i == index)
[11071]524                return child;
[2072]525            ++i;
526        }
[11071]527        return nullptr;
[2072]528    }
[2662]529
530    //! Attaches an Ogre::SceneNode to this WorldEntity.
531    void WorldEntity::attachNode(Ogre::SceneNode* node)
532    {
533        Ogre::Node* parent = node->getParent();
534        if (parent)
535            parent->removeChild(node);
536        this->node_->addChild(node);
537    }
538
539    //! Detaches an Ogre::SceneNode from this WorldEntity.
540    void WorldEntity::detachNode(Ogre::SceneNode* node)
541    {
542        this->node_->removeChild(node);
543//        this->getScene()->getRootSceneNode()->addChild(node);
544    }
545
546    //! Attaches an Ogre::MovableObject to this WorldEntity.
547    void WorldEntity::attachOgreObject(Ogre::MovableObject* object)
[6417]548    {
549        this->node_->attachObject(object);
[6501]550        object->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(this)));
[6417]551    }
552
[3196]553    void WorldEntity::attachOgreObject(Ogre::BillboardSet* object)
[6417]554        { this->attachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]555    void WorldEntity::attachOgreObject(Ogre::Camera* object)
[6417]556        { this->attachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]557    void WorldEntity::attachOgreObject(Ogre::Entity* object)
[6417]558        { this->attachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]559    void WorldEntity::attachOgreObject(Ogre::ParticleSystem* object)
[6417]560        { this->attachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[2662]561
562    //! Detaches an Ogre::MovableObject from this WorldEntity.
563    void WorldEntity::detachOgreObject(Ogre::MovableObject* object)
[6417]564    {
[11071]565        object->setUserAny(Ogre::Any(static_cast<OrxonoxClass*>(nullptr)));
[6417]566        this->node_->detachObject(object);
567    }
568
[3196]569    void WorldEntity::detachOgreObject(Ogre::BillboardSet* object)
[6417]570        { this->detachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]571    void WorldEntity::detachOgreObject(Ogre::Camera* object)
[6417]572        { this->detachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]573    void WorldEntity::detachOgreObject(Ogre::Entity* object)
[6417]574        { this->detachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[3196]575    void WorldEntity::detachOgreObject(Ogre::ParticleSystem* object)
[6417]576        { this->detachOgreObject(static_cast<Ogre::MovableObject*>(object)); }
[2662]577
578    //! Detaches an Ogre::MovableObject (by string) from this WorldEntity.
579    Ogre::MovableObject* WorldEntity::detachOgreObject(const Ogre::String& name)
580    {
581        return this->node_->detachObject(name);
582    }
583
584    //! Attaches a collision Shape to this object (delegated to the internal CompoundCollisionShape)
585    void WorldEntity::attachCollisionShape(CollisionShape* shape)
586    {
587        this->collisionShape_->attach(shape);
588        // Note: this->collisionShape_ already notifies us of any changes.
589    }
590
591    //! Detaches a collision Shape from this object (delegated to the internal CompoundCollisionShape)
592    void WorldEntity::detachCollisionShape(CollisionShape* shape)
593    {
594        // Note: The collision shapes may not be detached with this function!
595        this->collisionShape_->detach(shape);
596        // Note: this->collisionShape_ already notifies us of any changes.
597    }
598
599    //! Returns an attached collision Shape of this object (delegated to the internal CompoundCollisionShape)
600    CollisionShape* WorldEntity::getAttachedCollisionShape(unsigned int index)
601    {
602        return this->collisionShape_->getAttachedShape(index);
603    }
604
605    // Note: These functions are placed in WorldEntity.h as inline functions for the release build.
[3196]606#ifndef ORXONOX_RELEASE
[2662]607    const Vector3& WorldEntity::getPosition() const
608    {
609        return this->node_->getPosition();
610    }
611
612    const Quaternion& WorldEntity::getOrientation() const
613    {
614        return this->node_->getOrientation();
615    }
616
617    const Vector3& WorldEntity::getScale3D() const
618    {
619        return this->node_->getScale();
620    }
621#endif
622
623    //! Returns the position relative to the root space
624    const Vector3& WorldEntity::getWorldPosition() const
625    {
626        return this->node_->_getDerivedPosition();
627    }
628
629    //! Returns the orientation relative to the root space
630    const Quaternion& WorldEntity::getWorldOrientation() const
631    {
632        return this->node_->_getDerivedOrientation();
633    }
634
635    //! Returns the scaling applied relative to the root space in 3 coordinates
636    const Vector3& WorldEntity::getWorldScale3D() const
637    {
638        return this->node_->_getDerivedScale();
639    }
640
641    /**
642    @brief
643        Returns the scaling applied relative to the root space in 3 coordinates
644    @return
645        Returns the scaling if it is uniform, 1.0f otherwise.
646    */
647    float WorldEntity::getWorldScale() const
648    {
649        Vector3 scale = this->getWorldScale3D();
650        return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1;
651    }
652
653    /**
654    @brief
655        Sets the three dimensional scaling of this object.
[7401]656    @note
[2662]657        Scaling physical objects has not yet been implemented and is therefore forbidden.
658    */
659    void WorldEntity::setScale3D(const Vector3& scale)
660    {
[8706]661        // If physics is enabled scale the attached CollisionShape.
[11071]662        /*if (this->hasPhysics() && this->collisionShape_ != nullptr)
[2662]663        {
[8706]664            this->collisionShape_->setScale3D(scale);
665        }*/
666
[2662]667        this->node_->setScale(scale);
668
669        this->changedScale();
670    }
671
672    /**
673    @brief
674        Translates this WorldEntity by a vector.
[7401]675    @param distance
676        The relative distance of the translation
[2662]677    @param relativeTo
[7401]678        The TransformSpace of this translation
[2662]679    */
[3196]680    void WorldEntity::translate(const Vector3& distance, TransformSpace relativeTo)
[2662]681    {
682        switch (relativeTo)
683        {
[11071]684        case TransformSpace::Local:
[2662]685            // position is relative to parent so transform downwards
686            this->setPosition(this->getPosition() + this->getOrientation() * distance);
687            break;
[11071]688        case TransformSpace::Parent:
[2662]689            this->setPosition(this->getPosition() + distance);
690            break;
[11071]691        case TransformSpace::World:
[2662]692            // position is relative to parent so transform upwards
693            if (this->node_->getParent())
694                setPosition(getPosition() + (node_->getParent()->_getDerivedOrientation().Inverse() * distance)
695                    / node_->getParent()->_getDerivedScale());
696            else
697                this->setPosition(this->getPosition() + distance);
698            break;
699        }
700    }
701
702    /**
703    @brief
704        Rotates this WorldEntity by a quaternion.
[7401]705    @param rotation
706        The desired relative rotation
[2662]707    @param relativeTo
[7401]708        The TransformSpace of this translation
[2662]709    */
[3196]710    void WorldEntity::rotate(const Quaternion& rotation, TransformSpace relativeTo)
[2662]711    {
712        switch(relativeTo)
713        {
[11071]714        case TransformSpace::Local:
[2662]715            this->setOrientation(this->getOrientation() * rotation);
716            break;
[11071]717        case TransformSpace::Parent:
[2662]718            // Rotations are normally relative to local axes, transform up
719            this->setOrientation(rotation * this->getOrientation());
720            break;
[11071]721        case TransformSpace::World:
[2662]722            // Rotations are normally relative to local axes, transform up
723            this->setOrientation(this->getOrientation() * this->getWorldOrientation().Inverse()
724                * rotation * this->getWorldOrientation());
725            break;
726        }
727    }
728
729    /**
730    @brief
[6417]731        Makes this WorldEntity look at a specific target location.
[7401]732    @param target
733        An absolute point in the space which defines the direction of the entity
[2662]734    @param relativeTo
[7401]735        The TransformSpace of this translation
[2662]736    @param localDirectionVector
737        The vector which normally describes the natural direction of the object, usually -Z.
738    */
[3196]739    void WorldEntity::lookAt(const Vector3& target, TransformSpace relativeTo, const Vector3& localDirectionVector)
[2662]740    {
[3196]741        Vector3 origin(0, 0, 0);
[2662]742        switch (relativeTo)
743        {
[11071]744        case TransformSpace::Local:
[2662]745            origin = Vector3::ZERO;
746            break;
[11071]747        case TransformSpace::Parent:
[2662]748            origin = this->getPosition();
749            break;
[11071]750        case TransformSpace::World:
[2662]751            origin = this->getWorldPosition();
752            break;
753        }
754        this->setDirection(target - origin, relativeTo, localDirectionVector);
755    }
756
757    /**
758    @brief
759        Makes this WorldEntity look in specific direction.
[7401]760    @param direction
761        A point relative to the position of the WorldEntity which defines its orientation
[2662]762    @param relativeTo
[7401]763        The TransformSpace of this translation
[2662]764    @param localDirectionVector
765        The vector which normally describes the natural direction of the object, usually -Z.
766    */
[3196]767    void WorldEntity::setDirection(const Vector3& direction, TransformSpace relativeTo, const Vector3& localDirectionVector)
[2662]768    {
769        Quaternion savedOrientation(this->getOrientation());
[3196]770        this->node_->setDirection(direction, static_cast<Ogre::Node::TransformSpace>(relativeTo), localDirectionVector);
[2662]771        Quaternion newOrientation(this->node_->getOrientation());
772        this->node_->setOrientation(savedOrientation);
773        this->setOrientation(newOrientation);
774    }
775
776    //! Activates physics if the CollisionType is not None.
777    void WorldEntity::activatePhysics()
778    {
779        if (this->isActive() && this->hasPhysics() && !this->isPhysicsActive() && !this->parent_)
780        {
781            this->getScene()->addPhysicalObject(this);
782            this->bPhysicsActive_ = true;
783            this->bPhysicsActiveSynchronised_ = true;
784        }
785    }
786
787    //! Deactivates physics but the CollisionType does not change.
788    void WorldEntity::deactivatePhysics()
789    {
790        if (this->isPhysicsActive())
791        {
792            this->getScene()->removePhysicalObject(this);
793            this->bPhysicsActive_ = false;
794            this->bPhysicsActiveSynchronised_ = false;
795        }
796    }
797
798    //! Tells whether the object has already been added to the Bullet physics World.
799    bool WorldEntity::addedToPhysicalWorld() const
800    {
801        return this->physicalBody_ && this->physicalBody_->isInWorld();
802    }
803
804    /**
805    @brief
[7401]806        Sets the CollisionType. This alters the object significantly!
807    @note
[2662]808        Operation does not work on attached WorldEntities.
809    */
810    void WorldEntity::setCollisionType(CollisionType type)
811    {
812        if (this->collisionType_ == type)
813            return;
814
815        // If we are already attached to a parent, this would be a bad idea..
816        if (this->parent_)
817        {
[8858]818            orxout(internal_warning) << "Cannot set the collision type of a WorldEntity with a parent." << endl;
[2662]819            return;
820        }
821
822        // Check for type legality. Could be StaticEntity or MobileEntity.
823        if (!this->isCollisionTypeLegal(type))
824            return;
825
826        if (this->isPhysicsActive())
827            this->deactivatePhysics();
828
829        bool bReactivatePhysics = true;
830        if (this->hasPhysics() && !this->isPhysicsActive())
831            bReactivatePhysics = false;
832
833        // Check whether we have to create or destroy.
[11071]834        if (type != CollisionType::None && this->collisionType_ == CollisionType::None)
[2662]835        {
836/*
837HACK HACK HACK
838            // Check whether there was some scaling applied.
839            if (!this->node_->getScale().positionEquals(Vector3(1, 1, 1), 0.001))
840            {
[8858]841                orxout(internal_warning) << "Cannot create a physical body if there is scaling applied to the node: Not yet implemented." << endl;
[2662]842                return;
843            }
844HACK HACK HACK
845*/
846            // Create new rigid body
847            btRigidBody::btRigidBodyConstructionInfo bodyConstructionInfo(0, this, this->collisionShape_->getCollisionShape());
848            this->physicalBody_ = new btRigidBody(bodyConstructionInfo);
849            this->physicalBody_->setUserPointer(this);
850            this->physicalBody_->setActivationState(DISABLE_DEACTIVATION);
851        }
[11071]852        else if (type == CollisionType::None && this->collisionType_ != CollisionType::None)
[2662]853        {
854            // Destroy rigid body
855            assert(this->physicalBody_);
856            deactivatePhysics();
857            delete this->physicalBody_;
[11071]858            this->physicalBody_ = nullptr;
859            this->collisionType_ = CollisionType::None;
860            this->collisionTypeSynchronised_ = CollisionType::None;
[2662]861            return;
862        }
863
864        // Change type
865        switch (type)
866        {
[11071]867        case CollisionType::Dynamic:
[2855]868            this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_STATIC_OBJECT & !btCollisionObject::CF_KINEMATIC_OBJECT);
[2662]869            break;
[11071]870        case CollisionType::Kinematic:
[3196]871            this->physicalBody_->setCollisionFlags((this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_STATIC_OBJECT) | btCollisionObject::CF_KINEMATIC_OBJECT);
[2662]872            break;
[11071]873        case CollisionType::Static:
[3196]874            this->physicalBody_->setCollisionFlags((this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_KINEMATIC_OBJECT) | btCollisionObject::CF_STATIC_OBJECT);
[2662]875            break;
[11071]876        case CollisionType::None:
[2662]877            assert(false); // Doesn't happen
878            return;
879        }
880        this->collisionType_ = type;
881        this->collisionTypeSynchronised_ = type;
882
883        // update mass and inertia tensor
884        recalculateMassProps();
885        internalSetPhysicsProps();
886        collisionCallbackActivityChanged();
887        collisionResponseActivityChanged();
888        if (bReactivatePhysics)
889            activatePhysics();
890    }
891
892    //! Sets the CollisionType by string (used for the XMLPort)
893    void WorldEntity::setCollisionTypeStr(const std::string& typeStr)
894    {
[6417]895        const std::string& typeStrLower = getLowercase(typeStr);
[2662]896        CollisionType type;
897        if (typeStrLower == "dynamic")
[11071]898            type = CollisionType::Dynamic;
[2662]899        else if (typeStrLower == "static")
[11071]900            type = CollisionType::Static;
[2662]901        else if (typeStrLower == "kinematic")
[11071]902            type = CollisionType::Kinematic;
[2662]903        else if (typeStrLower == "none")
[11071]904            type = CollisionType::None;
[2662]905        else
906            ThrowException(ParseError, std::string("Attempting to set an unknown collision type: '") + typeStr + "'.");
907        this->setCollisionType(type);
908    }
909
910    //! Gets the CollisionType by string (used for the XMLPort)
911    std::string WorldEntity::getCollisionTypeStr() const
912    {
913        switch (this->getCollisionType())
914        {
[11071]915            case CollisionType::Dynamic:
[2662]916                return "dynamic";
[11071]917            case CollisionType::Kinematic:
[2662]918                return "kinematic";
[11071]919            case CollisionType::Static:
[2662]920                return "static";
[11071]921            case CollisionType::None:
[2662]922                return "none";
923            default:
924                assert(false);
925                return "";
926        }
927    }
928
929    /**
930    @brief
931        Recalculates the accumulated child mass and calls recalculateMassProps()
932        and notifies the parent of the change.
[7401]933    @note
[2662]934        Called by a child WE
935    */
936    void WorldEntity::notifyChildMassChanged()
937    {
938        // Note: CollisionShape changes of a child get handled over the internal CompoundCollisionShape already
939        // Recalculate mass
940        this->childrenMass_ = 0.0f;
[11071]941        for (WorldEntity* child : this->children_)
942            this->childrenMass_ += child->getMass();
[2662]943        recalculateMassProps();
944        // Notify parent WE
945        if (this->parent_)
946            parent_->notifyChildMassChanged();
947    }
948
949    /**
950    @brief
951        Undertakes the necessary steps to change the collision shape in Bullet, even at runtime.
[7401]952    @note
[2662]953        - called by this->collisionShape_
954        - May have a REALLY big overhead when called continuously at runtime, because then we need
955          to remove the physical body from Bullet and add it again.
956    */
957    void WorldEntity::notifyCollisionShapeChanged()
958    {
959        if (hasPhysics())
960        {
961            // Bullet doesn't like sudden changes of the collision shape, so we remove and add it again
962            if (this->addedToPhysicalWorld())
963            {
964                this->deactivatePhysics();
965                this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape());
966                this->activatePhysics();
967            }
968            else
969                this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape());
970        }
971        recalculateMassProps();
972    }
973
974    //! Updates all mass dependent parameters (mass, inertia tensor and child mass)
975    void WorldEntity::recalculateMassProps()
976    {
977        // Store local inertia for faster access. Evaluates to (0,0,0) if there is no collision shape.
978        float totalMass = this->mass_ + this->childrenMass_;
979        this->collisionShape_->calculateLocalInertia(totalMass, this->localInertia_);
980        if (this->hasPhysics())
981        {
982            if (this->isStatic())
983            {
984                // Just set everything to zero
985                this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0));
986            }
[8858]987            else if (totalMass == 0.0f)
[2662]988            {
989                // Use default values to avoid very large or very small values
[8858]990                orxout(internal_warning) << "Setting the internal physical mass to 1.0 because mass_ is 0.0" << endl;
[2662]991                btVector3 inertia(0, 0, 0);
992                this->collisionShape_->calculateLocalInertia(1.0f, inertia);
993                this->physicalBody_->setMassProps(1.0f, inertia);
994            }
995            else
996            {
997                this->physicalBody_->setMassProps(totalMass, this->localInertia_);
998            }
999        }
1000    }
1001
[6417]1002    //! Copies our own parameters for restitution, angular factor, damping and friction to the bullet rigid body.
[2662]1003    void WorldEntity::internalSetPhysicsProps()
1004    {
1005        if (this->hasPhysics())
1006        {
1007            this->physicalBody_->setRestitution(this->restitution_);
1008            this->physicalBody_->setAngularFactor(this->angularFactor_);
1009            this->physicalBody_->setDamping(this->linearDamping_, this->angularDamping_);
1010            this->physicalBody_->setFriction(this->friction_);
[10288]1011            this->physicalBody_->setCcdMotionThreshold(this->ccdMotionThreshold_);
1012            this->physicalBody_->setCcdSweptSphereRadius(this->ccdSweptSphereRadius_);
[2662]1013        }
1014    }
[2072]1015}
Note: See TracBrowser for help on using the repository browser.