Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Finally managed to work out some physics. According to my tests, collisions with simple spheres should work with dynamic/kinematic/static objects. There are currently only a limited number of XML parameters, but we're surely going to extend that. Furthermore there is some more thinking to be done concerning changes of btRigidBody properties when it's already added to the world.

  • Property svn:eol-style set to native
File size: 8.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 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _WorldEntity_H__
30#define _WorldEntity_H__
31
32#include "OrxonoxPrereqs.h"
33
34#define OGRE_FORCE_ANGLE_TYPES
35#include <OgreSceneNode.h>
36
37#include "LinearMath/btMotionState.h"
38#include "BulletDynamics/Dynamics/btRigidBody.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            enum CollisionType
50            {
51                Dynamic,
52                Kinematic,
53                Static
54            };
55
56        public:
57            WorldEntity(BaseObject* creator);
58            virtual ~WorldEntity();
59
60            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
61            void registerVariables();
62
63            inline Ogre::SceneNode* getNode() const
64                { return this->node_; }
65
66            static const Vector3 FRONT;
67            static const Vector3 BACK;
68            static const Vector3 LEFT;
69            static const Vector3 RIGHT;
70            static const Vector3 DOWN;
71            static const Vector3 UP;
72
73            virtual void setPosition(const Vector3& position) = 0;
74            inline void setPosition(float x, float y, float z)
75                { this->setPosition(Vector3(x, y, z)); }
76            inline const Vector3& getPosition() const
77                { return this->node_->getPosition(); }
78            inline const Vector3& getWorldPosition() const
79                { return this->node_->getWorldPosition(); }
80
81            virtual void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
82            inline void translate(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
83                { this->translate(Vector3(x, y, z), relativeTo); }
84
85            virtual void setOrientation(const Quaternion& orientation) = 0;
86            inline void setOrientation(float w, float x, float y, float z)
87                { this->setOrientation(Quaternion(w, x, y, z)); }
88            inline void setOrientation(const Vector3& axis, const Radian& angle)
89                { this->setOrientation(Quaternion(angle, axis)); }
90            inline void setOrientation(const Vector3& axis, const Degree& angle)
91                { this->setOrientation(Quaternion(angle, axis)); }
92            inline const Quaternion& getOrientation() const
93                { return this->node_->getOrientation(); }
94            inline const Quaternion& getWorldOrientation() const
95                { return this->node_->getWorldOrientation(); }
96
97            virtual void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
98            inline void rotate(const Vector3& axis, const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
99                { this->rotate(Quaternion(angle, axis), relativeTo); }
100            inline void rotate(const Vector3& axis, const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
101                { this->rotate(Quaternion(angle, axis), relativeTo); }
102
103            virtual void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
104            inline void yaw(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
105                { this->yaw(Degree(angle), relativeTo); }
106            virtual void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
107            inline void pitch(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
108                { this->pitch(Degree(angle), relativeTo); }
109            virtual void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0;
110            inline void roll(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
111                { this->roll(Degree(angle), relativeTo); }
112
113            virtual void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0;
114            virtual void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0;
115            inline void setDirection(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
116                { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); }
117
118            inline void setScale3D(const Vector3& scale)
119                { this->node_->setScale(scale); }
120            inline void setScale3D(float x, float y, float z)
121                { this->node_->setScale(x, y, z); }
122            inline const Vector3& getScale3D(void) const
123                { return this->node_->getScale(); }
124
125            inline void setScale(float scale)
126                { this->node_->setScale(scale, scale, scale); }
127            inline float getScale() const
128                { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; }
129
130            inline void scale3D(const Vector3& scale)
131                { this->node_->scale(scale); }
132            inline void scale3D(float x, float y, float z)
133                { this->node_->scale(x, y, z); }
134            inline void scale(float scale)
135                { this->node_->scale(scale, scale, scale); }
136
137            bool hasPhysics()  { return this->physicalBody_; }
138            bool isKinematic() { return this->physicalBody_ && this->physicalBody_->isKinematicObject(); }
139            bool isDynamic()   { return this->physicalBody_ && !this->physicalBody_->isStaticOrKinematicObject(); }
140
141            void attach(WorldEntity* object);
142            void detach(WorldEntity* object);
143            WorldEntity* getAttachedObject(unsigned int index) const;
144            inline const std::set<WorldEntity*>& getAttachedObjects() const
145                { return this->children_; }
146
147            inline void attachToParent(WorldEntity* parent)
148                { parent->attach(this); }
149            inline void detachFromParent()
150                { if (this->parent_) { this->parent_->detach(this); } }
151            inline WorldEntity* getParent() const
152                { return this->parent_; }
153
154            void setCollisionRadius(float radius);
155            float getCollisionRadius();
156
157            void setCollisionTypeStr(const std::string& type);
158            std::string getCollisionTypeStr();
159
160            void setMass(float mass);
161            float getMass();
162
163            CollisionType getCollisionType();
164
165        protected:
166            //virtual btCollisionShape* getCollisionShape() = 0;
167
168            void createPhysicalBody();
169            //virtual void attachPhysicalObject(WorldEntity* object);
170            virtual void setCollisionType(CollisionType type);
171
172            Ogre::SceneNode* node_;
173            btRigidBody* physicalBody_;
174
175        private:
176            void updateParent();
177
178            inline void lookAt_xmlport(const Vector3& target)
179                { this->lookAt(target); }
180            inline void setDirection_xmlport(const Vector3& direction)
181                { this->setDirection(direction); }
182            inline void yaw_xmlport(const Degree& angle)
183                { this->yaw(angle); }
184            inline void pitch_xmlport(const Degree& angle)
185                { this->pitch(angle); }
186            inline void roll_xmlport(const Degree& angle)
187                { this->roll(angle); }
188
189            WorldEntity* parent_;
190            unsigned int parentID_;
191            std::set<WorldEntity*> children_;
192    };
193}
194
195#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.