Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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