[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 |
---|
[2662] | 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 | |
---|
[3196] | 35 | #ifdef ORXONOX_RELEASE |
---|
| 36 | # include <OgreSceneNode.h> |
---|
[2662] | 37 | #endif |
---|
[3196] | 38 | #include <LinearMath/btMotionState.h> |
---|
[10726] | 39 | #include <LinearMath/btAlignedAllocator.h> |
---|
[2072] | 40 | |
---|
[2662] | 41 | #include "util/Math.h" |
---|
[3196] | 42 | #include "util/OgreForwardRefs.h" |
---|
[2072] | 43 | #include "core/BaseObject.h" |
---|
[2662] | 44 | #include "network/synchronisable/Synchronisable.h" |
---|
[2072] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[2662] | 48 | /** |
---|
| 49 | @brief |
---|
| 50 | The WorldEntity represents everything that can be put in a Scene at a certain location. |
---|
| 51 | |
---|
| 52 | It is supposed to be the base class of everything you would call an 'object' in a Scene. |
---|
| 53 | The class itself is abstract which means you cannot use it directly. You may use StaticEntity |
---|
| 54 | as the simplest derivative or (derived from MobileEntity) MovableEntity and ControllableEntity |
---|
| 55 | as more advanced ones. |
---|
| 56 | |
---|
| 57 | The basic task of the WorldEntity is provide a location, a direction and a scaling and the possibility |
---|
[6417] | 58 | to create an entire hierarchy of derived objects. |
---|
[2662] | 59 | It is also the basis for the physics interface to the Bullet physics engine. |
---|
| 60 | Every WorldEntity can have a specific collision type: @see CollisionType |
---|
| 61 | This would then imply that every scene object could have any collision type. To limit this, you can always |
---|
| 62 | override this->isCollisionTypeLegal(CollisionType). Return false if the collision type is not supported |
---|
| 63 | for a specific object. |
---|
| 64 | There is also support for attaching WorldEntities with physics to each other. Currently, the collision shape |
---|
| 65 | of both objects simply get merged into one larger shape (for static collision type). |
---|
[6417] | 66 | The physical body that is internally stored and administrated has the following supported properties: |
---|
| 67 | - Restitution, angular factor, linear damping, angular damping, friction, mass and collision shape. |
---|
[2662] | 68 | You can get more information at the corresponding set function. |
---|
| 69 | |
---|
| 70 | Collision shapes: These are controlled by the internal WorldEntityCollisionShape. @see WorldEntityCollisionShape. |
---|
| 71 | */ |
---|
[6501] | 72 | class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable, public btMotionState |
---|
[2072] | 73 | { |
---|
[2662] | 74 | friend class Scene; |
---|
| 75 | |
---|
[2072] | 76 | public: |
---|
[10726] | 77 | BT_DECLARE_ALIGNED_ALLOCATOR(); |
---|
| 78 | |
---|
[3196] | 79 | // Define our own transform space enum to avoid Ogre includes here |
---|
| 80 | /** |
---|
| 81 | @brief |
---|
| 82 | Enumeration denoting the spaces which a transform can be relative to. |
---|
| 83 | */ |
---|
[11071] | 84 | enum class TransformSpace |
---|
[3196] | 85 | { |
---|
| 86 | //! Transform is relative to the local space |
---|
| 87 | Local, |
---|
| 88 | //! Transform is relative to the space of the parent node |
---|
| 89 | Parent, |
---|
| 90 | //! Transform is relative to world space |
---|
| 91 | World |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | public: |
---|
[9667] | 95 | WorldEntity(Context* context); |
---|
[2072] | 96 | virtual ~WorldEntity(); |
---|
| 97 | |
---|
[11071] | 98 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override; |
---|
[2072] | 99 | |
---|
[2662] | 100 | inline const Ogre::SceneNode* getNode() const |
---|
[2072] | 101 | { return this->node_; } |
---|
| 102 | |
---|
| 103 | static const Vector3 FRONT; |
---|
| 104 | static const Vector3 BACK; |
---|
| 105 | static const Vector3 LEFT; |
---|
| 106 | static const Vector3 RIGHT; |
---|
| 107 | static const Vector3 DOWN; |
---|
| 108 | static const Vector3 UP; |
---|
[7163] | 109 | |
---|
[11071] | 110 | virtual void changedActivity(void) override; |
---|
| 111 | virtual void changedVisibility(void) override; |
---|
[2072] | 112 | |
---|
[12028] | 113 | inline std::string getID(void) |
---|
| 114 | { return this->id_; } |
---|
| 115 | |
---|
| 116 | inline void setID(std::string id) |
---|
| 117 | { this->id_ = id; } |
---|
| 118 | |
---|
[2072] | 119 | virtual void setPosition(const Vector3& position) = 0; |
---|
| 120 | inline void setPosition(float x, float y, float z) |
---|
| 121 | { this->setPosition(Vector3(x, y, z)); } |
---|
[2662] | 122 | const Vector3& getPosition() const; |
---|
| 123 | const Vector3& getWorldPosition() const; |
---|
[2072] | 124 | |
---|
[11071] | 125 | void translate(const Vector3& distance, TransformSpace relativeTo = TransformSpace::Parent); |
---|
| 126 | inline void translate(float x, float y, float z, TransformSpace relativeTo = TransformSpace::Parent) |
---|
[2072] | 127 | { this->translate(Vector3(x, y, z), relativeTo); } |
---|
| 128 | |
---|
[2662] | 129 | virtual inline const Vector3& getVelocity() const |
---|
| 130 | { return Vector3::ZERO; } |
---|
| 131 | |
---|
[2072] | 132 | virtual void setOrientation(const Quaternion& orientation) = 0; |
---|
| 133 | inline void setOrientation(float w, float x, float y, float z) |
---|
| 134 | { this->setOrientation(Quaternion(w, x, y, z)); } |
---|
| 135 | inline void setOrientation(const Vector3& axis, const Radian& angle) |
---|
| 136 | { this->setOrientation(Quaternion(angle, axis)); } |
---|
| 137 | inline void setOrientation(const Vector3& axis, const Degree& angle) |
---|
| 138 | { this->setOrientation(Quaternion(angle, axis)); } |
---|
[2662] | 139 | const Quaternion& getOrientation() const; |
---|
| 140 | const Quaternion& getWorldOrientation() const; |
---|
[2072] | 141 | |
---|
[11071] | 142 | void rotate(const Quaternion& rotation, TransformSpace relativeTo = TransformSpace::Local); |
---|
| 143 | inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace relativeTo = TransformSpace::Local) |
---|
[2072] | 144 | { this->rotate(Quaternion(angle, axis), relativeTo); } |
---|
| 145 | |
---|
[11071] | 146 | inline void yaw(const Degree& angle, TransformSpace relativeTo = TransformSpace::Local) |
---|
[2662] | 147 | { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); } |
---|
[11071] | 148 | inline void pitch(const Degree& angle, TransformSpace relativeTo = TransformSpace::Local) |
---|
[2662] | 149 | { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); } |
---|
[11071] | 150 | inline void roll(const Degree& angle, TransformSpace relativeTo = TransformSpace::Local) |
---|
[2662] | 151 | { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); } |
---|
[2072] | 152 | |
---|
[11071] | 153 | void lookAt(const Vector3& target, TransformSpace relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
---|
| 154 | void setDirection(const Vector3& direction, TransformSpace relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
---|
| 155 | inline void setDirection(float x, float y, float z, TransformSpace relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) |
---|
[2072] | 156 | { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); } |
---|
| 157 | |
---|
[2662] | 158 | virtual void setScale3D(const Vector3& scale); |
---|
[2072] | 159 | inline void setScale3D(float x, float y, float z) |
---|
[2662] | 160 | { this->setScale3D(Vector3(x, y, z)); } |
---|
[3301] | 161 | const Vector3& getScale3D() const; |
---|
[2662] | 162 | const Vector3& getWorldScale3D() const; |
---|
[2072] | 163 | |
---|
| 164 | inline void setScale(float scale) |
---|
[2662] | 165 | { this->setScale3D(scale, scale, scale); } |
---|
[2072] | 166 | inline float getScale() const |
---|
| 167 | { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } |
---|
[2662] | 168 | float getWorldScale() const; |
---|
[2072] | 169 | |
---|
| 170 | inline void scale3D(const Vector3& scale) |
---|
[2662] | 171 | { this->setScale3D(this->getScale3D() * scale); } |
---|
[2072] | 172 | inline void scale3D(float x, float y, float z) |
---|
[2662] | 173 | { this->scale3D(Vector3(x, y, z)); } |
---|
[2072] | 174 | inline void scale(float scale) |
---|
[2662] | 175 | { this->scale3D(scale, scale, scale); } |
---|
[2072] | 176 | |
---|
[2662] | 177 | virtual void changedScale() {} |
---|
| 178 | |
---|
[2072] | 179 | void attach(WorldEntity* object); |
---|
| 180 | void detach(WorldEntity* object); |
---|
[2662] | 181 | WorldEntity* getAttachedObject(unsigned int index); |
---|
[2072] | 182 | inline const std::set<WorldEntity*>& getAttachedObjects() const |
---|
| 183 | { return this->children_; } |
---|
| 184 | |
---|
[3196] | 185 | void attachOgreObject(Ogre::MovableObject* object); |
---|
| 186 | void attachOgreObject(Ogre::BillboardSet* object); |
---|
| 187 | void attachOgreObject(Ogre::Camera* object); |
---|
| 188 | void attachOgreObject(Ogre::Entity* object); |
---|
| 189 | void attachOgreObject(Ogre::ParticleSystem* object); |
---|
| 190 | |
---|
| 191 | void detachOgreObject(Ogre::MovableObject* object); |
---|
| 192 | void detachOgreObject(Ogre::BillboardSet* object); |
---|
| 193 | void detachOgreObject(Ogre::Camera* object); |
---|
| 194 | void detachOgreObject(Ogre::Entity* object); |
---|
| 195 | void detachOgreObject(Ogre::ParticleSystem* object); |
---|
| 196 | |
---|
[2662] | 197 | Ogre::MovableObject* detachOgreObject(const Ogre::String& name); |
---|
| 198 | |
---|
[2072] | 199 | inline void attachToParent(WorldEntity* parent) |
---|
| 200 | { parent->attach(this); } |
---|
| 201 | inline void detachFromParent() |
---|
| 202 | { if (this->parent_) { this->parent_->detach(this); } } |
---|
| 203 | inline WorldEntity* getParent() const |
---|
| 204 | { return this->parent_; } |
---|
| 205 | |
---|
[2662] | 206 | void attachNode(Ogre::SceneNode* node); |
---|
| 207 | void detachNode(Ogre::SceneNode* node); |
---|
| 208 | void attachToNode(Ogre::SceneNode* node); |
---|
| 209 | void detachFromNode(Ogre::SceneNode* node); |
---|
| 210 | |
---|
[3077] | 211 | inline void setDeleteWithParent(bool value) |
---|
| 212 | { this->bDeleteWithParent_ = value; } |
---|
| 213 | inline bool getDeleteWithParent() const |
---|
| 214 | { return this->bDeleteWithParent_; } |
---|
| 215 | |
---|
[2662] | 216 | void notifyChildPropsChanged(); |
---|
| 217 | |
---|
[2072] | 218 | protected: |
---|
[2851] | 219 | virtual void parentChanged() {} |
---|
| 220 | |
---|
[2072] | 221 | Ogre::SceneNode* node_; |
---|
| 222 | |
---|
| 223 | private: |
---|
[7163] | 224 | void registerVariables(); |
---|
[7910] | 225 | |
---|
[2072] | 226 | inline void lookAt_xmlport(const Vector3& target) |
---|
| 227 | { this->lookAt(target); } |
---|
| 228 | inline void setDirection_xmlport(const Vector3& direction) |
---|
| 229 | { this->setDirection(direction); } |
---|
| 230 | inline void yaw_xmlport(const Degree& angle) |
---|
| 231 | { this->yaw(angle); } |
---|
| 232 | inline void pitch_xmlport(const Degree& angle) |
---|
| 233 | { this->pitch(angle); } |
---|
| 234 | inline void roll_xmlport(const Degree& angle) |
---|
| 235 | { this->roll(angle); } |
---|
| 236 | |
---|
[2662] | 237 | // network callbacks |
---|
[2851] | 238 | void networkcallback_parentChanged(); |
---|
[2662] | 239 | inline void scaleChanged() |
---|
| 240 | { this->setScale3D(this->getScale3D()); } |
---|
| 241 | |
---|
[2072] | 242 | WorldEntity* parent_; |
---|
| 243 | unsigned int parentID_; |
---|
| 244 | std::set<WorldEntity*> children_; |
---|
[3077] | 245 | bool bDeleteWithParent_; |
---|
[7163] | 246 | |
---|
[6524] | 247 | bool bActiveMem_; |
---|
| 248 | bool bVisibleMem_; |
---|
[2662] | 249 | |
---|
| 250 | |
---|
| 251 | ///////////// |
---|
| 252 | // Physics // |
---|
| 253 | ///////////// |
---|
| 254 | |
---|
| 255 | public: |
---|
| 256 | /** |
---|
| 257 | @brief |
---|
| 258 | Denotes the possible types of physical objects in a Scene. |
---|
| 259 | |
---|
| 260 | Dynamic: The object is influenced by its physical environment, like for instance little ball. |
---|
| 261 | Kinematic: The object can only influence other dynamic objects. It's movement is coordinated by your own saying. |
---|
| 262 | Static: Like kinematic but the object is not allowed to move during the simulation. |
---|
| 263 | None: The object has no physics at all. |
---|
| 264 | */ |
---|
[11071] | 265 | enum class CollisionType |
---|
[2662] | 266 | { |
---|
| 267 | Dynamic, |
---|
| 268 | Kinematic, |
---|
| 269 | Static, |
---|
| 270 | None |
---|
| 271 | }; |
---|
| 272 | |
---|
| 273 | //! Tells whether the object has any connection to the Bullet physics engine. If hasPhysics() is false, the object may still have a velocity. |
---|
[11071] | 274 | bool hasPhysics() const { return getCollisionType() != CollisionType::None ; } |
---|
[2662] | 275 | //! @see CollisionType |
---|
[11071] | 276 | bool isStatic() const { return getCollisionType() == CollisionType::Static ; } |
---|
[2662] | 277 | //! @see CollisionType |
---|
[11071] | 278 | bool isKinematic() const { return getCollisionType() == CollisionType::Kinematic; } |
---|
[2662] | 279 | //! @see CollisionType |
---|
[11071] | 280 | bool isDynamic() const { return getCollisionType() == CollisionType::Dynamic ; } |
---|
[2662] | 281 | //! Tells whether physics has been activated (you can temporarily deactivate it) |
---|
| 282 | bool isPhysicsActive() const { return this->bPhysicsActive_; } |
---|
| 283 | bool addedToPhysicalWorld() const; |
---|
| 284 | |
---|
| 285 | void activatePhysics(); |
---|
| 286 | void deactivatePhysics(); |
---|
| 287 | |
---|
| 288 | //! Returns the CollisionType. @see CollisionType. |
---|
| 289 | inline CollisionType getCollisionType() const |
---|
| 290 | { return this->collisionType_; } |
---|
| 291 | void setCollisionType(CollisionType type); |
---|
| 292 | |
---|
| 293 | void setCollisionTypeStr(const std::string& type); |
---|
| 294 | std::string getCollisionTypeStr() const; |
---|
| 295 | |
---|
| 296 | //! Sets the mass of this object. Note that the total mass may be influenced by attached objects! |
---|
| 297 | inline void setMass(float mass) |
---|
| 298 | { this->mass_ = mass; recalculateMassProps(); } |
---|
| 299 | //! Returns the mass of this object without its children. |
---|
| 300 | inline float getMass() const |
---|
| 301 | { return this->mass_; } |
---|
| 302 | |
---|
| 303 | //! Returns the total mass of this object with all its attached children. |
---|
| 304 | inline float getTotalMass() const |
---|
| 305 | { return this->mass_ + this->childrenMass_; } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | @brief |
---|
| 309 | Returns the diagonal elements of the inertia tensor when calculated in local coordinates. |
---|
[7401] | 310 | @note |
---|
[2662] | 311 | The local inertia tensor cannot be set, but is calculated by Bullet according to the collisionShape. |
---|
| 312 | With compound collision shapes, an approximation is used. |
---|
| 313 | */ |
---|
| 314 | inline const btVector3& getLocalInertia() const |
---|
| 315 | { return this->localInertia_; } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | @brief |
---|
| 319 | Sets how much reaction is applied in a collision. |
---|
[2851] | 320 | |
---|
[2662] | 321 | Consider two equal spheres colliding with equal velocities: |
---|
| 322 | Restitution 1 means that both spheres simply reverse their velocity (no loss of energy) |
---|
| 323 | Restitution 0 means that both spheres will immediately stop moving |
---|
| 324 | (maximum loss of energy without violating of the preservation of momentum) |
---|
| 325 | */ |
---|
| 326 | inline void setRestitution(float restitution) |
---|
| 327 | { this->restitution_ = restitution; internalSetPhysicsProps(); } |
---|
| 328 | //! Returns the restitution parameter. @see setRestitution. |
---|
| 329 | inline float getRestitution() const |
---|
| 330 | { return this->restitution_; } |
---|
| 331 | |
---|
| 332 | /** |
---|
| 333 | @brief |
---|
| 334 | Sets an artificial parameter that tells how much torque is applied when you apply a non-central force. |
---|
| 335 | |
---|
[6417] | 336 | Normally the angular factor is 1, which means it's physically 'correct'. However if you have a player |
---|
[2662] | 337 | character that should not rotate when hit sideways, you can set the angular factor to 0. |
---|
| 338 | */ |
---|
| 339 | inline void setAngularFactor(float angularFactor) |
---|
| 340 | { this->angularFactor_ = angularFactor; internalSetPhysicsProps(); } |
---|
| 341 | //! Returns the angular factor. @see setAngularFactor. |
---|
| 342 | inline float getAngularFactor() const |
---|
| 343 | { return this->angularFactor_; } |
---|
| 344 | |
---|
| 345 | //! Applies a mass independent damping. Velocities will simply diminish exponentially. |
---|
| 346 | inline void setLinearDamping(float linearDamping) |
---|
| 347 | { this->linearDamping_ = linearDamping; internalSetPhysicsProps(); } |
---|
| 348 | //! Returns the linear damping. @see setLinearDamping. |
---|
| 349 | inline float getLinearDamping() const |
---|
| 350 | { return this->linearDamping_; } |
---|
| 351 | |
---|
| 352 | //! Applies a tensor independent rotation damping. Angular velocities will simply diminish exponentially. |
---|
| 353 | inline void setAngularDamping(float angularDamping) |
---|
| 354 | { this->angularDamping_ = angularDamping; internalSetPhysicsProps(); } |
---|
| 355 | //! Returns the angular damping. @see setAngularDamping. |
---|
| 356 | inline float getAngularDamping() const |
---|
| 357 | { return this->angularDamping_; } |
---|
| 358 | |
---|
| 359 | //! Applies friction to the object. Friction occurs when two objects collide. |
---|
| 360 | inline void setFriction(float friction) |
---|
| 361 | { this->friction_ = friction; internalSetPhysicsProps(); } |
---|
| 362 | //! Returns the amount of friction applied to the object. |
---|
| 363 | inline float getFriction() const |
---|
| 364 | { return this->friction_; } |
---|
| 365 | |
---|
[10288] | 366 | /** |
---|
| 367 | * Sets the motion threshold for continuous collision detection (CCD). This should be activated if an object moves further in one tick than its own |
---|
| 368 | * size. This means that in one tick the object may be in front of a wall and in the next tick it will be behind the wall without ever triggering a |
---|
| 369 | * collision. CCD ensures that collisions are still detected. By default it is deactivated (threshold = 0) which is fine for slow or static |
---|
| 370 | * objects, but it should be set to a real value for fast moving objects (e.g. projectiles). |
---|
| 371 | * |
---|
| 372 | * A good value for the threshold is (diameter^2). |
---|
| 373 | * |
---|
| 374 | * @param ccdMotionThreshold CCD is enabled if the squared velocity of the object is > ccdMotionThreshold (default 0.0). 0.0 means deactivated. |
---|
| 375 | */ |
---|
| 376 | inline void setCcdMotionThreshold(float ccdMotionThreshold) |
---|
| 377 | { this->ccdMotionThreshold_ = ccdMotionThreshold; internalSetPhysicsProps(); } |
---|
| 378 | //! Returns the currently used motion threshold for CCD (0 means CCD is deactivated). |
---|
| 379 | inline float getCcdMotionThreshold() const |
---|
| 380 | { return this->ccdMotionThreshold_; } |
---|
| 381 | |
---|
| 382 | /** |
---|
| 383 | * Sets the radius of the sphere which is used for continuous collision detection (CCD). The sphere should be embedded inside the objects collision |
---|
| 384 | * shape, preferably smaller. @see setCcdMotionThreshold for more information about CCD. |
---|
| 385 | * |
---|
| 386 | * A good value for the radius is (diameter/5). |
---|
| 387 | * |
---|
| 388 | * @param ccdSweptSphereRadius The diameter of the sphere which is used for CCD (default 0.0). |
---|
| 389 | */ |
---|
| 390 | inline void setCcdSweptSphereRadius(float ccdSweptSphereRadius) |
---|
| 391 | { this->ccdSweptSphereRadius_ = ccdSweptSphereRadius; internalSetPhysicsProps(); } |
---|
| 392 | //! Returns the currently used radius of the sphere for CCD. |
---|
| 393 | inline float getCcdSweptSphereRadius() const |
---|
| 394 | { return this->ccdSweptSphereRadius_; } |
---|
| 395 | |
---|
[2662] | 396 | void attachCollisionShape(CollisionShape* shape); |
---|
| 397 | void detachCollisionShape(CollisionShape* shape); |
---|
| 398 | CollisionShape* getAttachedCollisionShape(unsigned int index); |
---|
| 399 | |
---|
| 400 | void notifyCollisionShapeChanged(); |
---|
| 401 | void notifyChildMassChanged(); |
---|
| 402 | |
---|
| 403 | /** |
---|
| 404 | @brief |
---|
| 405 | Virtual function that gets called when this object collides with another. |
---|
| 406 | @param otherObject |
---|
| 407 | The object this one has collided into. |
---|
[11099] | 408 | @param ownCollisionShape |
---|
| 409 | The collision shape of the other object |
---|
[7401] | 410 | @param contactPoint |
---|
[2662] | 411 | Contact point provided by Bullet. Holds more information and can me modified. See return value. |
---|
[7401] | 412 | @return |
---|
[2662] | 413 | Returning false means that no modification to the contactPoint has been made. Return true otherwise! |
---|
[7401] | 414 | @note |
---|
[2662] | 415 | Condition is that enableCollisionCallback() was called. |
---|
| 416 | */ |
---|
[10216] | 417 | virtual inline bool collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) |
---|
[2662] | 418 | { return false; } /* With false, Bullet assumes no modification to the collision objects. */ |
---|
| 419 | |
---|
| 420 | //! Enables the collidesAgainst(.) function. The object doesn't respond to collision otherwise! |
---|
| 421 | inline void enableCollisionCallback() |
---|
| 422 | { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); } |
---|
| 423 | //! Disables the collidesAgainst(.) function. @see enableCollisionCallback() |
---|
| 424 | inline void disableCollisionCallback() |
---|
| 425 | { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); } |
---|
| 426 | //! Tells whether there could be a collision callback via collidesAgainst(.) |
---|
| 427 | inline bool isCollisionCallbackActive() const |
---|
| 428 | { return this->bCollisionCallbackActive_; } |
---|
| 429 | |
---|
| 430 | //! Enables or disables collision response (default is of course on) |
---|
| 431 | inline void setCollisionResponse(bool value) |
---|
| 432 | { this->bCollisionResponseActive_ = value; this->collisionResponseActivityChanged(); } |
---|
| 433 | //! Tells whether there could be a collision response |
---|
| 434 | inline bool hasCollisionResponse() |
---|
| 435 | { return this->bCollisionResponseActive_; } |
---|
| 436 | |
---|
| 437 | protected: |
---|
| 438 | /** |
---|
| 439 | @brief |
---|
| 440 | Function checks whether the requested collision type is legal to this object. |
---|
| 441 | |
---|
| 442 | You can override this function in a derived class to constrain the collision to e.g. None or Dynamic. |
---|
| 443 | A projectile may not prove very useful if there is no physical body. Simply set the CollisionType |
---|
[6417] | 444 | in its constructor and override this method. But be careful that a derived class's virtual functions |
---|
[2662] | 445 | don't yet exist in the constructor if a base class. |
---|
| 446 | */ |
---|
| 447 | virtual bool isCollisionTypeLegal(CollisionType type) const = 0; |
---|
| 448 | |
---|
| 449 | btRigidBody* physicalBody_; //!< Bullet rigid body. Everything physical is applied to this instance. |
---|
[12028] | 450 | std::string id_; //!< Used by the ScriptableController to identify objects |
---|
[2662] | 451 | |
---|
| 452 | private: |
---|
| 453 | void recalculateMassProps(); |
---|
| 454 | void internalSetPhysicsProps(); |
---|
| 455 | |
---|
| 456 | bool notifyBeingAttached(WorldEntity* newParent); |
---|
| 457 | void notifyDetached(); |
---|
| 458 | |
---|
| 459 | // network callbacks |
---|
| 460 | void collisionTypeChanged(); |
---|
| 461 | void physicsActivityChanged(); |
---|
| 462 | void collisionCallbackActivityChanged(); |
---|
| 463 | void collisionResponseActivityChanged(); |
---|
| 464 | //! Network callback workaround to call a function when the value changes. |
---|
| 465 | inline void massChanged() |
---|
| 466 | { this->setMass(this->mass_); } |
---|
| 467 | //! Network callback workaround to call a function when the value changes. |
---|
| 468 | inline void restitutionChanged() |
---|
| 469 | { this->setRestitution(this->restitution_); } |
---|
| 470 | //! Network callback workaround to call a function when the value changes. |
---|
| 471 | inline void angularFactorChanged() |
---|
| 472 | { this->setAngularFactor(this->angularFactor_); } |
---|
| 473 | //! Network callback workaround to call a function when the value changes. |
---|
| 474 | inline void linearDampingChanged() |
---|
| 475 | { this->setLinearDamping(this->linearDamping_); } |
---|
| 476 | //! Network callback workaround to call a function when the value changes. |
---|
| 477 | inline void angularDampingChanged() |
---|
| 478 | { this->setAngularDamping(this->angularDamping_); } |
---|
| 479 | //! Network callback workaround to call a function when the value changes. |
---|
| 480 | inline void frictionChanged() |
---|
| 481 | { this->setFriction(this->friction_); } |
---|
[10288] | 482 | //! Network callback workaround to call a function when the value changes. |
---|
| 483 | inline void ccdMotionThresholdChanged() |
---|
| 484 | { this->setCcdMotionThreshold(this->ccdMotionThreshold_); } |
---|
| 485 | //! Network callback workaround to call a function when the value changes. |
---|
| 486 | inline void ccdSweptSphereRadiusChanged() |
---|
| 487 | { this->setCcdSweptSphereRadius(this->ccdSweptSphereRadius_); } |
---|
[2662] | 488 | |
---|
| 489 | CollisionType collisionType_; //!< @see setCollisionType |
---|
| 490 | CollisionType collisionTypeSynchronised_; //!< Network synchronised variable for collisionType_ |
---|
| 491 | bool bPhysicsActive_; //!< @see isPhysicsActive |
---|
| 492 | bool bPhysicsActiveSynchronised_; //!< Network synchronised variable for bPhysicsActive_ |
---|
| 493 | //! When attaching objects hierarchically this variable tells this object (as child) whether physics was activated before attaching (because the deactivate physics while being attached). |
---|
| 494 | bool bPhysicsActiveBeforeAttaching_; |
---|
| 495 | WorldEntityCollisionShape* collisionShape_; //!< Attached collision shapes go here |
---|
| 496 | btScalar mass_; //!< @see setMass |
---|
| 497 | btVector3 localInertia_; //!< @see getLocalInertia |
---|
| 498 | btScalar restitution_; //!< @see setRestitution |
---|
| 499 | btScalar angularFactor_; //!< @see setAngularFactor |
---|
| 500 | btScalar linearDamping_; //!< @see setLinearDamping |
---|
| 501 | btScalar angularDamping_; //!< @see setAngularDamping |
---|
| 502 | btScalar friction_; //!< @see setFriction |
---|
| 503 | btScalar childrenMass_; //!< Sum of all the children's masses |
---|
[10288] | 504 | btScalar ccdMotionThreshold_; //!< @see setCcdMotionThreshold |
---|
| 505 | btScalar ccdSweptSphereRadius_; //!< @see setCcdSweptSphereRadius |
---|
[2662] | 506 | bool bCollisionCallbackActive_; //!< @see enableCollisionCallback |
---|
| 507 | bool bCollisionResponseActive_; //!< Tells whether the object should respond to collisions |
---|
[2072] | 508 | }; |
---|
[2662] | 509 | |
---|
| 510 | // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here. |
---|
[3196] | 511 | #ifdef ORXONOX_RELEASE |
---|
[2662] | 512 | inline const Vector3& WorldEntity::getPosition() const |
---|
| 513 | { return this->node_->getPosition(); } |
---|
| 514 | inline const Quaternion& WorldEntity::getOrientation() const |
---|
[2787] | 515 | { return this->node_->getOrientation(); } |
---|
[3301] | 516 | inline const Vector3& WorldEntity::getScale3D() const |
---|
[2662] | 517 | { return this->node_->getScale(); } |
---|
| 518 | #endif |
---|
| 519 | |
---|
| 520 | SUPER_FUNCTION(5, WorldEntity, changedScale, false); |
---|
[2072] | 521 | } |
---|
| 522 | |
---|
| 523 | #endif /* _WorldEntity_H__ */ |
---|