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 | |
---|
44 | namespace 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 | WorldEntity* parent_; |
---|
167 | unsigned int parentID_; |
---|
168 | std::set<WorldEntity*> children_; |
---|
169 | |
---|
170 | |
---|
171 | ///////////// |
---|
172 | // Physics // |
---|
173 | ///////////// |
---|
174 | |
---|
175 | public: |
---|
176 | enum CollisionType |
---|
177 | { |
---|
178 | Dynamic, |
---|
179 | Kinematic, |
---|
180 | Static, |
---|
181 | None |
---|
182 | }; |
---|
183 | |
---|
184 | bool hasPhysics() const { return getCollisionType() != None ; } |
---|
185 | bool isStatic() const { return getCollisionType() == Static ; } |
---|
186 | bool isKinematic() const { return getCollisionType() == Kinematic; } |
---|
187 | bool isDynamic() const { return getCollisionType() == Dynamic ; } |
---|
188 | |
---|
189 | inline CollisionType getCollisionType() const |
---|
190 | { return this->collisionType_; } |
---|
191 | void setCollisionType(CollisionType type); |
---|
192 | |
---|
193 | void setCollisionTypeStr(const std::string& type); |
---|
194 | std::string getCollisionTypeStr() const; |
---|
195 | |
---|
196 | void setMass(float mass); |
---|
197 | inline float getMass() const |
---|
198 | { return this->mass_; } |
---|
199 | |
---|
200 | void attachCollisionShape(CollisionShape* shape); |
---|
201 | CollisionShape* getAttachedCollisionShape(unsigned int index) const; |
---|
202 | |
---|
203 | inline CollisionShape* getCollisionShape() |
---|
204 | { return this->collisionShape_; } |
---|
205 | inline btRigidBody* getPhysicalBody() |
---|
206 | { return this->physicalBody_; } |
---|
207 | |
---|
208 | protected: |
---|
209 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
---|
210 | |
---|
211 | btRigidBody* physicalBody_; |
---|
212 | |
---|
213 | private: |
---|
214 | void updateCollisionType(); |
---|
215 | void mergeCollisionShape(CollisionShape* shape); |
---|
216 | void internalSetMassProps(); |
---|
217 | btVector3 getLocalInertia(btScalar mass) const; |
---|
218 | bool checkPhysics() const; |
---|
219 | void addToPhysicalWorld() const; |
---|
220 | void removeFromPhysicalWorld() const; |
---|
221 | |
---|
222 | CollisionType collisionType_; |
---|
223 | std::vector<CollisionShape*> attachedShapes_; |
---|
224 | CollisionShape* collisionShape_; |
---|
225 | btScalar mass_; |
---|
226 | btScalar childMass_; |
---|
227 | }; |
---|
228 | } |
---|
229 | |
---|
230 | #endif /* _WorldEntity_H__ */ |
---|