PhysicsCollisionObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #ifndef PHYSICSCOLLISIONOBJECT_H_
  2. #define PHYSICSCOLLISIONOBJECT_H_
  3. #include "Vector3.h"
  4. #include "PhysicsCollisionShape.h"
  5. namespace gameplay
  6. {
  7. class Node;
  8. class PhysicsRigidBody;
  9. class PhysicsCharacter;
  10. class PhysicsGhostObject;
  11. class PhysicsVehicle;
  12. class PhysicsVehicleWheel;
  13. #define PHYSICS_COLLISION_GROUP_DEFAULT btBroadphaseProxy::DefaultFilter
  14. #define PHYSICS_COLLISION_MASK_DEFAULT btBroadphaseProxy::AllFilter
  15. /**
  16. * Base class for all gameplay physics objects that support collision events.
  17. */
  18. class PhysicsCollisionObject
  19. {
  20. friend class PhysicsController;
  21. friend class PhysicsConstraint;
  22. friend class PhysicsRigidBody;
  23. friend class PhysicsGhostObject;
  24. public:
  25. /**
  26. * Represents the different types of collision objects.
  27. */
  28. enum Type
  29. {
  30. /**
  31. * PhysicsRigidBody type.
  32. */
  33. RIGID_BODY,
  34. /**
  35. * PhysicsCharacter type.
  36. */
  37. CHARACTER,
  38. /**
  39. * PhysicsGhostObject type.
  40. */
  41. GHOST_OBJECT,
  42. /**
  43. * PhysicsVehicle type.
  44. */
  45. VEHICLE,
  46. /**
  47. * PhysicsVehicleWheel type.
  48. */
  49. VEHICLE_WHEEL,
  50. /**
  51. * No collision object.
  52. */
  53. NONE
  54. };
  55. /**
  56. * Defines a pair of rigid bodies that collided (or may collide).
  57. */
  58. class CollisionPair
  59. {
  60. public:
  61. /**
  62. * Constructor.
  63. */
  64. CollisionPair(PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB);
  65. /**
  66. * Less than operator (needed for use as a key in map).
  67. *
  68. * @param collisionPair The collision pair to compare.
  69. * @return True if this pair is "less than" the given pair; false otherwise.
  70. */
  71. bool operator < (const CollisionPair& collisionPair) const;
  72. /**
  73. * The first object in the collision.
  74. */
  75. PhysicsCollisionObject* objectA;
  76. /**
  77. * The second object in the collision.
  78. */
  79. PhysicsCollisionObject* objectB;
  80. };
  81. /**
  82. * Collision listener interface.
  83. */
  84. class CollisionListener
  85. {
  86. friend class PhysicsCollisionObject;
  87. friend class PhysicsController;
  88. public:
  89. /**
  90. * The type of collision event.
  91. */
  92. enum EventType
  93. {
  94. /**
  95. * Event fired when the two rigid bodies start colliding.
  96. */
  97. COLLIDING,
  98. /**
  99. * Event fired when the two rigid bodies no longer collide.
  100. */
  101. NOT_COLLIDING
  102. };
  103. /**
  104. * Virtual destructor.
  105. */
  106. virtual ~CollisionListener() { }
  107. /**
  108. * Called when a collision occurs between two objects in the physics world.
  109. *
  110. * NOTE: You are not permitted to disable physics objects from within this callback. Disabling physics on a collision object
  111. * removes the object from the physics world. This is not permitted during the PhysicsController::update.
  112. *
  113. * @param type The type of collision event.
  114. * @param collisionPair The two collision objects involved in the collision.
  115. * @param contactPointA The contact point with the first object (in world space).
  116. * @param contactPointB The contact point with the second object (in world space).
  117. */
  118. virtual void collisionEvent(PhysicsCollisionObject::CollisionListener::EventType type,
  119. const PhysicsCollisionObject::CollisionPair& collisionPair,
  120. const Vector3& contactPointA = Vector3::zero(),
  121. const Vector3& contactPointB = Vector3::zero()) = 0;
  122. };
  123. /**
  124. * Virtual destructor.
  125. */
  126. virtual ~PhysicsCollisionObject();
  127. /**
  128. * Returns the type of the collision object.
  129. */
  130. virtual PhysicsCollisionObject::Type getType() const = 0;
  131. /**
  132. * Returns the type of the shape for this collision object.
  133. */
  134. PhysicsCollisionShape::Type getShapeType() const;
  135. /**
  136. * Returns the node associated with this collision object.
  137. */
  138. Node* getNode() const;
  139. /**
  140. * Returns the collision shape.
  141. *
  142. * @return The collision shape.
  143. */
  144. PhysicsCollisionShape* getCollisionShape() const;
  145. /**
  146. * Returns whether this collision object is kinematic.
  147. *
  148. * A kinematic collision object is an object that is not simulated by
  149. * the physics system and instead has its transform driven manually.
  150. *
  151. * @return true if the collision object is kinematic.
  152. */
  153. bool isKinematic() const;
  154. /**
  155. * Returns whether this collision object is static.
  156. *
  157. * A static collision object is not simulated by the physics system and cannot be
  158. * transformed once created.
  159. *
  160. * @return true if the collision object is static.
  161. */
  162. bool isStatic() const;
  163. /**
  164. * Returns whether this collision object is dynamic.
  165. *
  166. * A dynamic collision object is simulated entirely by the physics system,
  167. * such as with dynamic rigid bodies.
  168. *
  169. * @return true if the collision object is dynamic.
  170. */
  171. bool isDynamic() const;
  172. /**
  173. * Check if the collision object is enabled.
  174. *
  175. * @return true if the collision object is enabled.
  176. */
  177. bool isEnabled() const;
  178. /**
  179. * Sets the collision object to be enabled or disabled.
  180. *
  181. * @param enable true enables the collision object, false disables it.
  182. */
  183. void setEnabled(bool enable);
  184. /**
  185. * Adds a collision listener for this collision object.
  186. *
  187. * @param listener The listener to add.
  188. * @param object Optional collision object used to filter the collision event.
  189. */
  190. void addCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  191. /**
  192. * Removes a collision listener.
  193. *
  194. * @param listener The listener to remove.
  195. * @param object Optional collision object used to filter the collision event.
  196. */
  197. void removeCollisionListener(CollisionListener* listener, PhysicsCollisionObject* object = NULL);
  198. /**
  199. * Adds a collision listener for this collision object.
  200. *
  201. * Note: the given Lua function must match the function signature of PhysicsCollisionObject::CollisionListener::collisionEvent.
  202. *
  203. * @param function The Lua script function to add as a listener callback.
  204. * @param object Optional collision object used to filter the collision event.
  205. */
  206. void addCollisionListener(const char* function, PhysicsCollisionObject* object = NULL);
  207. /**
  208. * Removes a collision listener.
  209. *
  210. * @param function The Lua function (used as a listener callback) to remove.
  211. * @param object Optional collision object used to filter the collision event.
  212. */
  213. void removeCollisionListener(const char* function, PhysicsCollisionObject* object = NULL);
  214. /**
  215. * Checks if this collision object collides with the given object.
  216. *
  217. * @param object The collision object to test for collision with.
  218. *
  219. * @return true if this object collides with the specified one; false otherwise.
  220. */
  221. bool collidesWith(PhysicsCollisionObject* object) const;
  222. /**
  223. * Returns this collision object as a physics rigid body.
  224. *
  225. * If this collision object is not of type RIGID_BODY, this method returns NULL.
  226. *
  227. * @return This collision object cast to a PhysicsRigidBody.
  228. */
  229. PhysicsRigidBody* asRigidBody();
  230. /**
  231. * Returns this collision object as a physics character.
  232. *
  233. * If this collision object is not of type CHARACTER, this method returns NULL.
  234. *
  235. * @return This collision object cast to a PhysicsCharacter.
  236. */
  237. PhysicsCharacter* asCharacter();
  238. /**
  239. * Returns this collision object as a physics ghost object.
  240. *
  241. * If this collision object is not of type GHOST_OBJECT, this method returns NULL.
  242. *
  243. * @return This collision object cast to a PhysicsGhostObject.
  244. */
  245. PhysicsGhostObject* asGhostObject();
  246. /**
  247. * Returns this collision object as a physics vehicle.
  248. *
  249. * If this collision object is not of type VEHICLE, this method returns NULL.
  250. *
  251. * @return This collision object cast to a PhysicsVehicle.
  252. */
  253. PhysicsVehicle* asVehicle();
  254. /**
  255. * Returns this collision object as a physics vehicle wheel.
  256. *
  257. * If this collision object is not of type VEHICLE_WHEEL, this method returns NULL.
  258. *
  259. * @return This collision object cast to a PhysicsVehicleWheel.
  260. */
  261. PhysicsVehicleWheel* asVehicleWheel();
  262. protected:
  263. /**
  264. * Handles collision event callbacks to Lua script functions.
  265. */
  266. struct ScriptListener : public CollisionListener
  267. {
  268. /**
  269. * Constructor.
  270. */
  271. ScriptListener(const char* url);
  272. /**
  273. * @see PhysicsCollisionObject::CollisionListener
  274. */
  275. void collisionEvent(PhysicsCollisionObject::CollisionListener::EventType type, const PhysicsCollisionObject::CollisionPair& collisionPair,
  276. const Vector3& contactPointA, const Vector3& contactPointB);
  277. /** The URL to the Lua script function to use as the callback. */
  278. std::string url;
  279. /** The name of the Lua script function to use as the callback. */
  280. std::string function;
  281. };
  282. /**
  283. * Constructor.
  284. */
  285. PhysicsCollisionObject(Node* node, int group = PHYSICS_COLLISION_GROUP_DEFAULT, int mask = PHYSICS_COLLISION_MASK_DEFAULT);
  286. /**
  287. * Returns the Bullet Physics collision object.
  288. *
  289. * @return The Bullet collision object.
  290. */
  291. virtual btCollisionObject* getCollisionObject() const = 0;
  292. /**
  293. * Pointer to Node contained by this collision object.
  294. */
  295. Node* _node;
  296. /**
  297. * The PhysicsCollisionObject's collision shape.
  298. */
  299. PhysicsCollisionShape* _collisionShape;
  300. /**
  301. * If the collision object is enabled or not.
  302. */
  303. bool _enabled;
  304. /**
  305. * The list of script listeners.
  306. */
  307. std::vector<ScriptListener*>* _scriptListeners;
  308. private:
  309. /**
  310. * Interface between GamePlay and Bullet to keep object transforms synchronized properly.
  311. *
  312. * @see btMotionState
  313. */
  314. class PhysicsMotionState : public btMotionState
  315. {
  316. friend class PhysicsConstraint;
  317. public:
  318. /**
  319. * Creates a physics motion state for a rigid body.
  320. *
  321. * @param node The node that contains the transformation to be associated with the motion state.
  322. * @param collisionObject The collision object that owns the motion state.
  323. * @param centerOfMassOffset The translation offset to the center of mass of the rigid body.
  324. */
  325. PhysicsMotionState(Node* node, PhysicsCollisionObject* collisionObject, const Vector3* centerOfMassOffset = NULL);
  326. /**
  327. * Destructor.
  328. */
  329. virtual ~PhysicsMotionState();
  330. /**
  331. * @see btMotionState::getWorldTransform
  332. */
  333. virtual void getWorldTransform(btTransform &transform) const;
  334. /**
  335. * @see btMotionState::setWorldTransform
  336. */
  337. virtual void setWorldTransform(const btTransform &transform);
  338. /**
  339. * Updates the motion state's world transform from the GamePlay Node object's world transform.
  340. */
  341. void updateTransformFromNode() const;
  342. /**
  343. * Sets the center of mass offset for the associated collision shape.
  344. */
  345. void setCenterOfMassOffset(const Vector3& centerOfMassOffset);
  346. private:
  347. Node* _node;
  348. PhysicsCollisionObject* _collisionObject;
  349. btTransform _centerOfMassOffset;
  350. mutable btTransform _worldTransform;
  351. };
  352. /**
  353. * The PhysicsCollisionObject's motion state.
  354. */
  355. PhysicsMotionState* _motionState;
  356. /**
  357. * Group identifier and the bitmask for collision filtering.
  358. */
  359. int _group;
  360. int _mask;
  361. };
  362. }
  363. #endif