| 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 "network/Synchronisable.h" |
|---|
| 43 | #include "core/BaseObject.h" |
|---|
| 44 | #include "util/Math.h" |
|---|
| 45 | |
|---|
| 46 | namespace orxonox |
|---|
| 47 | { |
|---|
| 48 | class _OrxonoxExport WorldEntity : public BaseObject, public network::Synchronisable, public btMotionState |
|---|
| 49 | { |
|---|
| 50 | public: |
|---|
| 51 | WorldEntity(BaseObject* creator); |
|---|
| 52 | virtual ~WorldEntity(); |
|---|
| 53 | |
|---|
| 54 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
|---|
| 55 | void registerVariables(); |
|---|
| 56 | |
|---|
| 57 | inline const Ogre::SceneNode* getNode() const |
|---|
| 58 | { return this->node_; } |
|---|
| 59 | |
|---|
| 60 | static const Vector3 FRONT; |
|---|
| 61 | static const Vector3 BACK; |
|---|
| 62 | static const Vector3 LEFT; |
|---|
| 63 | static const Vector3 RIGHT; |
|---|
| 64 | static const Vector3 DOWN; |
|---|
| 65 | static const Vector3 UP; |
|---|
| 66 | |
|---|
| 67 | virtual void setPosition(const Vector3& position) = 0; |
|---|
| 68 | inline void setPosition(float x, float y, float z) |
|---|
| 69 | { this->setPosition(Vector3(x, y, z)); } |
|---|
| 70 | const Vector3& getPosition() const; |
|---|
| 71 | const Vector3& getWorldPosition() const; |
|---|
| 72 | |
|---|
| 73 | void translate(const Vector3& distance, TransformSpace::Space relativeTo = TransformSpace::Parent); |
|---|
| 74 | inline void translate(float x, float y, float z, TransformSpace::Space relativeTo = TransformSpace::Parent) |
|---|
| 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 | const Quaternion& getOrientation() const; |
|---|
| 85 | const Quaternion& getWorldOrientation() const; |
|---|
| 86 | |
|---|
| 87 | void rotate(const Quaternion& rotation, TransformSpace::Space relativeTo = TransformSpace::Local); |
|---|
| 88 | inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local) |
|---|
| 89 | { this->rotate(Quaternion(angle, axis), relativeTo); } |
|---|
| 90 | |
|---|
| 91 | inline void yaw(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local) |
|---|
| 92 | { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); } |
|---|
| 93 | inline void pitch(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local) |
|---|
| 94 | { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); } |
|---|
| 95 | inline void roll(const Degree& angle, TransformSpace::Space relativeTo = TransformSpace::Local) |
|---|
| 96 | { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); } |
|---|
| 97 | |
|---|
| 98 | void lookAt(const Vector3& target, TransformSpace::Space relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
|---|
| 99 | void setDirection(const Vector3& direction, TransformSpace::Space relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
|---|
| 100 | inline void setDirection(float x, float y, float z, TransformSpace::Space relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) |
|---|
| 101 | { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); } |
|---|
| 102 | |
|---|
| 103 | virtual void setScale3D(const Vector3& scale); |
|---|
| 104 | inline void setScale3D(float x, float y, float z) |
|---|
| 105 | { this->setScale3D(Vector3(x, y, z)); } |
|---|
| 106 | const Vector3& getScale3D(void) const; |
|---|
| 107 | |
|---|
| 108 | void setScale(float scale) |
|---|
| 109 | { this->setScale3D(scale, scale, scale); } |
|---|
| 110 | inline float getScale() const |
|---|
| 111 | { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } |
|---|
| 112 | |
|---|
| 113 | inline void scale3D(const Vector3& scale) |
|---|
| 114 | { this->setScale3D(this->getScale3D() * scale); } |
|---|
| 115 | inline void scale3D(float x, float y, float z) |
|---|
| 116 | { this->scale3D(Vector3(x, y, z)); } |
|---|
| 117 | inline void scale(float scale) |
|---|
| 118 | { this->scale3D(scale, scale, scale); } |
|---|
| 119 | |
|---|
| 120 | void attach(WorldEntity* object); |
|---|
| 121 | void detach(WorldEntity* object); |
|---|
| 122 | WorldEntity* getAttachedObject(unsigned int index) const; |
|---|
| 123 | inline const std::set<WorldEntity*>& getAttachedObjects() const |
|---|
| 124 | { return this->children_; } |
|---|
| 125 | |
|---|
| 126 | void attachOgreObject(Ogre::MovableObject* object); |
|---|
| 127 | void detachOgreObject(Ogre::MovableObject* object); |
|---|
| 128 | Ogre::MovableObject* detachOgreObject(const Ogre::String& name); |
|---|
| 129 | |
|---|
| 130 | inline void attachToParent(WorldEntity* parent) |
|---|
| 131 | { parent->attach(this); } |
|---|
| 132 | inline void detachFromParent() |
|---|
| 133 | { if (this->parent_) { this->parent_->detach(this); } } |
|---|
| 134 | inline WorldEntity* getParent() const |
|---|
| 135 | { return this->parent_; } |
|---|
| 136 | |
|---|
| 137 | void notifyChildPropsChanged(); |
|---|
| 138 | |
|---|
| 139 | protected: |
|---|
| 140 | Ogre::SceneNode* node_; |
|---|
| 141 | |
|---|
| 142 | private: |
|---|
| 143 | inline void lookAt_xmlport(const Vector3& target) |
|---|
| 144 | { this->lookAt(target); } |
|---|
| 145 | inline void setDirection_xmlport(const Vector3& direction) |
|---|
| 146 | { this->setDirection(direction); } |
|---|
| 147 | inline void yaw_xmlport(const Degree& angle) |
|---|
| 148 | { this->yaw(angle); } |
|---|
| 149 | inline void pitch_xmlport(const Degree& angle) |
|---|
| 150 | { this->pitch(angle); } |
|---|
| 151 | inline void roll_xmlport(const Degree& angle) |
|---|
| 152 | { this->roll(angle); } |
|---|
| 153 | |
|---|
| 154 | // network callbacks |
|---|
| 155 | void parentChanged(); |
|---|
| 156 | inline void scaleChanged() |
|---|
| 157 | { this->setScale3D(this->getScale3D()); } |
|---|
| 158 | |
|---|
| 159 | WorldEntity* parent_; |
|---|
| 160 | unsigned int parentID_; |
|---|
| 161 | std::set<WorldEntity*> children_; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | ///////////// |
|---|
| 165 | // Physics // |
|---|
| 166 | ///////////// |
|---|
| 167 | |
|---|
| 168 | public: |
|---|
| 169 | enum CollisionType |
|---|
| 170 | { |
|---|
| 171 | Dynamic, |
|---|
| 172 | Kinematic, |
|---|
| 173 | Static, |
|---|
| 174 | None |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | bool hasPhysics() const { return getCollisionType() != None ; } |
|---|
| 178 | bool isStatic() const { return getCollisionType() == Static ; } |
|---|
| 179 | bool isKinematic() const { return getCollisionType() == Kinematic; } |
|---|
| 180 | bool isDynamic() const { return getCollisionType() == Dynamic ; } |
|---|
| 181 | bool isPhysicsActive() const { return this->bPhysicsActive_; } |
|---|
| 182 | bool addedToPhysicalWorld() const; |
|---|
| 183 | |
|---|
| 184 | void activatePhysics(); |
|---|
| 185 | void deactivatePhysics(); |
|---|
| 186 | |
|---|
| 187 | inline CollisionType getCollisionType() const |
|---|
| 188 | { return this->collisionType_; } |
|---|
| 189 | void setCollisionType(CollisionType type); |
|---|
| 190 | |
|---|
| 191 | void setCollisionTypeStr(const std::string& type); |
|---|
| 192 | std::string getCollisionTypeStr() const; |
|---|
| 193 | |
|---|
| 194 | inline void setMass(float mass) |
|---|
| 195 | { this->mass_ = mass; recalculateMassProps(); } |
|---|
| 196 | inline float getMass() const |
|---|
| 197 | { return this->mass_; } |
|---|
| 198 | |
|---|
| 199 | inline float getTotalMass() const |
|---|
| 200 | { return this->mass_ + this->childrenMass_; } |
|---|
| 201 | |
|---|
| 202 | inline void setRestitution(float restitution) |
|---|
| 203 | { this->restitution_ = restitution; resetPhysicsProps(); } |
|---|
| 204 | inline float getRestitution() const |
|---|
| 205 | { return this->restitution_; } |
|---|
| 206 | |
|---|
| 207 | inline void setAngularFactor(float angularFactor) |
|---|
| 208 | { this->angularFactor_ = angularFactor; resetPhysicsProps(); } |
|---|
| 209 | inline float getAngularFactor() const |
|---|
| 210 | { return this->angularFactor_; } |
|---|
| 211 | |
|---|
| 212 | inline void setLinearDamping(float linearDamping) |
|---|
| 213 | { this->linearDamping_ = linearDamping; resetPhysicsProps(); } |
|---|
| 214 | inline float getLinearDamping() const |
|---|
| 215 | { return this->linearDamping_; } |
|---|
| 216 | |
|---|
| 217 | inline void setAngularDamping(float angularDamping) |
|---|
| 218 | { this->angularDamping_ = angularDamping; resetPhysicsProps(); } |
|---|
| 219 | inline float getAngularDamping() const |
|---|
| 220 | { return this->angularDamping_; } |
|---|
| 221 | |
|---|
| 222 | inline void setFriction(float friction) |
|---|
| 223 | { this->friction_ = friction; resetPhysicsProps(); } |
|---|
| 224 | inline float getFriction() const |
|---|
| 225 | { return this->friction_; } |
|---|
| 226 | |
|---|
| 227 | void attachCollisionShape(CollisionShape* shape); |
|---|
| 228 | void detachCollisionShape(CollisionShape* shape); |
|---|
| 229 | CollisionShape* getAttachedCollisionShape(unsigned int index) const; |
|---|
| 230 | |
|---|
| 231 | inline CompoundCollisionShape* getCollisionShape() |
|---|
| 232 | { return this->collisionShape_; } |
|---|
| 233 | inline btRigidBody* getPhysicalBody() |
|---|
| 234 | { return this->physicalBody_; } |
|---|
| 235 | |
|---|
| 236 | void notifyCollisionShapeChanged(); |
|---|
| 237 | void notifyChildMassChanged(); |
|---|
| 238 | |
|---|
| 239 | protected: |
|---|
| 240 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
|---|
| 241 | |
|---|
| 242 | btRigidBody* physicalBody_; |
|---|
| 243 | |
|---|
| 244 | private: |
|---|
| 245 | void updateCollisionType(); |
|---|
| 246 | void recalculateMassProps(); |
|---|
| 247 | void resetPhysicsProps(); |
|---|
| 248 | |
|---|
| 249 | // network callbacks |
|---|
| 250 | void collisionTypeChanged(); |
|---|
| 251 | void physicsActivityChanged(); |
|---|
| 252 | inline void massChanged() |
|---|
| 253 | { this->setMass(this->mass_); } |
|---|
| 254 | inline void restitutionChanged() |
|---|
| 255 | { this->setRestitution(this->restitution_); } |
|---|
| 256 | inline void angularFactorChanged() |
|---|
| 257 | { this->setAngularFactor(this->angularFactor_); } |
|---|
| 258 | inline void linearDampingChanged() |
|---|
| 259 | { this->setLinearDamping(this->linearDamping_); } |
|---|
| 260 | inline void angularDampingChanged() |
|---|
| 261 | { this->setAngularDamping(this->angularDamping_); } |
|---|
| 262 | inline void frictionChanged() |
|---|
| 263 | { this->setFriction(this->friction_); } |
|---|
| 264 | |
|---|
| 265 | CollisionType collisionType_; |
|---|
| 266 | CollisionType collisionTypeSynchronised_; |
|---|
| 267 | bool bPhysicsActive_; |
|---|
| 268 | bool bPhysicsActiveSynchronised_; |
|---|
| 269 | CompoundCollisionShape* collisionShape_; |
|---|
| 270 | btScalar mass_; |
|---|
| 271 | btScalar restitution_; |
|---|
| 272 | btScalar angularFactor_; |
|---|
| 273 | btScalar linearDamping_; |
|---|
| 274 | btScalar angularDamping_; |
|---|
| 275 | btScalar friction_; |
|---|
| 276 | btScalar childrenMass_; |
|---|
| 277 | }; |
|---|
| 278 | |
|---|
| 279 | // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here. |
|---|
| 280 | #ifdef _NDEBUG |
|---|
| 281 | inline const Vector3& WorldEntity::getPosition() const |
|---|
| 282 | { return this->node_->getPosition(); } |
|---|
| 283 | inline const Quaternion& WorldEntity::getOrientation() const |
|---|
| 284 | { return this->node_->getrOrientation(); } |
|---|
| 285 | inline const Vector3& WorldEntity::getScale3D(void) const |
|---|
| 286 | { return this->node_->getScale(); } |
|---|
| 287 | #endif |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | #endif /* _WorldEntity_H__ */ |
|---|