Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/WorldEntity.h @ 2452

Last change on this file since 2452 was 2452, checked in by rgrieder, 15 years ago

Minor changes and added localInertia_ to worldentity for faster access.

  • Property svn:eol-style set to native
File size: 12.4 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 void setOrientation(const Quaternion& orientation) = 0;
78            inline void setOrientation(float w, float x, float y, float z)
79                { this->setOrientation(Quaternion(w, x, y, z)); }
80            inline void setOrientation(const Vector3& axis, const Radian& angle)
81                { this->setOrientation(Quaternion(angle, axis)); }
82            inline void setOrientation(const Vector3& axis, const Degree& angle)
83                { this->setOrientation(Quaternion(angle, axis)); }
84            const Quaternion& getOrientation() const;
85            const Quaternion& getWorldOrientation() const;
86
87            void rotate(const Quaternion& rotation, TransformSpace::Space relativeTo = TransformSpace::Local);
88            inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local)
89                { this->rotate(Quaternion(angle, axis), relativeTo); }
90
91            inline void yaw(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local)
92                { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); }
93            inline void pitch(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local)
94                { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); }
95            inline void roll(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local)
96                { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); }
97
98            void lookAt(const Vector3& target, TransformSpace::Space relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
99            void setDirection(const Vector3& direction, TransformSpace::Space relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
100            inline void setDirection(float x, float y, float z, TransformSpace::Space relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
101                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
102
103            virtual void setScale3D(const Vector3& scale);
104            inline void setScale3D(float x, float y, float z)
105                { this->setScale3D(Vector3(x, y, z)); }
106            const Vector3& getScale3D(void) const;
107
108            void setScale(float scale)
109                { this->setScale3D(scale, scale, scale); }
110            inline float getScale() const
111                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
112
113            inline void scale3D(const Vector3& scale)
114                { this->setScale3D(this->getScale3D() * scale); }
115            inline void scale3D(float x, float y, float z)
116                { this->scale3D(Vector3(x, y, z)); }
117            inline void scale(float scale)
118                { this->scale3D(scale, scale, scale); }
119
120            void attach(WorldEntity* object);
121            void detach(WorldEntity* object);
122            WorldEntity* getAttachedObject(unsigned int index) const;
123            inline const std::set<WorldEntity*>& getAttachedObjects() const
124                { return this->children_; }
125
126            void attachOgreObject(Ogre::MovableObject* object);
127            void detachOgreObject(Ogre::MovableObject* object);
128            Ogre::MovableObject* detachOgreObject(const Ogre::String& name);
129
130            inline void attachToParent(WorldEntity* parent)
131                { parent->attach(this); }
132            inline void detachFromParent()
133                { if (this->parent_) { this->parent_->detach(this); } }
134            inline WorldEntity* getParent() const
135                { return this->parent_; }
136
137            void notifyChildPropsChanged();
138
139        protected:
140            Ogre::SceneNode* node_;
141
142        private:
143            inline void lookAt_xmlport(const Vector3& target)
144                { this->lookAt(target); }
145            inline void setDirection_xmlport(const Vector3& direction)
146                { this->setDirection(direction); }
147            inline void yaw_xmlport(const Degree& angle)
148                { this->yaw(angle); }
149            inline void pitch_xmlport(const Degree& angle)
150                { this->pitch(angle); }
151            inline void roll_xmlport(const Degree& angle)
152                { this->roll(angle); }
153
154            // network callbacks
155            void parentChanged();
156            inline void scaleChanged()
157                { this->setScale3D(this->getScale3D()); }
158
159            WorldEntity* parent_;
160            unsigned int parentID_;
161            std::set<WorldEntity*> children_;
162
163
164        /////////////
165        // Physics //
166        /////////////
167
168        public:
169            enum CollisionType
170            {
171                Dynamic,
172                Kinematic,
173                Static,
174                None
175            };
176
177            bool hasPhysics()       const { return getCollisionType() != None     ; }
178            bool isStatic()         const { return getCollisionType() == Static   ; }
179            bool isKinematic()      const { return getCollisionType() == Kinematic; }
180            bool isDynamic()        const { return getCollisionType() == Dynamic  ; }
181            bool isPhysicsActive()  const { return this->bPhysicsActive_; }
182            bool addedToPhysicalWorld() const;
183
184            void activatePhysics();
185            void deactivatePhysics();
186
187            inline CollisionType getCollisionType() const
188                { return this->collisionType_; }
189            void setCollisionType(CollisionType type);
190
191            void setCollisionTypeStr(const std::string& type);
192            std::string getCollisionTypeStr() const;
193
194            inline void setMass(float mass)
195                { this->mass_ = mass; recalculateMassProps(); }
196            inline float getMass() const
197                { return this->mass_; }
198
199            inline float getTotalMass() const
200                { return this->mass_ + this->childrenMass_; }
201
202            inline const btVector3& getLocalInertia() const
203                { return this->localInertia_; }
204
205            inline void setRestitution(float restitution)
206                { this->restitution_ = restitution; resetPhysicsProps(); }
207            inline float getRestitution() const
208                { return this->restitution_; }
209
210            inline void setAngularFactor(float angularFactor)
211                { this->angularFactor_ = angularFactor; resetPhysicsProps(); }
212            inline float getAngularFactor() const
213                { return this->angularFactor_; }
214
215            inline void setLinearDamping(float linearDamping)
216                { this->linearDamping_ = linearDamping; resetPhysicsProps(); }
217            inline float getLinearDamping() const
218                { return this->linearDamping_; }
219
220            inline void setAngularDamping(float angularDamping)
221                { this->angularDamping_ = angularDamping; resetPhysicsProps(); }
222            inline float getAngularDamping() const
223                { return this->angularDamping_; }
224
225            inline void setFriction(float friction)
226                { this->friction_ = friction; resetPhysicsProps(); }
227            inline float getFriction() const
228                { return this->friction_; }
229
230            void attachCollisionShape(CollisionShape* shape);
231            void detachCollisionShape(CollisionShape* shape);
232            CollisionShape* getAttachedCollisionShape(unsigned int index) const;
233
234            inline CompoundCollisionShape* getCollisionShape()
235                { return this->collisionShape_; }
236            inline btRigidBody* getPhysicalBody()
237                { return this->physicalBody_; }
238
239            void notifyCollisionShapeChanged();
240            void notifyChildMassChanged();
241
242        protected:
243            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
244
245            btRigidBody*  physicalBody_;
246
247        private:
248            void updateCollisionType();
249            void recalculateMassProps();
250            void resetPhysicsProps();
251
252            // network callbacks
253            void collisionTypeChanged();
254            void physicsActivityChanged();
255            inline void massChanged()
256                { this->setMass(this->mass_); }
257            inline void restitutionChanged()
258                { this->setRestitution(this->restitution_); }
259            inline void angularFactorChanged()
260                { this->setAngularFactor(this->angularFactor_); }
261            inline void linearDampingChanged()
262                { this->setLinearDamping(this->linearDamping_); }
263            inline void angularDampingChanged()
264                { this->setAngularDamping(this->angularDamping_); }
265            inline void frictionChanged()
266                { this->setFriction(this->friction_); }
267
268            CollisionType                collisionType_;
269            CollisionType                collisionTypeSynchronised_;
270            bool                         bPhysicsActive_;
271            bool                         bPhysicsActiveSynchronised_;
272            CompoundCollisionShape*      collisionShape_;
273            btScalar                     mass_;
274            btVector3                    localInertia_;
275            btScalar                     restitution_;
276            btScalar                     angularFactor_;
277            btScalar                     linearDamping_;
278            btScalar                     angularDamping_;
279            btScalar                     friction_;
280            btScalar                     childrenMass_;
281    };
282
283    // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here.
284#ifdef _NDEBUG
285    inline const Vector3& WorldEntity::getPosition() const
286        { return this->node_->getPosition(); }
287    inline const Quaternion& WorldEntity::getOrientation() const
288        { return this->node_->getrOrientation(); }
289    inline const Vector3& WorldEntity::getScale3D(void) const
290        { return this->node_->getScale(); }
291#endif
292}
293
294#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.