[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> |
---|
| 37 | |
---|
[2192] | 38 | #include "LinearMath/btMotionState.h" |
---|
| 39 | |
---|
[2072] | 40 | #include "network/Synchronisable.h" |
---|
| 41 | #include "core/BaseObject.h" |
---|
| 42 | #include "util/Math.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[2192] | 46 | class _OrxonoxExport WorldEntity : public BaseObject, public network::Synchronisable, public btMotionState |
---|
[2072] | 47 | { |
---|
| 48 | public: |
---|
| 49 | WorldEntity(BaseObject* creator); |
---|
| 50 | virtual ~WorldEntity(); |
---|
| 51 | |
---|
| 52 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
---|
| 53 | void registerVariables(); |
---|
| 54 | |
---|
[2296] | 55 | inline const Ogre::SceneNode* getNode() const |
---|
[2072] | 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 | |
---|
[2303] | 110 | void setScale3D(const Vector3& scale); |
---|
[2072] | 111 | inline void setScale3D(float x, float y, float z) |
---|
[2303] | 112 | { this->setScale3D(Vector3(x, y, z)); } |
---|
[2072] | 113 | inline const Vector3& getScale3D(void) const |
---|
| 114 | { return this->node_->getScale(); } |
---|
| 115 | |
---|
[2303] | 116 | void setScale(float scale) |
---|
| 117 | { this->setScale3D(scale, scale, scale); } |
---|
[2072] | 118 | inline float getScale() const |
---|
| 119 | { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } |
---|
| 120 | |
---|
[2303] | 121 | void scale3D(const Vector3& scale); |
---|
[2072] | 122 | inline void scale3D(float x, float y, float z) |
---|
[2303] | 123 | { this->scale3D(Vector3(x, y, z)); } |
---|
[2072] | 124 | inline void scale(float scale) |
---|
[2303] | 125 | { this->scale3D(scale, scale, scale); } |
---|
[2072] | 126 | |
---|
| 127 | void attach(WorldEntity* object); |
---|
[2303] | 128 | // void attachAsdf(BlinkingBillboard* object); |
---|
[2072] | 129 | void detach(WorldEntity* object); |
---|
| 130 | WorldEntity* getAttachedObject(unsigned int index) const; |
---|
[2303] | 131 | // BlinkingBillboard* getAttachedAsdfObject(unsigned int index) const; |
---|
[2072] | 132 | inline const std::set<WorldEntity*>& getAttachedObjects() const |
---|
| 133 | { return this->children_; } |
---|
| 134 | |
---|
[2296] | 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 | |
---|
[2072] | 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_; |
---|
[2298] | 169 | |
---|
| 170 | |
---|
[2300] | 171 | ///////////// |
---|
| 172 | // Physics // |
---|
| 173 | ///////////// |
---|
| 174 | |
---|
[2298] | 175 | public: |
---|
| 176 | enum CollisionType |
---|
| 177 | { |
---|
| 178 | Dynamic, |
---|
| 179 | Kinematic, |
---|
| 180 | Static, |
---|
| 181 | None |
---|
| 182 | }; |
---|
| 183 | |
---|
[2300] | 184 | bool hasPhysics() const { return getCollisionType() != None; } |
---|
[2298] | 185 | |
---|
[2300] | 186 | CollisionType getCollisionType() const { return this->collisionType_; } |
---|
[2298] | 187 | void setCollisionType(CollisionType type); |
---|
| 188 | |
---|
| 189 | void setCollisionTypeStr(const std::string& type); |
---|
[2300] | 190 | std::string getCollisionTypeStr() const; |
---|
[2298] | 191 | |
---|
[2300] | 192 | bool isStatic() const { return getCollisionType() == Static ; } |
---|
| 193 | bool isKinematic() const { return getCollisionType() == Kinematic; } |
---|
| 194 | bool isDynamic() const { return getCollisionType() == Dynamic ; } |
---|
[2298] | 195 | |
---|
| 196 | void setMass(float mass); |
---|
[2303] | 197 | inline float getMass() const |
---|
| 198 | { return this->mass_; } |
---|
[2298] | 199 | |
---|
[2303] | 200 | void attachCollisionShape(CollisionShape* shape); |
---|
| 201 | CollisionShape* getAttachedCollisionShape(unsigned int index) const; |
---|
[2298] | 202 | |
---|
[2303] | 203 | CollisionShape* getCollisionShape() { return this->collisionShape_; } |
---|
| 204 | btRigidBody* getPhysicalBody() { return this->physicalBody_; } |
---|
| 205 | |
---|
[2298] | 206 | protected: |
---|
| 207 | //virtual btCollisionShape* getCollisionShape() = 0; |
---|
| 208 | //virtual void attachPhysicalObject(WorldEntity* object); |
---|
| 209 | |
---|
[2300] | 210 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
---|
| 211 | bool checkPhysics() const; |
---|
[2298] | 212 | void updateCollisionType(); |
---|
| 213 | |
---|
| 214 | btRigidBody* physicalBody_; |
---|
| 215 | |
---|
| 216 | private: |
---|
[2303] | 217 | void mergeCollisionShape(CollisionShape* shape); |
---|
[2298] | 218 | |
---|
[2303] | 219 | CollisionType collisionType_; |
---|
| 220 | std::vector<CollisionShape*> attachedShapes_; |
---|
| 221 | CollisionShape* collisionShape_; |
---|
| 222 | btScalar mass_; |
---|
[2072] | 223 | }; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | #endif /* _WorldEntity_H__ */ |
---|