Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.h @ 2374

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

Trying to synchronise phyiscs over the network.

  • Removed derivation of CollisionShape from WorldEntity (BaseObject instead).
  • Property svn:eol-style set to native
File size: 10.3 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#define OGRE_FORCE_ANGLE_TYPES
36#include <OgreSceneNode.h>
37
38#include "LinearMath/btMotionState.h"
39
40#include "network/Synchronisable.h"
41#include "core/BaseObject.h"
42#include "util/Math.h"
43
44namespace orxonox
45{
46    class _OrxonoxExport WorldEntity : public BaseObject, public network::Synchronisable, public btMotionState
47    {
48        public:
49            WorldEntity(BaseObject* creator);
50            virtual ~WorldEntity();
51
52            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
53            void registerVariables();
54
55            inline const Ogre::SceneNode* getNode() const
56                { return this->node_; }
57
58            static const Vector3 FRONT;
59            static const Vector3 BACK;
60            static const Vector3 LEFT;
61            static const Vector3 RIGHT;
62            static const Vector3 DOWN;
63            static const Vector3 UP;
64
65            virtual void setPosition(const Vector3& position) = 0;
66            inline void setPosition(float x, float y, float z)
67                { this->setPosition(Vector3(x, y, z)); }
68            inline const Vector3& getPosition() const
69                { return this->node_->getPosition(); }
70            inline const Vector3& getWorldPosition() const
71                { return this->node_->getWorldPosition(); }
72
73            virtual void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
74            inline void translate(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
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            inline const Quaternion& getOrientation() const
85                { return this->node_->getOrientation(); }
86            inline const Quaternion& getWorldOrientation() const
87                { return this->node_->getWorldOrientation(); }
88
89            virtual void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
90            inline void rotate(const Vector3& axis, const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
91                { this->rotate(Quaternion(angle, axis), relativeTo); }
92            inline void rotate(const Vector3& axis, const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
93                { this->rotate(Quaternion(angle, axis), relativeTo); }
94
95            virtual void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
96            inline void yaw(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
97                { this->yaw(Degree(angle), relativeTo); }
98            virtual void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
99            inline void pitch(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
100                { this->pitch(Degree(angle), relativeTo); }
101            virtual void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
102            inline void roll(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
103                { this->roll(Degree(angle), relativeTo); }
104
105            virtual void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0;
106            virtual void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0;
107            inline void setDirection(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
108                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
109
110            virtual void setScale3D(const Vector3& scale);
111            inline void setScale3D(float x, float y, float z)
112                { this->setScale3D(Vector3(x, y, z)); }
113            inline const Vector3& getScale3D(void) const
114                { return this->node_->getScale(); }
115
116            void setScale(float scale)
117                { this->setScale3D(scale, scale, scale); }
118            inline float getScale() const
119                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
120
121            virtual void scale3D(const Vector3& scale);
122            inline void scale3D(float x, float y, float z)
123                { this->scale3D(Vector3(x, y, z)); }
124            inline void scale(float scale)
125                { this->scale3D(scale, scale, scale); }
126
127            void attach(WorldEntity* object);
128//            void attachAsdf(BlinkingBillboard* object);
129            void detach(WorldEntity* object);
130            WorldEntity* getAttachedObject(unsigned int index) const;
131//            BlinkingBillboard* getAttachedAsdfObject(unsigned int index) const;
132            inline const std::set<WorldEntity*>& getAttachedObjects() const
133                { return this->children_; }
134
135            inline void attachOgreObject(Ogre::MovableObject* object)
136                { this->node_->attachObject(object); }
137            inline void detachOgreObject(Ogre::MovableObject* object)
138                { this->node_->detachObject(object); }
139            inline Ogre::MovableObject* detachOgreObject(const Ogre::String& name)
140                { return this->node_->detachObject(name); }
141
142            inline void attachToParent(WorldEntity* parent)
143                { parent->attach(this); }
144            inline void detachFromParent()
145                { if (this->parent_) { this->parent_->detach(this); } }
146            inline WorldEntity* getParent() const
147                { return this->parent_; }
148
149        protected:
150            Ogre::SceneNode* node_;
151
152        private:
153            void updateParent();
154
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            inline void scaleChanged()
167                { this->setScale3D(this->getScale3D()); }
168
169            WorldEntity* parent_;
170            unsigned int parentID_;
171            std::set<WorldEntity*> children_;
172
173
174        /////////////
175        // Physics //
176        /////////////
177
178        public:
179            enum CollisionType
180            {
181                Dynamic,
182                Kinematic,
183                Static,
184                None
185            };
186
187            bool hasPhysics()  const { return getCollisionType() != None     ; }
188            bool isStatic()    const { return getCollisionType() == Static   ; }
189            bool isKinematic() const { return getCollisionType() == Kinematic; }
190            bool isDynamic()   const { return getCollisionType() == Dynamic  ; }
191            bool isPhysicsRunning() const;
192
193            inline CollisionType getCollisionType() const
194                { return this->collisionType_; }
195            void setCollisionType(CollisionType type);
196
197            void setCollisionTypeStr(const std::string& type);
198            std::string getCollisionTypeStr() const;
199
200            void setMass(float mass);
201            inline float getMass() const
202                { return this->mass_; }
203
204            void attachCollisionShape(CollisionShape* shape);
205            CollisionShape* getAttachedCollisionShape(unsigned int index) const;
206
207            inline CompoundCollisionShape* getCollisionShape()
208                { return this->collisionShape_; }
209            inline btRigidBody* getPhysicalBody()
210                { return this->physicalBody_; }
211
212        protected:
213            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
214
215            btRigidBody*  physicalBody_;
216
217        private:
218            void updateCollisionType();
219            void mergeCollisionShape(CollisionShape* shape);
220            void internalSetMassProps();
221            btVector3 getLocalInertia(btScalar mass) const;
222            bool checkPhysics() const;
223            void addToPhysicalWorld() const;
224            void removeFromPhysicalWorld() const;
225
226            // network callbacks
227            void collisionTypeChanged();
228            void massChanged();
229
230            CollisionType                collisionType_;
231            CollisionType                collisionTypeSynchronised_;
232            CompoundCollisionShape*      collisionShape_;
233            btScalar                     mass_;
234            btScalar                     childMass_;
235    };
236}
237
238#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.