PhysicsCollisionObject.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef PHYSICSCOLLISIONOBJECT_H_
  2. #define PHYSICSCOLLISIONOBJECT_H_
  3. #include "Vector3.h"
  4. #include "PhysicsCollisionShape.h"
  5. #include "PhysicsMotionState.h"
  6. namespace gameplay
  7. {
  8. class Node;
  9. /**
  10. * Base class for all gameplay physics objects that support collision events.
  11. */
  12. class PhysicsCollisionObject
  13. {
  14. friend class PhysicsController;
  15. friend class PhysicsConstraint;
  16. public:
  17. /**
  18. * Represents the different types of collision objects.
  19. */
  20. enum Type
  21. {
  22. /**
  23. * PhysicsRigidBody type.
  24. */
  25. RIGID_BODY,
  26. /**
  27. * PhysicsCharacter type.
  28. */
  29. CHARACTER,
  30. /**
  31. * PhysicsGhostObject type.
  32. */
  33. GHOST_OBJECT,
  34. /**
  35. * No collision object.
  36. */
  37. NONE
  38. };
  39. /**
  40. * Defines a pair of rigid bodies that collided (or may collide).
  41. */
  42. class CollisionPair
  43. {
  44. public:
  45. /**
  46. * Constructor.
  47. */
  48. CollisionPair(PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB);
  49. /**
  50. * Less than operator (needed for use as a key in map).
  51. *
  52. * @param collisionPair The collision pair to compare.
  53. * @return True if this pair is "less than" the given pair; false otherwise.
  54. */
  55. bool operator < (const CollisionPair& collisionPair) const;
  56. /**
  57. * The first object in the collision.
  58. */
  59. PhysicsCollisionObject* objectA;
  60. /**
  61. * The second object in the collision.
  62. */
  63. PhysicsCollisionObject* objectB;
  64. };
  65. /**
  66. * Collision listener interface.
  67. */
  68. class CollisionListener
  69. {
  70. friend class PhysicsCollisionObject;
  71. friend class PhysicsController;
  72. public:
  73. /**
  74. * The type of collision event.
  75. */
  76. enum EventType
  77. {
  78. /**
  79. * Event fired when the two rigid bodies start colliding.
  80. */
  81. COLLIDING,
  82. /**
  83. * Event fired when the two rigid bodies no longer collide.
  84. */
  85. NOT_COLLIDING
  86. };
  87. /**
  88. * Virtual destructor.
  89. */
  90. virtual ~CollisionListener() { }
  91. /**
  92. * Called when a collision occurs between two objects in the physics world.
  93. *
  94. * @param type The type of collision event.
  95. * @param collisionPair The two collision objects involved in the collision.
  96. * @param contactPointA The contact point with the first object (in world space).
  97. * @param contactPointB The contact point with the second object (in world space).
  98. */
  99. virtual void collisionEvent(PhysicsCollisionObject::CollisionListener::EventType type,
  100. const PhysicsCollisionObject::CollisionPair& collisionPair,
  101. const Vector3& contactPointA = Vector3::zero(),
  102. const Vector3& contactPointB = Vector3::zero()) = 0;
  103. };
  104. /**
  105. * Virtual destructor.
  106. */
  107. virtual ~PhysicsCollisionObject();
  108. /**
  109. * Returns the type of the collision object.
  110. */
  111. virtual PhysicsCollisionObject::Type getType() const = 0;
  112. /**
  113. * Returns the type of the shape for this collision object.
  114. */
  115. PhysicsCollisionShape::Type getShapeType() const;
  116. /**
  117. * Returns the node associated with this collision object.
  118. */
  119. Node* getNode() const;
  120. /**
  121. * Returns the collision shape.
  122. *
  123. * @return The collision shape.
  124. */
  125. PhysicsCollisionShape* getCollisionShape() const;
  126. /**
  127. * Returns whether this collision object is kinematic.
  128. *
  129. * A kinematic collision object is an object that is not simulated by
  130. * the physics system and instead has its transform driven manually.
  131. *
  132. * @return true if the collision object is kinematic.
  133. */
  134. bool isKinematic() const;
  135. /**
  136. * Returns whether this collision object is dynamic.
  137. *
  138. * A dynamic collision object is simulated entirely by the physics system,
  139. * such as with dynamic rigid bodies.
  140. *
  141. * @return true if the collision object is dynamic.
  142. */
  143. bool isDynamic() const;
  144. /**
  145. * Check if th collision object is enabled.
  146. *
  147. * @return true if the collision object is enabled.
  148. */
  149. bool isEnabled() const;
  150. /**
  151. * Sets the collision object be enabled or disabled.
  152. *
  153. * @param enable true enables the collision object, false diables it.
  154. */
  155. void setEnabled(bool enable);
  156. /**
  157. * Adds a collision listener for this collision object.
  158. *
  159. * @param listener The listener to add.
  160. * @param object Optional collision object used to filter the collision event.
  161. */
  162. void addCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  163. /**
  164. * Removes a collision listener.
  165. *
  166. * @param listener The listener to remove.
  167. * @param object Optional collision object used to filter the collision event.
  168. */
  169. void removeCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  170. /**
  171. * Checks if this collision object collides with the given object.
  172. *
  173. * @param object The collision object to test for collision with.
  174. *
  175. * @return true if this object collides with the specified one; false otherwise.
  176. */
  177. bool collidesWith(PhysicsCollisionObject* object) const;
  178. protected:
  179. /**
  180. * Interface between GamePlay and Bullet to keep object transforms synchronized properly.
  181. *
  182. * @see btMotionState
  183. */
  184. class PhysicsMotionState : public btMotionState
  185. {
  186. friend class PhysicsConstraint;
  187. public:
  188. /**
  189. * Creates a physics motion state for a rigid body.
  190. *
  191. * @param node The node that owns the rigid body that the motion state is being created for.
  192. * @param centerOfMassOffset The translation offset to the center of mass of the rigid body.
  193. */
  194. PhysicsMotionState(Node* node, const Vector3* centerOfMassOffset = NULL);
  195. /**
  196. * Destructor.
  197. */
  198. virtual ~PhysicsMotionState();
  199. /**
  200. * @see btMotionState#getWorldTransform
  201. */
  202. virtual void getWorldTransform(btTransform &transform) const;
  203. /**
  204. * @see btMotionState#setWorldTransform
  205. */
  206. virtual void setWorldTransform(const btTransform &transform);
  207. /**
  208. * Updates the motion state's world transform from the GamePlay Node object's world transform.
  209. */
  210. void updateTransformFromNode() const;
  211. private:
  212. Node* _node;
  213. btTransform _centerOfMassOffset;
  214. mutable btTransform _worldTransform;
  215. };
  216. /**
  217. * Constructor.
  218. */
  219. PhysicsCollisionObject(Node* node);
  220. /**
  221. * Returns the Bullet Physics collision object.
  222. *
  223. * @return The Bullet collision object.
  224. */
  225. virtual btCollisionObject* getCollisionObject() const = 0;
  226. /**
  227. * Pointer to Node contained by this collision object.
  228. */
  229. Node* _node;
  230. /**
  231. * The PhysicsCollisionObject's motion state.
  232. */
  233. PhysicsMotionState* _motionState;
  234. /**
  235. * The PhysicsCollisionObject's collision shape.
  236. */
  237. PhysicsCollisionShape* _collisionShape;
  238. /**
  239. * If the collision object is enabled or not.
  240. */
  241. bool _enabled;
  242. };
  243. }
  244. #endif