PhysicsCollisionObject.h 12 KB

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