Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/orxonox/worldentities/WorldEntity.h @ 6519

Last change on this file since 6519 was 6489, checked in by dafrick, 15 years ago

Changed changedActivity and changedVisibility such that when transiting to inactive/invisible the current activity/visibility of all atached objects is saved by them and restored once the parent node transits to active/visible again.

  • Property svn:eol-style set to native
File size: 23.2 KB
Line 
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
24 *      Reto Grieder (physics)
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#ifndef _WorldEntity_H__
31#define _WorldEntity_H__
32
33#include "OrxonoxPrereqs.h"
34
35#include <OgreUserDefinedObject.h>
36#ifdef ORXONOX_RELEASE
37#  include <OgreSceneNode.h>
38#endif
39#include <LinearMath/btMotionState.h>
40
41#include "util/Math.h"
42#include "util/OgreForwardRefs.h"
43#include "core/BaseObject.h"
44#include "network/synchronisable/Synchronisable.h"
45
46namespace orxonox
47{
48    /**
49    @brief
50        The WorldEntity represents everything that can be put in a Scene at a certain location.
51
52        It is supposed to be the base class of everything you would call an 'object' in a Scene.
53        The class itself is abstract which means you cannot use it directly. You may use StaticEntity
54        as the simplest derivative or (derived from MobileEntity) MovableEntity and ControllableEntity
55        as more advanced ones.
56
57        The basic task of the WorldEntity is provide a location, a direction and a scaling and the possibility
58        to create an entire hierarchy of derived objects.
59        It is also the basis for the physics interface to the Bullet physics engine.
60        Every WorldEntity can have a specific collision type: @see CollisionType
61        This would then imply that every scene object could have any collision type. To limit this, you can always
62        override this->isCollisionTypeLegal(CollisionType). Return false if the collision type is not supported
63        for a specific object.
64        There is also support for attaching WorldEntities with physics to each other. Currently, the collision shape
65        of both objects simply get merged into one larger shape (for static collision type).
66        The physical body that is internally stored and administrated has the following supported properties:
67        - Restitution, angular factor, linear damping, angular damping, friction, mass and collision shape.
68        You can get more information at the corresponding set function.
69
70        Collision shapes: These are controlled by the internal WorldEntityCollisionShape. @see WorldEntityCollisionShape.
71    */
72    class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable, public btMotionState, public Ogre::UserDefinedObject
73    {
74        friend class Scene;
75
76        public:
77            // Define our own transform space enum to avoid Ogre includes here
78            /**
79            @brief
80                Enumeration denoting the spaces which a transform can be relative to.
81            */
82            enum TransformSpace
83            {
84                //! Transform is relative to the local space
85                Local,
86                //! Transform is relative to the space of the parent node
87                Parent,
88                //! Transform is relative to world space
89                World
90            };
91
92        public:
93            WorldEntity(BaseObject* creator);
94            virtual ~WorldEntity();
95
96            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
97            void registerVariables();
98
99            inline const Ogre::SceneNode* getNode() const
100                { return this->node_; }
101
102            static const Vector3 FRONT;
103            static const Vector3 BACK;
104            static const Vector3 LEFT;
105            static const Vector3 RIGHT;
106            static const Vector3 DOWN;
107            static const Vector3 UP;
108           
109            virtual void changedActivity(void);
110            virtual void changedVisibility(void);
111
112            virtual void setPosition(const Vector3& position) = 0;
113            inline void setPosition(float x, float y, float z)
114                { this->setPosition(Vector3(x, y, z)); }
115            const Vector3& getPosition() const;
116            const Vector3& getWorldPosition() const;
117
118            void translate(const Vector3& distance, TransformSpace relativeTo = WorldEntity::Parent);
119            inline void translate(float x, float y, float z, TransformSpace relativeTo = WorldEntity::Parent)
120                { this->translate(Vector3(x, y, z), relativeTo); }
121
122            virtual inline const Vector3& getVelocity() const
123                { return Vector3::ZERO; }
124
125            virtual void setOrientation(const Quaternion& orientation) = 0;
126            inline void setOrientation(float w, float x, float y, float z)
127                { this->setOrientation(Quaternion(w, x, y, z)); }
128            inline void setOrientation(const Vector3& axis, const Radian& angle)
129                { this->setOrientation(Quaternion(angle, axis)); }
130            inline void setOrientation(const Vector3& axis, const Degree& angle)
131                { this->setOrientation(Quaternion(angle, axis)); }
132            const Quaternion& getOrientation() const;
133            const Quaternion& getWorldOrientation() const;
134
135            void rotate(const Quaternion& rotation, TransformSpace relativeTo = WorldEntity::Local);
136            inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace relativeTo = WorldEntity::Local)
137                { this->rotate(Quaternion(angle, axis), relativeTo); }
138
139            inline void yaw(const Degree& angle, TransformSpace relativeTo = WorldEntity::Local)
140                { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); }
141            inline void pitch(const Degree& angle, TransformSpace relativeTo = WorldEntity::Local)
142                { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); }
143            inline void roll(const Degree& angle, TransformSpace relativeTo = WorldEntity::Local)
144                { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); }
145
146            void lookAt(const Vector3& target, TransformSpace relativeTo = WorldEntity::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
147            void setDirection(const Vector3& direction, TransformSpace relativeTo = WorldEntity::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
148            inline void setDirection(float x, float y, float z, TransformSpace relativeTo = WorldEntity::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
149                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
150
151            virtual void setScale3D(const Vector3& scale);
152            inline void setScale3D(float x, float y, float z)
153                { this->setScale3D(Vector3(x, y, z)); }
154            const Vector3& getScale3D() const;
155            const Vector3& getWorldScale3D() const;
156
157            inline void setScale(float scale)
158                { this->setScale3D(scale, scale, scale); }
159            inline float getScale() const
160                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
161            float getWorldScale() const;
162
163            inline void scale3D(const Vector3& scale)
164                { this->setScale3D(this->getScale3D() * scale); }
165            inline void scale3D(float x, float y, float z)
166                { this->scale3D(Vector3(x, y, z)); }
167            inline void scale(float scale)
168                { this->scale3D(scale, scale, scale); }
169
170            virtual void changedScale() {}
171
172            void attach(WorldEntity* object);
173            void detach(WorldEntity* object);
174            WorldEntity* getAttachedObject(unsigned int index);
175            inline const std::set<WorldEntity*>& getAttachedObjects() const
176                { return this->children_; }
177
178            void attachOgreObject(Ogre::MovableObject*  object);
179            void attachOgreObject(Ogre::BillboardSet*   object);
180            void attachOgreObject(Ogre::Camera*         object);
181            void attachOgreObject(Ogre::Entity*         object);
182            void attachOgreObject(Ogre::ParticleSystem* object);
183
184            void detachOgreObject(Ogre::MovableObject*  object);
185            void detachOgreObject(Ogre::BillboardSet*   object);
186            void detachOgreObject(Ogre::Camera*         object);
187            void detachOgreObject(Ogre::Entity*         object);
188            void detachOgreObject(Ogre::ParticleSystem* object);
189
190            Ogre::MovableObject* detachOgreObject(const Ogre::String& name);
191
192            inline void attachToParent(WorldEntity* parent)
193                { parent->attach(this); }
194            inline void detachFromParent()
195                { if (this->parent_) { this->parent_->detach(this); } }
196            inline WorldEntity* getParent() const
197                { return this->parent_; }
198
199            void attachNode(Ogre::SceneNode* node);
200            void detachNode(Ogre::SceneNode* node);
201            void attachToNode(Ogre::SceneNode* node);
202            void detachFromNode(Ogre::SceneNode* node);
203
204            inline void setDeleteWithParent(bool value)
205                { this->bDeleteWithParent_ = value; }
206            inline bool getDeleteWithParent() const
207                { return this->bDeleteWithParent_; }
208
209            void notifyChildPropsChanged();
210
211        protected:
212            virtual void parentChanged() {}
213
214            Ogre::SceneNode* node_;
215
216        private:
217            inline void lookAt_xmlport(const Vector3& target)
218                { this->lookAt(target); }
219            inline void setDirection_xmlport(const Vector3& direction)
220                { this->setDirection(direction); }
221            inline void yaw_xmlport(const Degree& angle)
222                { this->yaw(angle); }
223            inline void pitch_xmlport(const Degree& angle)
224                { this->pitch(angle); }
225            inline void roll_xmlport(const Degree& angle)
226                { this->roll(angle); }
227
228            // network callbacks
229            void networkcallback_parentChanged();
230            inline void scaleChanged()
231                { this->setScale3D(this->getScale3D()); }
232
233            WorldEntity* parent_;
234            unsigned int parentID_;
235            std::set<WorldEntity*> children_;
236            bool bDeleteWithParent_;
237           
238            bool bActiveMem_;
239            bool bVisibleMem_;
240
241
242        /////////////
243        // Physics //
244        /////////////
245
246        public:
247            /**
248            @brief
249                Denotes the possible types of physical objects in a Scene.
250
251                Dynamic:   The object is influenced by its physical environment, like for instance little ball.
252                Kinematic: The object can only influence other dynamic objects. It's movement is coordinated by your own saying.
253                Static:    Like kinematic but the object is not allowed to move during the simulation.
254                None:      The object has no physics at all.
255            */
256            enum CollisionType
257            {
258                Dynamic,
259                Kinematic,
260                Static,
261                None
262            };
263
264            //! Tells whether the object has any connection to the Bullet physics engine. If hasPhysics() is false, the object may still have a velocity.
265            bool hasPhysics()       const { return getCollisionType() != None     ; }
266            //! @see CollisionType
267            bool isStatic()         const { return getCollisionType() == Static   ; }
268            //! @see CollisionType
269            bool isKinematic()      const { return getCollisionType() == Kinematic; }
270            //! @see CollisionType
271            bool isDynamic()        const { return getCollisionType() == Dynamic  ; }
272            //! Tells whether physics has been activated (you can temporarily deactivate it)
273            bool isPhysicsActive()  const { return this->bPhysicsActive_; }
274            bool addedToPhysicalWorld() const;
275
276            void activatePhysics();
277            void deactivatePhysics();
278
279            //! Returns the CollisionType. @see CollisionType.
280            inline CollisionType getCollisionType() const
281                { return this->collisionType_; }
282            void setCollisionType(CollisionType type);
283
284            void setCollisionTypeStr(const std::string& type);
285            std::string getCollisionTypeStr() const;
286
287            //! Sets the mass of this object. Note that the total mass may be influenced by attached objects!
288            inline void setMass(float mass)
289                { this->mass_ = mass; recalculateMassProps(); }
290            //! Returns the mass of this object without its children.
291            inline float getMass() const
292                { return this->mass_; }
293
294            //! Returns the total mass of this object with all its attached children.
295            inline float getTotalMass() const
296                { return this->mass_ + this->childrenMass_; }
297
298            /**
299            @brief
300                Returns the diagonal elements of the inertia tensor when calculated in local coordinates.
301            @Note
302                The local inertia tensor cannot be set, but is calculated by Bullet according to the collisionShape.
303                With compound collision shapes, an approximation is used.
304            */
305            inline const btVector3& getLocalInertia() const
306                { return this->localInertia_; }
307
308            /**
309            @brief
310                Sets how much reaction is applied in a collision.
311
312                Consider two equal spheres colliding with equal velocities:
313                Restitution 1 means that both spheres simply reverse their velocity (no loss of energy)
314                Restitution 0 means that both spheres will immediately stop moving
315                (maximum loss of energy without violating of the preservation of momentum)
316            */
317            inline void setRestitution(float restitution)
318                { this->restitution_ = restitution; internalSetPhysicsProps(); }
319            //! Returns the restitution parameter. @see setRestitution.
320            inline float getRestitution() const
321                { return this->restitution_; }
322
323            /**
324            @brief
325                Sets an artificial parameter that tells how much torque is applied when you apply a non-central force.
326
327                Normally the angular factor is 1, which means it's physically 'correct'. However if you have a player
328                character that should not rotate when hit sideways, you can set the angular factor to 0.
329            */
330            inline void setAngularFactor(float angularFactor)
331                { this->angularFactor_ = angularFactor; internalSetPhysicsProps(); }
332            //! Returns the angular factor. @see setAngularFactor.
333            inline float getAngularFactor() const
334                { return this->angularFactor_; }
335
336            //! Applies a mass independent damping. Velocities will simply diminish exponentially.
337            inline void setLinearDamping(float linearDamping)
338                { this->linearDamping_ = linearDamping; internalSetPhysicsProps(); }
339            //! Returns the linear damping. @see setLinearDamping.
340            inline float getLinearDamping() const
341                { return this->linearDamping_; }
342
343            //! Applies a tensor independent rotation damping. Angular velocities will simply diminish exponentially.
344            inline void setAngularDamping(float angularDamping)
345                { this->angularDamping_ = angularDamping; internalSetPhysicsProps(); }
346            //! Returns the angular damping. @see setAngularDamping.
347            inline float getAngularDamping() const
348                { return this->angularDamping_; }
349
350            //! Applies friction to the object. Friction occurs when two objects collide.
351            inline void setFriction(float friction)
352                { this->friction_ = friction; internalSetPhysicsProps(); }
353            //! Returns the amount of friction applied to the object.
354            inline float getFriction() const
355                { return this->friction_; }
356
357            void attachCollisionShape(CollisionShape* shape);
358            void detachCollisionShape(CollisionShape* shape);
359            CollisionShape* getAttachedCollisionShape(unsigned int index);
360
361            void notifyCollisionShapeChanged();
362            void notifyChildMassChanged();
363
364            /**
365            @brief
366                Virtual function that gets called when this object collides with another.
367            @param otherObject
368                The object this one has collided into.
369            @pram contactPoint
370                Contact point provided by Bullet. Holds more information and can me modified. See return value.
371            @Return
372                Returning false means that no modification to the contactPoint has been made. Return true otherwise!
373            @Note
374                Condition is that enableCollisionCallback() was called.
375            */
376            virtual inline bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
377                { return false; } /* With false, Bullet assumes no modification to the collision objects. */
378
379            //! Enables the collidesAgainst(.) function. The object doesn't respond to collision otherwise!
380            inline void enableCollisionCallback()
381                { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); }
382            //! Disables the collidesAgainst(.) function. @see enableCollisionCallback()
383            inline void disableCollisionCallback()
384                { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); }
385            //! Tells whether there could be a collision callback via collidesAgainst(.)
386            inline bool isCollisionCallbackActive() const
387                { return this->bCollisionCallbackActive_; }
388
389            //! Enables or disables collision response (default is of course on)
390            inline void setCollisionResponse(bool value)
391                { this->bCollisionResponseActive_ = value; this->collisionResponseActivityChanged(); }
392            //! Tells whether there could be a collision response
393            inline bool hasCollisionResponse()
394                { return this->bCollisionResponseActive_; }
395
396        protected:
397            /**
398            @brief
399                Function checks whether the requested collision type is legal to this object.
400
401                You can override this function in a derived class to constrain the collision to e.g. None or Dynamic.
402                A projectile may not prove very useful if there is no physical body. Simply set the CollisionType
403                in its constructor and override this method. But be careful that a derived class's virtual functions
404                don't yet exist in the constructor if a base class.
405            */
406            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
407
408            btRigidBody*  physicalBody_; //!< Bullet rigid body. Everything physical is applied to this instance.
409
410        private:
411            void recalculateMassProps();
412            void internalSetPhysicsProps();
413
414            bool notifyBeingAttached(WorldEntity* newParent);
415            void notifyDetached();
416
417            // network callbacks
418            void collisionTypeChanged();
419            void physicsActivityChanged();
420            void collisionCallbackActivityChanged();
421            void collisionResponseActivityChanged();
422            //! Network callback workaround to call a function when the value changes.
423            inline void massChanged()
424                { this->setMass(this->mass_); }
425            //! Network callback workaround to call a function when the value changes.
426            inline void restitutionChanged()
427                { this->setRestitution(this->restitution_); }
428            //! Network callback workaround to call a function when the value changes.
429            inline void angularFactorChanged()
430                { this->setAngularFactor(this->angularFactor_); }
431            //! Network callback workaround to call a function when the value changes.
432            inline void linearDampingChanged()
433                { this->setLinearDamping(this->linearDamping_); }
434            //! Network callback workaround to call a function when the value changes.
435            inline void angularDampingChanged()
436                { this->setAngularDamping(this->angularDamping_); }
437            //! Network callback workaround to call a function when the value changes.
438            inline void frictionChanged()
439                { this->setFriction(this->friction_); }
440
441            CollisionType                collisionType_;                 //!< @see setCollisionType
442            CollisionType                collisionTypeSynchronised_;     //!< Network synchronised variable for collisionType_
443            bool                         bPhysicsActive_;                //!< @see isPhysicsActive
444            bool                         bPhysicsActiveSynchronised_;    //!< Network synchronised variable for bPhysicsActive_
445            //! When attaching objects hierarchically this variable tells this object (as child) whether physics was activated before attaching (because the deactivate physics while being attached).
446            bool                         bPhysicsActiveBeforeAttaching_;
447            WorldEntityCollisionShape*   collisionShape_;                //!< Attached collision shapes go here
448            btScalar                     mass_;                          //!< @see setMass
449            btVector3                    localInertia_;                  //!< @see getLocalInertia
450            btScalar                     restitution_;                   //!< @see setRestitution
451            btScalar                     angularFactor_;                 //!< @see setAngularFactor
452            btScalar                     linearDamping_;                 //!< @see setLinearDamping
453            btScalar                     angularDamping_;                //!< @see setAngularDamping
454            btScalar                     friction_;                      //!< @see setFriction
455            btScalar                     childrenMass_;                  //!< Sum of all the children's masses
456            bool                         bCollisionCallbackActive_;      //!< @see enableCollisionCallback
457            bool                         bCollisionResponseActive_;      //!< Tells whether the object should respond to collisions
458    };
459
460    // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here.
461#ifdef ORXONOX_RELEASE
462    inline const Vector3& WorldEntity::getPosition() const
463        { return this->node_->getPosition(); }
464    inline const Quaternion& WorldEntity::getOrientation() const
465        { return this->node_->getOrientation(); }
466    inline const Vector3& WorldEntity::getScale3D() const
467        { return this->node_->getScale(); }
468#endif
469
470    SUPER_FUNCTION(5, WorldEntity, changedScale, false);
471}
472
473#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.