Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2423 was 2423, checked in by rgrieder, 15 years ago
  • Added detach functions to CollisionShapes
  • Added update functions across the CollisionShape hierarchy so that when you change the radius of a sphere, everything up to the WE gets updated.
  • Setting the btCollisionShape at run time doesn't work after all, fixed that (you can still do it, just a question of internals)
  • Improved network synchronisation
  • new WE function: addedToPhysicalWorld() to check whether we can still perform operations that are disallowed at run time (esp. StaticEntity)

Conclusively, I can say that right now, all operations considering physics should be handled automatically, bugs not withstanding.

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