Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/WorldEntity.h @ 2535

Last change on this file since 2535 was 2535, checked in by rgrieder, 15 years ago
  • Renamed TransformSpace::Space to TransformSpace::Enum
  • Small changes and adjustments WorldEntity (Revealed while commenting…)
  • Property svn:eol-style set to native
File size: 13.7 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
[2459]24 *      Reto Grieder (physics)
[2072]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#ifndef _WorldEntity_H__
31#define _WorldEntity_H__
32
33#include "OrxonoxPrereqs.h"
34
[2459]35#ifdef _NDEBUG
[2072]36#include <OgreSceneNode.h>
[2459]37#else
38#include <OgrePrerequisites.h>
39#endif
40#include "LinearMath/btMotionState.h"
[2072]41
[2535]42#include "util/Math.h"
43#include "core/BaseObject.h"
[2371]44#include "network/synchronisable/Synchronisable.h"
[2535]45#include "objects/collisionshapes/CompoundCollisionShape.h"
[2072]46
47namespace orxonox
48{
[2459]49    class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable, public btMotionState
[2072]50    {
[2535]51        friend class Scene;
52
[2072]53        public:
54            WorldEntity(BaseObject* creator);
55            virtual ~WorldEntity();
56
57            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
58            void registerVariables();
59
[2459]60            inline const Ogre::SceneNode* getNode() const
[2072]61                { return this->node_; }
62
63            static const Vector3 FRONT;
64            static const Vector3 BACK;
65            static const Vector3 LEFT;
66            static const Vector3 RIGHT;
67            static const Vector3 DOWN;
68            static const Vector3 UP;
69
70            virtual void setPosition(const Vector3& position) = 0;
71            inline void setPosition(float x, float y, float z)
72                { this->setPosition(Vector3(x, y, z)); }
[2459]73            const Vector3& getPosition() const;
74            const Vector3& getWorldPosition() const;
[2072]75
[2535]76            void translate(const Vector3& distance, TransformSpace::Enum relativeTo = TransformSpace::Parent);
77            inline void translate(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::Parent)
[2072]78                { this->translate(Vector3(x, y, z), relativeTo); }
79
[2485]80            virtual inline const Vector3& getVelocity() const
81                { return Vector3::ZERO; }
82
[2072]83            virtual void setOrientation(const Quaternion& orientation) = 0;
84            inline void setOrientation(float w, float x, float y, float z)
85                { this->setOrientation(Quaternion(w, x, y, z)); }
86            inline void setOrientation(const Vector3& axis, const Radian& angle)
87                { this->setOrientation(Quaternion(angle, axis)); }
88            inline void setOrientation(const Vector3& axis, const Degree& angle)
89                { this->setOrientation(Quaternion(angle, axis)); }
[2459]90            const Quaternion& getOrientation() const;
91            const Quaternion& getWorldOrientation() const;
[2072]92
[2535]93            void rotate(const Quaternion& rotation, TransformSpace::Enum relativeTo = TransformSpace::Local);
94            inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local)
[2072]95                { this->rotate(Quaternion(angle, axis), relativeTo); }
96
[2535]97            inline void yaw(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local)
[2459]98                { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); }
[2535]99            inline void pitch(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local)
[2459]100                { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); }
[2535]101            inline void roll(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local)
[2459]102                { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); }
[2072]103
[2535]104            void lookAt(const Vector3& target, TransformSpace::Enum relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
105            void setDirection(const Vector3& direction, TransformSpace::Enum relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
106            inline void setDirection(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
[2072]107                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
108
[2459]109            virtual void setScale3D(const Vector3& scale);
[2072]110            inline void setScale3D(float x, float y, float z)
[2459]111                { this->setScale3D(Vector3(x, y, z)); }
112            const Vector3& getScale3D(void) const;
[2485]113            const Vector3& getWorldScale3D() const;
[2072]114
[2485]115            inline void setScale(float scale)
[2459]116                { this->setScale3D(scale, scale, scale); }
[2072]117            inline float getScale() const
118                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
[2485]119            float getWorldScale() const;
[2072]120
121            inline void scale3D(const Vector3& scale)
[2459]122                { this->setScale3D(this->getScale3D() * scale); }
[2072]123            inline void scale3D(float x, float y, float z)
[2459]124                { this->scale3D(Vector3(x, y, z)); }
[2072]125            inline void scale(float scale)
[2459]126                { this->scale3D(scale, scale, scale); }
[2072]127
[2485]128            virtual void changedScale() {}
129
[2072]130            void attach(WorldEntity* object);
131            void detach(WorldEntity* object);
[2535]132            WorldEntity* getAttachedObject(unsigned int index);
[2072]133            inline const std::set<WorldEntity*>& getAttachedObjects() const
134                { return this->children_; }
135
[2459]136            void attachOgreObject(Ogre::MovableObject* object);
137            void detachOgreObject(Ogre::MovableObject* object);
138            Ogre::MovableObject* detachOgreObject(const Ogre::String& name);
139
[2072]140            inline void attachToParent(WorldEntity* parent)
141                { parent->attach(this); }
142            inline void detachFromParent()
143                { if (this->parent_) { this->parent_->detach(this); } }
144            inline WorldEntity* getParent() const
145                { return this->parent_; }
146
[2485]147            void attachNode(Ogre::SceneNode* node);
148            void detachNode(Ogre::SceneNode* node);
149            void attachToNode(Ogre::SceneNode* node);
150            void detachFromNode(Ogre::SceneNode* node);
151
[2459]152            void notifyChildPropsChanged();
153
[2072]154        protected:
155            Ogre::SceneNode* node_;
156
157        private:
158            inline void lookAt_xmlport(const Vector3& target)
159                { this->lookAt(target); }
160            inline void setDirection_xmlport(const Vector3& direction)
161                { this->setDirection(direction); }
162            inline void yaw_xmlport(const Degree& angle)
163                { this->yaw(angle); }
164            inline void pitch_xmlport(const Degree& angle)
165                { this->pitch(angle); }
166            inline void roll_xmlport(const Degree& angle)
167                { this->roll(angle); }
168
[2459]169            // network callbacks
170            void parentChanged();
171            inline void scaleChanged()
172                { this->setScale3D(this->getScale3D()); }
173
[2072]174            WorldEntity* parent_;
175            unsigned int parentID_;
176            std::set<WorldEntity*> children_;
[2459]177
178
179        /////////////
180        // Physics //
181        /////////////
182
183        public:
184            enum CollisionType
185            {
186                Dynamic,
187                Kinematic,
188                Static,
189                None
190            };
191
192            bool hasPhysics()       const { return getCollisionType() != None     ; }
193            bool isStatic()         const { return getCollisionType() == Static   ; }
194            bool isKinematic()      const { return getCollisionType() == Kinematic; }
195            bool isDynamic()        const { return getCollisionType() == Dynamic  ; }
196            bool isPhysicsActive()  const { return this->bPhysicsActive_; }
197            bool addedToPhysicalWorld() const;
198
199            void activatePhysics();
200            void deactivatePhysics();
201
202            inline CollisionType getCollisionType() const
203                { return this->collisionType_; }
204            void setCollisionType(CollisionType type);
205
206            void setCollisionTypeStr(const std::string& type);
207            std::string getCollisionTypeStr() const;
208
209            inline void setMass(float mass)
210                { this->mass_ = mass; recalculateMassProps(); }
211            inline float getMass() const
212                { return this->mass_; }
213
214            inline float getTotalMass() const
215                { return this->mass_ + this->childrenMass_; }
216
217            inline const btVector3& getLocalInertia() const
218                { return this->localInertia_; }
219
220            inline void setRestitution(float restitution)
[2535]221                { this->restitution_ = restitution; internalSetPhysicsProps(); }
[2459]222            inline float getRestitution() const
223                { return this->restitution_; }
224
225            inline void setAngularFactor(float angularFactor)
[2535]226                { this->angularFactor_ = angularFactor; internalSetPhysicsProps(); }
[2459]227            inline float getAngularFactor() const
228                { return this->angularFactor_; }
229
230            inline void setLinearDamping(float linearDamping)
[2535]231                { this->linearDamping_ = linearDamping; internalSetPhysicsProps(); }
[2459]232            inline float getLinearDamping() const
233                { return this->linearDamping_; }
234
235            inline void setAngularDamping(float angularDamping)
[2535]236                { this->angularDamping_ = angularDamping; internalSetPhysicsProps(); }
[2459]237            inline float getAngularDamping() const
238                { return this->angularDamping_; }
239
240            inline void setFriction(float friction)
[2535]241                { this->friction_ = friction; internalSetPhysicsProps(); }
[2459]242            inline float getFriction() const
243                { return this->friction_; }
244
245            void attachCollisionShape(CollisionShape* shape);
246            void detachCollisionShape(CollisionShape* shape);
[2535]247            CollisionShape* getAttachedCollisionShape(unsigned int index);
[2459]248
249            void notifyCollisionShapeChanged();
250            void notifyChildMassChanged();
251
[2466]252            virtual inline bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
253                { return false; } /* With false, Bullet assumes no modification to the collision objects. */
254
255            inline void enableCollisionCallback()
256                { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); }
257            inline void disableCollisionCallback()
258                { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); }
[2527]259            inline bool isCollisionCallbackActive() const
[2466]260                { return this->bCollisionCallbackActive_; }
261
[2459]262        protected:
263            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
264
[2535]265            btRigidBody* physicalBody_;
[2459]266
267        private:
268            void updateCollisionType();
269            void recalculateMassProps();
[2535]270            void internalSetPhysicsProps();
[2459]271
[2527]272            bool notifyBeingAttached(WorldEntity* newParent);
273            void notifyDetached();
274
[2459]275            // network callbacks
276            void collisionTypeChanged();
277            void physicsActivityChanged();
[2466]278            void collisionCallbackActivityChanged();
[2459]279            inline void massChanged()
280                { this->setMass(this->mass_); }
281            inline void restitutionChanged()
282                { this->setRestitution(this->restitution_); }
283            inline void angularFactorChanged()
284                { this->setAngularFactor(this->angularFactor_); }
285            inline void linearDampingChanged()
286                { this->setLinearDamping(this->linearDamping_); }
287            inline void angularDampingChanged()
288                { this->setAngularDamping(this->angularDamping_); }
289            inline void frictionChanged()
290                { this->setFriction(this->friction_); }
291
292            CollisionType                collisionType_;
293            CollisionType                collisionTypeSynchronised_;
294            bool                         bPhysicsActive_;
295            bool                         bPhysicsActiveSynchronised_;
[2527]296            bool                         bPhysicsActiveBeforeAttaching_;
[2535]297            CompoundCollisionShape       collisionShape_;
[2459]298            btScalar                     mass_;
299            btVector3                    localInertia_;
300            btScalar                     restitution_;
301            btScalar                     angularFactor_;
302            btScalar                     linearDamping_;
303            btScalar                     angularDamping_;
304            btScalar                     friction_;
305            btScalar                     childrenMass_;
[2466]306            bool                         bCollisionCallbackActive_;
[2072]307    };
[2459]308
309    // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here.
310#ifdef _NDEBUG
311    inline const Vector3& WorldEntity::getPosition() const
312        { return this->node_->getPosition(); }
313    inline const Quaternion& WorldEntity::getOrientation() const
314        { return this->node_->getrOrientation(); }
315    inline const Vector3& WorldEntity::getScale3D(void) const
316        { return this->node_->getScale(); }
317#endif
[2485]318
319    SUPER_FUNCTION(5, WorldEntity, changedScale, false);
[2072]320}
321
322#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.