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 | #ifdef _NDEBUG |
---|
36 | #include <OgreSceneNode.h> |
---|
37 | #else |
---|
38 | #include <OgrePrerequisites.h> |
---|
39 | #endif |
---|
40 | #include "LinearMath/btMotionState.h" |
---|
41 | |
---|
42 | #include "util/Math.h" |
---|
43 | #include "core/BaseObject.h" |
---|
44 | #include "network/synchronisable/Synchronisable.h" |
---|
45 | #include "objects/collisionshapes/CompoundCollisionShape.h" |
---|
46 | |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable, public btMotionState |
---|
50 | { |
---|
51 | friend class Scene; |
---|
52 | |
---|
53 | public: |
---|
54 | WorldEntity(BaseObject* creator); |
---|
55 | virtual ~WorldEntity(); |
---|
56 | |
---|
57 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
---|
58 | void registerVariables(); |
---|
59 | |
---|
60 | inline const Ogre::SceneNode* getNode() const |
---|
61 | { return this->node_; } |
---|
62 | |
---|
63 | static const Vector3 FRONT; |
---|
64 | static const Vector3 BACK; |
---|
65 | static const Vector3 LEFT; |
---|
66 | static const Vector3 RIGHT; |
---|
67 | static const Vector3 DOWN; |
---|
68 | static const Vector3 UP; |
---|
69 | |
---|
70 | virtual void setPosition(const Vector3& position) = 0; |
---|
71 | inline void setPosition(float x, float y, float z) |
---|
72 | { this->setPosition(Vector3(x, y, z)); } |
---|
73 | const Vector3& getPosition() const; |
---|
74 | const Vector3& getWorldPosition() const; |
---|
75 | |
---|
76 | void translate(const Vector3& distance, TransformSpace::Enum relativeTo = TransformSpace::Parent); |
---|
77 | inline void translate(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::Parent) |
---|
78 | { this->translate(Vector3(x, y, z), relativeTo); } |
---|
79 | |
---|
80 | virtual inline const Vector3& getVelocity() const |
---|
81 | { return Vector3::ZERO; } |
---|
82 | |
---|
83 | virtual void setOrientation(const Quaternion& orientation) = 0; |
---|
84 | inline void setOrientation(float w, float x, float y, float z) |
---|
85 | { this->setOrientation(Quaternion(w, x, y, z)); } |
---|
86 | inline void setOrientation(const Vector3& axis, const Radian& angle) |
---|
87 | { this->setOrientation(Quaternion(angle, axis)); } |
---|
88 | inline void setOrientation(const Vector3& axis, const Degree& angle) |
---|
89 | { this->setOrientation(Quaternion(angle, axis)); } |
---|
90 | const Quaternion& getOrientation() const; |
---|
91 | const Quaternion& getWorldOrientation() const; |
---|
92 | |
---|
93 | void rotate(const Quaternion& rotation, TransformSpace::Enum relativeTo = TransformSpace::Local); |
---|
94 | inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) |
---|
95 | { this->rotate(Quaternion(angle, axis), relativeTo); } |
---|
96 | |
---|
97 | inline void yaw(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) |
---|
98 | { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); } |
---|
99 | inline void pitch(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) |
---|
100 | { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); } |
---|
101 | inline void roll(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) |
---|
102 | { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); } |
---|
103 | |
---|
104 | void lookAt(const Vector3& target, TransformSpace::Enum relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
---|
105 | void setDirection(const Vector3& direction, TransformSpace::Enum relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
---|
106 | inline void setDirection(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::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 | const Vector3& getScale3D(void) const; |
---|
113 | const Vector3& getWorldScale3D() const; |
---|
114 | |
---|
115 | inline 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 | float getWorldScale() const; |
---|
120 | |
---|
121 | inline void scale3D(const Vector3& scale) |
---|
122 | { this->setScale3D(this->getScale3D() * scale); } |
---|
123 | inline void scale3D(float x, float y, float z) |
---|
124 | { this->scale3D(Vector3(x, y, z)); } |
---|
125 | inline void scale(float scale) |
---|
126 | { this->scale3D(scale, scale, scale); } |
---|
127 | |
---|
128 | virtual void changedScale() {} |
---|
129 | |
---|
130 | void attach(WorldEntity* object); |
---|
131 | void detach(WorldEntity* object); |
---|
132 | WorldEntity* getAttachedObject(unsigned int index); |
---|
133 | inline const std::set<WorldEntity*>& getAttachedObjects() const |
---|
134 | { return this->children_; } |
---|
135 | |
---|
136 | void attachOgreObject(Ogre::MovableObject* object); |
---|
137 | void detachOgreObject(Ogre::MovableObject* object); |
---|
138 | Ogre::MovableObject* detachOgreObject(const Ogre::String& name); |
---|
139 | |
---|
140 | inline void attachToParent(WorldEntity* parent) |
---|
141 | { parent->attach(this); } |
---|
142 | inline void detachFromParent() |
---|
143 | { if (this->parent_) { this->parent_->detach(this); } } |
---|
144 | inline WorldEntity* getParent() const |
---|
145 | { return this->parent_; } |
---|
146 | |
---|
147 | void attachNode(Ogre::SceneNode* node); |
---|
148 | void detachNode(Ogre::SceneNode* node); |
---|
149 | void attachToNode(Ogre::SceneNode* node); |
---|
150 | void detachFromNode(Ogre::SceneNode* node); |
---|
151 | |
---|
152 | void notifyChildPropsChanged(); |
---|
153 | |
---|
154 | protected: |
---|
155 | Ogre::SceneNode* node_; |
---|
156 | |
---|
157 | private: |
---|
158 | inline void lookAt_xmlport(const Vector3& target) |
---|
159 | { this->lookAt(target); } |
---|
160 | inline void setDirection_xmlport(const Vector3& direction) |
---|
161 | { this->setDirection(direction); } |
---|
162 | inline void yaw_xmlport(const Degree& angle) |
---|
163 | { this->yaw(angle); } |
---|
164 | inline void pitch_xmlport(const Degree& angle) |
---|
165 | { this->pitch(angle); } |
---|
166 | inline void roll_xmlport(const Degree& angle) |
---|
167 | { this->roll(angle); } |
---|
168 | |
---|
169 | // network callbacks |
---|
170 | void parentChanged(); |
---|
171 | inline void scaleChanged() |
---|
172 | { this->setScale3D(this->getScale3D()); } |
---|
173 | |
---|
174 | WorldEntity* parent_; |
---|
175 | unsigned int parentID_; |
---|
176 | std::set<WorldEntity*> children_; |
---|
177 | |
---|
178 | |
---|
179 | ///////////// |
---|
180 | // Physics // |
---|
181 | ///////////// |
---|
182 | |
---|
183 | public: |
---|
184 | enum CollisionType |
---|
185 | { |
---|
186 | Dynamic, |
---|
187 | Kinematic, |
---|
188 | Static, |
---|
189 | None |
---|
190 | }; |
---|
191 | |
---|
192 | bool hasPhysics() const { return getCollisionType() != None ; } |
---|
193 | bool isStatic() const { return getCollisionType() == Static ; } |
---|
194 | bool isKinematic() const { return getCollisionType() == Kinematic; } |
---|
195 | bool isDynamic() const { return getCollisionType() == Dynamic ; } |
---|
196 | bool isPhysicsActive() const { return this->bPhysicsActive_; } |
---|
197 | bool addedToPhysicalWorld() const; |
---|
198 | |
---|
199 | void activatePhysics(); |
---|
200 | void deactivatePhysics(); |
---|
201 | |
---|
202 | inline CollisionType getCollisionType() const |
---|
203 | { return this->collisionType_; } |
---|
204 | void setCollisionType(CollisionType type); |
---|
205 | |
---|
206 | void setCollisionTypeStr(const std::string& type); |
---|
207 | std::string getCollisionTypeStr() const; |
---|
208 | |
---|
209 | inline void setMass(float mass) |
---|
210 | { this->mass_ = mass; recalculateMassProps(); } |
---|
211 | inline float getMass() const |
---|
212 | { return this->mass_; } |
---|
213 | |
---|
214 | inline float getTotalMass() const |
---|
215 | { return this->mass_ + this->childrenMass_; } |
---|
216 | |
---|
217 | inline const btVector3& getLocalInertia() const |
---|
218 | { return this->localInertia_; } |
---|
219 | |
---|
220 | inline void setRestitution(float restitution) |
---|
221 | { this->restitution_ = restitution; internalSetPhysicsProps(); } |
---|
222 | inline float getRestitution() const |
---|
223 | { return this->restitution_; } |
---|
224 | |
---|
225 | inline void setAngularFactor(float angularFactor) |
---|
226 | { this->angularFactor_ = angularFactor; internalSetPhysicsProps(); } |
---|
227 | inline float getAngularFactor() const |
---|
228 | { return this->angularFactor_; } |
---|
229 | |
---|
230 | inline void setLinearDamping(float linearDamping) |
---|
231 | { this->linearDamping_ = linearDamping; internalSetPhysicsProps(); } |
---|
232 | inline float getLinearDamping() const |
---|
233 | { return this->linearDamping_; } |
---|
234 | |
---|
235 | inline void setAngularDamping(float angularDamping) |
---|
236 | { this->angularDamping_ = angularDamping; internalSetPhysicsProps(); } |
---|
237 | inline float getAngularDamping() const |
---|
238 | { return this->angularDamping_; } |
---|
239 | |
---|
240 | inline void setFriction(float friction) |
---|
241 | { this->friction_ = friction; internalSetPhysicsProps(); } |
---|
242 | inline float getFriction() const |
---|
243 | { return this->friction_; } |
---|
244 | |
---|
245 | void attachCollisionShape(CollisionShape* shape); |
---|
246 | void detachCollisionShape(CollisionShape* shape); |
---|
247 | CollisionShape* getAttachedCollisionShape(unsigned int index); |
---|
248 | |
---|
249 | void notifyCollisionShapeChanged(); |
---|
250 | void notifyChildMassChanged(); |
---|
251 | |
---|
252 | virtual inline bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) |
---|
253 | { return false; } /* With false, Bullet assumes no modification to the collision objects. */ |
---|
254 | |
---|
255 | inline void enableCollisionCallback() |
---|
256 | { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); } |
---|
257 | inline void disableCollisionCallback() |
---|
258 | { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); } |
---|
259 | inline bool isCollisionCallbackActive() const |
---|
260 | { return this->bCollisionCallbackActive_; } |
---|
261 | |
---|
262 | protected: |
---|
263 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
---|
264 | |
---|
265 | btRigidBody* physicalBody_; |
---|
266 | |
---|
267 | private: |
---|
268 | void updateCollisionType(); |
---|
269 | void recalculateMassProps(); |
---|
270 | void internalSetPhysicsProps(); |
---|
271 | |
---|
272 | bool notifyBeingAttached(WorldEntity* newParent); |
---|
273 | void notifyDetached(); |
---|
274 | |
---|
275 | // network callbacks |
---|
276 | void collisionTypeChanged(); |
---|
277 | void physicsActivityChanged(); |
---|
278 | void collisionCallbackActivityChanged(); |
---|
279 | inline void massChanged() |
---|
280 | { this->setMass(this->mass_); } |
---|
281 | inline void restitutionChanged() |
---|
282 | { this->setRestitution(this->restitution_); } |
---|
283 | inline void angularFactorChanged() |
---|
284 | { this->setAngularFactor(this->angularFactor_); } |
---|
285 | inline void linearDampingChanged() |
---|
286 | { this->setLinearDamping(this->linearDamping_); } |
---|
287 | inline void angularDampingChanged() |
---|
288 | { this->setAngularDamping(this->angularDamping_); } |
---|
289 | inline void frictionChanged() |
---|
290 | { this->setFriction(this->friction_); } |
---|
291 | |
---|
292 | CollisionType collisionType_; |
---|
293 | CollisionType collisionTypeSynchronised_; |
---|
294 | bool bPhysicsActive_; |
---|
295 | bool bPhysicsActiveSynchronised_; |
---|
296 | bool bPhysicsActiveBeforeAttaching_; |
---|
297 | CompoundCollisionShape collisionShape_; |
---|
298 | btScalar mass_; |
---|
299 | btVector3 localInertia_; |
---|
300 | btScalar restitution_; |
---|
301 | btScalar angularFactor_; |
---|
302 | btScalar linearDamping_; |
---|
303 | btScalar angularDamping_; |
---|
304 | btScalar friction_; |
---|
305 | btScalar childrenMass_; |
---|
306 | bool bCollisionCallbackActive_; |
---|
307 | }; |
---|
308 | |
---|
309 | // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here. |
---|
310 | #ifdef _NDEBUG |
---|
311 | inline const Vector3& WorldEntity::getPosition() const |
---|
312 | { return this->node_->getPosition(); } |
---|
313 | inline const Quaternion& WorldEntity::getOrientation() const |
---|
314 | { return this->node_->getrOrientation(); } |
---|
315 | inline const Vector3& WorldEntity::getScale3D(void) const |
---|
316 | { return this->node_->getScale(); } |
---|
317 | #endif |
---|
318 | |
---|
319 | SUPER_FUNCTION(5, WorldEntity, changedScale, false); |
---|
320 | } |
---|
321 | |
---|
322 | #endif /* _WorldEntity_H__ */ |
---|