PhysicsController.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * PhysicsController.h
  3. */
  4. #ifndef PHYSICSCONTROLLER_H_
  5. #define PHYSICSCONTROLLER_H_
  6. #include "PhysicsConstraint.h"
  7. #include "PhysicsFixedConstraint.h"
  8. #include "PhysicsGenericConstraint.h"
  9. #include "PhysicsHingeConstraint.h"
  10. #include "PhysicsSocketConstraint.h"
  11. #include "PhysicsSpringConstraint.h"
  12. #include "PhysicsRigidBody.h"
  13. namespace gameplay
  14. {
  15. /**
  16. * Defines a class for controlling game physics.
  17. */
  18. class PhysicsController
  19. {
  20. friend class Game;
  21. friend class PhysicsConstraint;
  22. friend class PhysicsRigidBody;
  23. public:
  24. /**
  25. * Status listener interface.
  26. */
  27. class Listener
  28. {
  29. public:
  30. /**
  31. * The type of physics status event.
  32. */
  33. enum EventType
  34. {
  35. /**
  36. * Event fired when there were no active physics objects and at least one is now active.
  37. */
  38. ACTIVATED,
  39. /**
  40. * Event fired when there are no more active physics objects in the world.
  41. */
  42. DEACTIVATED
  43. };
  44. /**
  45. * Handles when the physics world status changes.
  46. */
  47. virtual void statusEvent(EventType type) = 0;
  48. };
  49. /**
  50. * Adds a status listener.
  51. *
  52. * @param listener The listener to add.
  53. */
  54. void addStatusListener(Listener* listener);
  55. /**
  56. * Creates a fixed constraint.
  57. *
  58. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  59. * body specified the constraint applies between it and the global physics world object.
  60. * @param b The second rigid body to constrain (optional).
  61. */
  62. PhysicsFixedConstraint* createFixedConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b = NULL);
  63. /**
  64. * Creates a generic constraint so that the rigid body (or bodies) is
  65. * (are) constrained to its (their) current world position(s).
  66. *
  67. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  68. * body specified the constraint applies between it and the global physics world object.
  69. * @param b The second rigid body to constrain (optional).
  70. */
  71. PhysicsGenericConstraint* createGenericConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b = NULL);
  72. /**
  73. * Creates a generic constraint.
  74. *
  75. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  76. * body specified the constraint applies between it and the global physics world object.
  77. * @param rotationOffsetA The rotation offset for the first rigid body
  78. * (in its local space) with respect to the constraint joint.
  79. * @param translationOffsetA The translation offset for the first rigid body
  80. * (in its local space) with respect to the constraint joint.
  81. * @param b The second rigid body to constrain (optional).
  82. * @param rotationOffsetB The rotation offset for the second rigid body
  83. * (in its local space) with respect to the constraint joint (optional).
  84. * @param translationOffsetB The translation offset for the second rigid body
  85. * (in its local space) with respect to the constraint joint (optional).
  86. */
  87. PhysicsGenericConstraint* createGenericConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA,
  88. const Vector3& translationOffsetA, PhysicsRigidBody* b = NULL,
  89. const Quaternion& rotationOffsetB = Quaternion(), const Vector3& translationOffsetB = Vector3());
  90. /**
  91. * Creates a hinge constraint.
  92. *
  93. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  94. * body specified the constraint applies between it and the global physics world object.
  95. * @param rotationOffsetA The rotation offset for the first rigid body
  96. * (in its local space) with respect to the constraint joint.
  97. * @param translationOffsetA The translation offset for the first rigid body
  98. * (in its local space) with respect to the constraint joint.
  99. * @param b The second rigid body to constrain (optional).
  100. * @param rotationOffsetB The rotation offset for the second rigid body
  101. * (in its local space) with respect to the constraint joint (optional).
  102. * @param translationOffsetB The translation offset for the second rigid body
  103. * (in its local space) with respect to the constraint joint (optional).
  104. */
  105. PhysicsHingeConstraint* createHingeConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA,
  106. const Vector3& translationOffsetA, PhysicsRigidBody* b = NULL,
  107. const Quaternion& rotationOffsetB = Quaternion(), const Vector3& translationOffsetB = Vector3());
  108. /**
  109. * Creates a socket constraint so that the rigid body (or bodies) is
  110. * (are) constrained using its (their) current world position(s) for
  111. * the translation offset(s) to the constraint.
  112. *
  113. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  114. * body specified the constraint applies between it and the global physics world object.
  115. * @param b The second rigid body to constrain (optional).
  116. */
  117. PhysicsSocketConstraint* createSocketConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b = NULL);
  118. /**
  119. * Creates a socket constraint.
  120. *
  121. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  122. * body specified the constraint applies between it and the global physics world object.
  123. * @param translationOffsetA The translation offset for the first rigid body
  124. * (in its local space) with respect to the constraint joint.
  125. * @param b The second rigid body to constrain (optional).
  126. * @param translationOffsetB The translation offset for the second rigid body
  127. * (in its local space) with respect to the constraint joint (optional).
  128. */
  129. PhysicsSocketConstraint* createSocketConstraint(PhysicsRigidBody* a, const Vector3& translationOffsetA,
  130. PhysicsRigidBody* b = NULL, const Vector3& translationOffsetB = Vector3());
  131. /**
  132. * Creates a spring constraint so that the rigid body (or bodies) is
  133. * (are) constrained using its (their) current world position(s) for
  134. * the translation offset(s) to the constraint.
  135. *
  136. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  137. * body specified the constraint applies between it and the global physics world object.
  138. * @param b The second rigid body to constrain (optional).
  139. */
  140. PhysicsSpringConstraint* createSpringConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b);
  141. /**
  142. * Creates a spring constraint.
  143. *
  144. * @param a The first (possibly only) rigid body to constrain. If this is the only rigid
  145. * body specified the constraint applies between it and the global physics world object.
  146. * @param rotationOffsetA The rotation offset for the first rigid body
  147. * (in its local space) with respect to the constraint joint.
  148. * @param translationOffsetA The translation offset for the first rigid body
  149. * (in its local space) with respect to the constraint joint.
  150. * @param b The second rigid body to constrain (optional).
  151. * @param rotationOffsetB The rotation offset for the second rigid body
  152. * (in its local space) with respect to the constraint joint (optional).
  153. * @param translationOffsetB The translation offset for the second rigid body
  154. * (in its local space) with respect to the constraint joint (optional).
  155. */
  156. PhysicsSpringConstraint* createSpringConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA,
  157. const Vector3& translationOffsetA, PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB);
  158. /**
  159. * Gets the gravity vector for the simulated physics world.
  160. *
  161. * @return The gravity vector.
  162. */
  163. const Vector3& getGravity(const Vector3& gravity) const;
  164. /**
  165. * Sets the gravity vector for the simulated physics world.
  166. *
  167. * @param gravity The gravity vector.
  168. */
  169. void setGravity(const Vector3& gravity);
  170. private:
  171. /**
  172. * Constructor.
  173. */
  174. PhysicsController();
  175. /**
  176. * Destructor.
  177. */
  178. ~PhysicsController();
  179. /**
  180. * Controller initialize.
  181. */
  182. void initialize();
  183. /**
  184. * Controller finalize.
  185. */
  186. void finalize();
  187. /**
  188. * Controller pause.
  189. */
  190. void pause();
  191. /**
  192. * Controller resume.
  193. */
  194. void resume();
  195. /**
  196. * Controller update.
  197. */
  198. void update(long elapsedTime);
  199. // Adds the given rigid body to the world.
  200. void addRigidBody(PhysicsRigidBody* body);
  201. // Creates a box collision shape to be used in the creation of a rigid body.
  202. btCollisionShape* getBox(const Vector3& min, const Vector3& max, const btVector3& scale);
  203. // Gets the corresponding GamePlay object for the given Bullet object.
  204. PhysicsRigidBody* getPhysicsRigidBody(const btCollisionObject* collisionObject);
  205. // Creates a sphere collision shape to be used in the creation of a rigid body.
  206. btCollisionShape* getSphere(float radius, const btVector3& scale);
  207. // Removes the given constraint from the simulated physics world.
  208. void removeConstraint(PhysicsConstraint* constraint);
  209. // Removes the given rigid body from the simulated physics world.
  210. void removeRigidBody(PhysicsRigidBody* rigidBody);
  211. // Sets up the given constraint for the given two rigid bodies.
  212. void setupConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b, PhysicsConstraint* constraint);
  213. Vector3 _gravity;
  214. btDefaultCollisionConfiguration* _collisionConfiguration;
  215. btCollisionDispatcher* _dispatcher;
  216. btBroadphaseInterface* _overlappingPairCache;
  217. btSequentialImpulseConstraintSolver* _solver;
  218. btDynamicsWorld* _world;
  219. btAlignedObjectArray<btCollisionShape*> _shapes;
  220. Listener::EventType _status;
  221. std::vector<PhysicsRigidBody*> _bodies;
  222. std::vector<Listener*>* _listeners;
  223. };
  224. }
  225. #endif