[2072] | 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 |
---|
[2304] | 24 | * Reto Grieder (physics) |
---|
[2072] | 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> |
---|
[2192] | 37 | #include "LinearMath/btMotionState.h" |
---|
| 38 | |
---|
[2072] | 39 | #include "network/Synchronisable.h" |
---|
| 40 | #include "core/BaseObject.h" |
---|
| 41 | #include "util/Math.h" |
---|
| 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
[2192] | 45 | class _OrxonoxExport WorldEntity : public BaseObject, public network::Synchronisable, public btMotionState |
---|
[2072] | 46 | { |
---|
| 47 | public: |
---|
| 48 | WorldEntity(BaseObject* creator); |
---|
| 49 | virtual ~WorldEntity(); |
---|
| 50 | |
---|
| 51 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
---|
| 52 | void registerVariables(); |
---|
| 53 | |
---|
[2296] | 54 | inline const Ogre::SceneNode* getNode() const |
---|
[2072] | 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 | |
---|
[2306] | 109 | virtual void setScale3D(const Vector3& scale); |
---|
[2072] | 110 | inline void setScale3D(float x, float y, float z) |
---|
[2303] | 111 | { this->setScale3D(Vector3(x, y, z)); } |
---|
[2072] | 112 | inline const Vector3& getScale3D(void) const |
---|
| 113 | { return this->node_->getScale(); } |
---|
| 114 | |
---|
[2303] | 115 | void setScale(float scale) |
---|
| 116 | { this->setScale3D(scale, scale, scale); } |
---|
[2072] | 117 | inline float getScale() const |
---|
| 118 | { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } |
---|
| 119 | |
---|
[2306] | 120 | virtual void scale3D(const Vector3& scale); |
---|
[2072] | 121 | inline void scale3D(float x, float y, float z) |
---|
[2303] | 122 | { this->scale3D(Vector3(x, y, z)); } |
---|
[2072] | 123 | inline void scale(float scale) |
---|
[2303] | 124 | { this->scale3D(scale, scale, scale); } |
---|
[2072] | 125 | |
---|
| 126 | void attach(WorldEntity* object); |
---|
| 127 | void detach(WorldEntity* object); |
---|
| 128 | WorldEntity* getAttachedObject(unsigned int index) const; |
---|
| 129 | inline const std::set<WorldEntity*>& getAttachedObjects() const |
---|
| 130 | { return this->children_; } |
---|
| 131 | |
---|
[2296] | 132 | inline void attachOgreObject(Ogre::MovableObject* object) |
---|
| 133 | { this->node_->attachObject(object); } |
---|
| 134 | inline void detachOgreObject(Ogre::MovableObject* object) |
---|
| 135 | { this->node_->detachObject(object); } |
---|
| 136 | inline Ogre::MovableObject* detachOgreObject(const Ogre::String& name) |
---|
| 137 | { return this->node_->detachObject(name); } |
---|
| 138 | |
---|
[2072] | 139 | inline void attachToParent(WorldEntity* parent) |
---|
| 140 | { parent->attach(this); } |
---|
| 141 | inline void detachFromParent() |
---|
| 142 | { if (this->parent_) { this->parent_->detach(this); } } |
---|
| 143 | inline WorldEntity* getParent() const |
---|
| 144 | { return this->parent_; } |
---|
| 145 | |
---|
[2423] | 146 | void notifyChildPropsChanged(); |
---|
| 147 | |
---|
[2072] | 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 | |
---|
[2374] | 165 | inline void scaleChanged() |
---|
| 166 | { this->setScale3D(this->getScale3D()); } |
---|
| 167 | |
---|
[2072] | 168 | WorldEntity* parent_; |
---|
| 169 | unsigned int parentID_; |
---|
| 170 | std::set<WorldEntity*> children_; |
---|
[2298] | 171 | |
---|
| 172 | |
---|
[2300] | 173 | ///////////// |
---|
| 174 | // Physics // |
---|
| 175 | ///////////// |
---|
| 176 | |
---|
[2298] | 177 | public: |
---|
| 178 | enum CollisionType |
---|
| 179 | { |
---|
| 180 | Dynamic, |
---|
| 181 | Kinematic, |
---|
| 182 | Static, |
---|
| 183 | None |
---|
| 184 | }; |
---|
| 185 | |
---|
[2423] | 186 | bool hasPhysics() const { return getCollisionType() != None ; } |
---|
| 187 | bool isStatic() const { return getCollisionType() == Static ; } |
---|
| 188 | bool isKinematic() const { return getCollisionType() == Kinematic; } |
---|
| 189 | bool isDynamic() const { return getCollisionType() == Dynamic ; } |
---|
| 190 | bool isPhysicsActive() const { return this->bPhysicsActive_; } |
---|
| 191 | bool addedToPhysicalWorld() const; |
---|
[2298] | 192 | |
---|
[2423] | 193 | void activatePhysics(); |
---|
| 194 | void deactivatePhysics(); |
---|
| 195 | |
---|
[2306] | 196 | inline CollisionType getCollisionType() const |
---|
| 197 | { return this->collisionType_; } |
---|
[2298] | 198 | void setCollisionType(CollisionType type); |
---|
| 199 | |
---|
| 200 | void setCollisionTypeStr(const std::string& type); |
---|
[2300] | 201 | std::string getCollisionTypeStr() const; |
---|
[2298] | 202 | |
---|
| 203 | void setMass(float mass); |
---|
[2303] | 204 | inline float getMass() const |
---|
| 205 | { return this->mass_; } |
---|
[2423] | 206 | inline float getTotalMass() const |
---|
| 207 | { return this->mass_ + this->childrenMass_; } |
---|
[2298] | 208 | |
---|
[2423] | 209 | void attachCollisionShape(CollisionShape* shape); |
---|
| 210 | void detachCollisionShape(CollisionShape* shape); |
---|
[2303] | 211 | CollisionShape* getAttachedCollisionShape(unsigned int index) const; |
---|
[2298] | 212 | |
---|
[2374] | 213 | inline CompoundCollisionShape* getCollisionShape() |
---|
[2306] | 214 | { return this->collisionShape_; } |
---|
| 215 | inline btRigidBody* getPhysicalBody() |
---|
| 216 | { return this->physicalBody_; } |
---|
[2303] | 217 | |
---|
[2423] | 218 | void notifyCollisionShapeChanged(); |
---|
| 219 | void notifyChildMassChanged(); |
---|
| 220 | |
---|
[2298] | 221 | protected: |
---|
[2300] | 222 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
---|
[2298] | 223 | |
---|
| 224 | btRigidBody* physicalBody_; |
---|
| 225 | |
---|
| 226 | private: |
---|
[2306] | 227 | void updateCollisionType(); |
---|
[2423] | 228 | void recalculatePhysicsProps(); |
---|
[2298] | 229 | |
---|
[2374] | 230 | // network callbacks |
---|
| 231 | void collisionTypeChanged(); |
---|
| 232 | void massChanged(); |
---|
[2423] | 233 | void physicsActivityChanged(); |
---|
[2374] | 234 | |
---|
[2303] | 235 | CollisionType collisionType_; |
---|
[2374] | 236 | CollisionType collisionTypeSynchronised_; |
---|
[2423] | 237 | bool bPhysicsActive_; |
---|
| 238 | bool bPhysicsActiveSynchronised_; |
---|
[2374] | 239 | CompoundCollisionShape* collisionShape_; |
---|
[2303] | 240 | btScalar mass_; |
---|
[2423] | 241 | btScalar childrenMass_; |
---|
[2072] | 242 | }; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | #endif /* _WorldEntity_H__ */ |
---|