Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2303 was 2303, checked in by rgrieder, 15 years ago
  • Added support for attaching physical WorldEntities to each other. Currently you can only add static objects to kinematic/dynamic/static other objects. Future plans involve attaching kinematic objects to static/kinematic ones. All other combinations don't make sense at all.
  • Added CollisionShape, SphereCollisionShape and CompoundCollisionShape

Usage for collision shapes (example):

<LinearEntity collisionType="kinematic">

<attached>

<Model position="0,0,0" scale=10 mesh="ast1.mesh" />
<StaticEntity position="0,10,0" collisionType="static"> # Everything else than "static" fails!

<collisionShapes>

<SphereCollisionShape radius=40 position="10,10,-10"/>
<CompoundCollisionShape position="4,4,4"> # You can also make compound shapes directly

<SphereCollisionShape radius=10/>

</CompoundCollisionShape>

</collisionShapes>

</StaticEntity>

</attached>

</LinearEntity>

  • Property svn:eol-style set to native
File size: 9.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 *   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
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            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            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 attachAsdf(BlinkingBillboard* object);
128            void detach(WorldEntity* object);
129            WorldEntity* getAttachedObject(unsigned int index) const;
130//            BlinkingBillboard* getAttachedAsdfObject(unsigned int index) const;
131            inline const std::set<WorldEntity*>& getAttachedObjects() const
132                { return this->children_; }
133
134            inline void attachOgreObject(Ogre::MovableObject* object)
135                { this->node_->attachObject(object); }
136            inline void detachOgreObject(Ogre::MovableObject* object)
137                { this->node_->detachObject(object); }
138            inline Ogre::MovableObject* detachOgreObject(const Ogre::String& name)
139                { return this->node_->detachObject(name); }
140
141            inline void attachToParent(WorldEntity* parent)
142                { parent->attach(this); }
143            inline void detachFromParent()
144                { if (this->parent_) { this->parent_->detach(this); } }
145            inline WorldEntity* getParent() const
146                { return this->parent_; }
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            WorldEntity* parent_;
166            unsigned int parentID_;
167            std::set<WorldEntity*> children_;
168
169
170        /////////////
171        // Physics //
172        /////////////
173
174        public:
175            enum CollisionType
176            {
177                Dynamic,
178                Kinematic,
179                Static,
180                None
181            };
182
183            bool hasPhysics() const { return getCollisionType() != None; }
184
185            CollisionType getCollisionType() const { return this->collisionType_; }
186            void setCollisionType(CollisionType type);
187
188            void setCollisionTypeStr(const std::string& type);
189            std::string getCollisionTypeStr() const;
190
191            bool isStatic()    const { return getCollisionType() == Static   ; }
192            bool isKinematic() const { return getCollisionType() == Kinematic; }
193            bool isDynamic()   const { return getCollisionType() == Dynamic  ; }
194
195            void setMass(float mass);
196            inline float getMass() const
197                { return this->mass_; }
198
199            void attachCollisionShape(CollisionShape* shape);
200            CollisionShape* getAttachedCollisionShape(unsigned int index) const;
201
202            CollisionShape* getCollisionShape() { return this->collisionShape_; }
203            btRigidBody* getPhysicalBody() { return this->physicalBody_; }
204
205        protected:
206            //virtual btCollisionShape* getCollisionShape() = 0;
207            //virtual void attachPhysicalObject(WorldEntity* object);
208
209            virtual bool isCollisionTypeLegal(CollisionType type) const = 0;
210            bool checkPhysics() const;
211            void updateCollisionType();
212
213            btRigidBody*  physicalBody_;
214
215        private:
216            void mergeCollisionShape(CollisionShape* shape);
217
218            CollisionType                collisionType_;
219            std::vector<CollisionShape*> attachedShapes_;
220            CollisionShape*              collisionShape_;
221            btScalar                     mass_;
222    };
223}
224
225#endif /* _WorldEntity_H__ */
Note: See TracBrowser for help on using the repository browser.