PhysicsCollisionObject.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef PHYSICSCOLLISIONOBJECT_H_
  2. #define PHYSICSCOLLISIONOBJECT_H_
  3. #include "Vector3.h"
  4. namespace gameplay
  5. {
  6. class Node;
  7. /**
  8. * Base class for all gameplay physics objects that support collision events.
  9. */
  10. class PhysicsCollisionObject
  11. {
  12. friend class PhysicsController;
  13. public:
  14. /**
  15. * Enumeration of all possible collision object types.
  16. */
  17. enum Type
  18. {
  19. /**
  20. * PhysicsRigidBody type.
  21. */
  22. RIGID_BODY,
  23. /**
  24. * PhysicsCharacter type.
  25. */
  26. CHARACTER
  27. };
  28. /**
  29. * Defines a pair of rigid bodies that collided (or may collide).
  30. */
  31. class CollisionPair
  32. {
  33. public:
  34. /**
  35. * Constructor.
  36. */
  37. CollisionPair(PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB);
  38. /**
  39. * Less than operator (needed for use as a key in map).
  40. *
  41. * @param collisionPair The collision pair to compare.
  42. * @return True if this pair is "less than" the given pair; false otherwise.
  43. */
  44. bool operator < (const CollisionPair& collisionPair) const;
  45. /**
  46. * The first object in the collision.
  47. */
  48. PhysicsCollisionObject* objectA;
  49. /**
  50. * The second object in the collision.
  51. */
  52. PhysicsCollisionObject* objectB;
  53. };
  54. /**
  55. * Collision listener interface.
  56. */
  57. class CollisionListener
  58. {
  59. friend class PhysicsCollisionObject;
  60. friend class PhysicsController;
  61. public:
  62. /**
  63. * The type of collision event.
  64. */
  65. enum EventType
  66. {
  67. /**
  68. * Event fired when the two rigid bodies start colliding.
  69. */
  70. COLLIDING,
  71. /**
  72. * Event fired when the two rigid bodies no longer collide.
  73. */
  74. NOT_COLLIDING
  75. };
  76. /**
  77. * Virtual destructor.
  78. */
  79. virtual ~CollisionListener() { }
  80. /**
  81. * Called when a collision occurs between two objects in the physics world.
  82. *
  83. * @param type The type of collision event.
  84. * @param collisionPair The two collision objects involved in the collision.
  85. * @param contactPointA The contact point with the first object (in world space).
  86. * @param contactPointB The contact point with the second object (in world space).
  87. */
  88. virtual void collisionEvent(PhysicsCollisionObject::CollisionListener::EventType type,
  89. const PhysicsCollisionObject::CollisionPair& collisionPair,
  90. const Vector3& contactPointA = Vector3::zero(),
  91. const Vector3& contactPointB = Vector3::zero()) = 0;
  92. };
  93. /**
  94. * Returns the type of the collision object.
  95. */
  96. virtual PhysicsCollisionObject::Type getType() const = 0;
  97. /**
  98. * Returns the node associated with this collision object.
  99. */
  100. virtual Node* getNode() const = 0;
  101. /**
  102. * Adds a collision listener for this collision object.
  103. *
  104. * @param listener The listener to add.
  105. * @param object Optional collision object used to filter the collision event.
  106. */
  107. void addCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  108. /**
  109. * Removes a collision listener.
  110. *
  111. * @param listener The listener to remove.
  112. */
  113. void removeCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  114. /**
  115. * Checks if this collision object collides with the given object.
  116. *
  117. * @param object The collision object to test for collision with.
  118. *
  119. * @return True if this object collides with the specified one; false otherwise.
  120. */
  121. bool collidesWith(PhysicsCollisionObject* object) const;
  122. protected:
  123. /**
  124. * Constructor.
  125. */
  126. PhysicsCollisionObject();
  127. /**
  128. * Virtual destructor.
  129. */
  130. virtual ~PhysicsCollisionObject();
  131. /**
  132. * Returns the Bullet Physics collision object.
  133. *
  134. * @return The Bullet collision object.
  135. */
  136. virtual btCollisionObject* getCollisionObject() const = 0;
  137. /**
  138. * Returns the Bullet Physics collision shape.
  139. *
  140. * @return The Bullet collision shape.
  141. */
  142. virtual btCollisionShape* getCollisionShape() const = 0;
  143. private:
  144. // Internal class used to implement the collidesWith(PhysicsRigidBody*) function.
  145. struct CollidesWithCallback : public btCollisionWorld::ContactResultCallback
  146. {
  147. btScalar addSingleResult(btManifoldPoint& cp,
  148. const btCollisionObject* a, int partIdA, int indexA,
  149. const btCollisionObject* b, int partIdB, int indexB);
  150. bool result;
  151. };
  152. };
  153. }
  154. #endif