343 line
10 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  10. //
  11. //
  12. // The EasyPhysic class
  13. // ------------------
  14. //
  15. #if !defined __EASYPHYSICS_EASYPHYSICS_H__
  16. #define __EASYPHYSICS_EASYPHYSICS_H__
  17. #ifdef HAVE_PHYS_USE_BULLET
  18. #include "core.h"
  19. #include <bullet/btBulletDynamicsCommon.h>
  20. #include <bullet/btBulletCollisionCommon.h>
  21. #include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h>
  22. #include <BulletDynamics/Character/btKinematicCharacterController.h>
  23. #endif
  24. namespace lol
  25. {
  26. namespace phys
  27. {
  28. class EasyPhysic
  29. {
  30. friend class EasyConstraint;
  31. #ifdef HAVE_PHYS_USE_BULLET
  32. public:
  33. EasyPhysic();
  34. ~EasyPhysic();
  35. virtual void SetShapeToBox(lol::vec3& box_size);
  36. virtual void SetShapeToSphere(float radius);
  37. virtual void SetShapeToCone(float radius, float height);
  38. virtual void SetShapeToCylinder(lol::vec3& cyl_size);
  39. virtual void SetShapeToCapsule(float radius, float height);
  40. virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  41. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  42. virtual void SetMass(float mass);
  43. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  44. virtual void InitBodyToGhost();
  45. virtual void AddToSimulation(class Simulation* current_simulation);
  46. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  47. virtual mat4 GetTransform();
  48. protected:
  49. virtual void SetLocalInertia(float mass);
  50. virtual void SetShapeTo(btCollisionShape* collision_shape);
  51. virtual btGhostObject* GetGhostObject();
  52. btCollisionObject* m_collision_object;
  53. btGhostObject* m_ghost_object;
  54. btRigidBody* m_rigid_body;
  55. btVector3 m_local_inertia;
  56. btCollisionShape* m_collision_shape;
  57. btConvexShape* m_convex_shape;
  58. btMotionState* m_motion_state;
  59. #else // NO PHYSIC IMPLEMENTATION
  60. public:
  61. EasyPhysic() { }
  62. virtual void SetShapeToBox(lol::vec3& BoxSize) { }
  63. virtual void SetShapeToSphere(float radius) { }
  64. virtual void SetShapeToCone(float radius, float height) { }
  65. virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
  66. virtual void SetShapeToCapsule(float radius, float height) { }
  67. virtual bool CanChangeCollisionChannel() { return true; }
  68. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  69. virtual void SetMass(float mass) { }
  70. virtual void InitBodyToRigid() { }
  71. virtual void InitBodyToGhost() { }
  72. virtual void AddToSimulation(class Simulation* current_simulation) { }
  73. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  74. virtual mat4 GetTransform() { return mat4(1.0f); }
  75. virtual void InitBodyToGhost() { }
  76. #endif // PHYSIC IMPLEMENTATION
  77. public:
  78. //Sets the collision Group & Mask.
  79. //Mask can change at runtime, not group !
  80. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  81. {
  82. if (CanChangeCollisionChannel())
  83. {
  84. m_collision_group = (1<<NewGroup);
  85. m_collision_mask = NewMask;
  86. return true;
  87. }
  88. return false;
  89. }
  90. int GetCollisionGroup() { return m_collision_group; }
  91. int GetCollisionMask() { return m_collision_mask; }
  92. protected:
  93. lol::mat4 m_local_to_world;
  94. float m_mass;
  95. int m_collision_group;
  96. int m_collision_mask;
  97. };
  98. class EasyCharacterController : public EasyPhysic
  99. {
  100. #ifdef HAVE_PHYS_USE_BULLET
  101. public:
  102. EasyCharacterController() :
  103. EasyPhysic(),
  104. m_character(NULL)
  105. {
  106. m_up_axis = 1;
  107. }
  108. ~EasyCharacterController()
  109. {
  110. delete m_character;
  111. }
  112. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  113. virtual void InitBodyToGhost();
  114. virtual void AddToSimulation(class Simulation* current_simulation);
  115. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  116. virtual void SetMovementForFrame(vec3 &MoveQuantity);
  117. protected:
  118. virtual btGhostObject* GetGhostObject();
  119. btPairCachingGhostObject* m_pair_caching_object;
  120. btKinematicCharacterController* m_character;
  121. float m_step_height;
  122. int m_up_axis;
  123. #else // NO PHYSIC IMPLEMENTATION
  124. #endif // PHYSIC IMPLEMENTATION
  125. };
  126. class EasyConstraint
  127. {
  128. #ifdef HAVE_PHYS_USE_BULLET
  129. public:
  130. EasyConstraint() :
  131. m_typed_constraint(NULL),
  132. m_p2p_constraint(NULL),
  133. m_hinge_constraint(NULL),
  134. m_slider_constraint(NULL),
  135. m_cone_twist_constraint(NULL),
  136. m_6dof_constraint(NULL),
  137. m_a_physobj(NULL),
  138. m_b_physobj(NULL),
  139. m_a_transform(lol::mat4(1.f)),
  140. m_b_transform(lol::mat4(1.f)),
  141. m_using_ref_a(false),
  142. m_disable_a2b_collision(false)
  143. {
  144. }
  145. ~EasyConstraint()
  146. {
  147. delete m_typed_constraint;
  148. m_p2p_constraint = NULL;
  149. m_hinge_constraint = NULL;
  150. m_slider_constraint = NULL;
  151. m_cone_twist_constraint = NULL;
  152. m_6dof_constraint = NULL;
  153. }
  154. void AddToSimulation(class Simulation* current_simulation);
  155. void RemoveFromSimulation(class Simulation* current_simulation);
  156. private:
  157. //check if Init can be done
  158. bool CanProceedWithInit()
  159. {
  160. if (!m_a_physobj || !m_b_physobj)
  161. return false;
  162. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  163. return false;
  164. return true;
  165. }
  166. //-------------------------------------------------------------------------
  167. //Init constraint functions
  168. //--
  169. void CustomInitConstraintToPoint2Point()
  170. {
  171. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  172. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  173. m_typed_constraint = m_p2p_constraint;
  174. }
  175. void CustomInitConstraintToHinge()
  176. {
  177. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  178. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  179. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  180. m_using_ref_a);
  181. m_typed_constraint = m_hinge_constraint;
  182. }
  183. void CustomInitConstraintToSlider()
  184. {
  185. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  186. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  187. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  188. m_using_ref_a);
  189. m_typed_constraint = m_slider_constraint;
  190. }
  191. void CustomInitConstraintToConeTwist()
  192. {
  193. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  194. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  195. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  196. m_typed_constraint = m_cone_twist_constraint;
  197. }
  198. void CustomInitConstraintTo6Dof()
  199. {
  200. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  201. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  202. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  203. m_using_ref_a);
  204. m_typed_constraint = m_6dof_constraint;
  205. }
  206. btTypedConstraint* m_typed_constraint;
  207. btPoint2PointConstraint* m_p2p_constraint;
  208. btHingeConstraint* m_hinge_constraint;
  209. btSliderConstraint* m_slider_constraint;
  210. btConeTwistConstraint* m_cone_twist_constraint;
  211. btGeneric6DofConstraint* m_6dof_constraint;
  212. #else // NO PHYSIC IMPLEMENTATION
  213. public:
  214. EasyConstraint() :
  215. m_a_physobj(NULL),
  216. m_b_physobj(NULL),
  217. m_a_transform(lol::mat4(1.f)),
  218. m_b_transform(lol::mat4(1.f)),
  219. m_using_ref_a(false),
  220. m_disable_a2b_collision(false)
  221. {
  222. }
  223. private:
  224. void AddToSimulation(class Simulation* current_simulation) { }
  225. void RemoveFromSimulation(class Simulation* current_simulation) { }
  226. //check if Init can be done
  227. bool CanProceedWithInit() { return false; }
  228. void CustomInitConstraintToPoint2Point() { }
  229. void CustomInitConstraintToHinge() { }
  230. void CustomInitConstraintToSlider() { }
  231. void CustomInitConstraintToConeTwist() { }
  232. void CustomInitConstraintTo6Dof() { }
  233. #endif // PHYSIC IMPLEMENTATION
  234. public:
  235. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  236. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  237. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  238. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  239. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  240. //Set given physic object to the proper slot.
  241. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  242. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  243. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  244. {
  245. if (SetToB)
  246. {
  247. m_b_physobj = NewPhysObj;
  248. m_b_transform = NewTransform;
  249. }
  250. else
  251. {
  252. m_a_physobj = NewPhysObj;
  253. m_a_transform = NewTransform;
  254. }
  255. }
  256. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  257. void SetRefAsA(bool NewUseRefA)
  258. {
  259. m_using_ref_a = NewUseRefA;
  260. }
  261. //Set whether or not to disable the collision between the bodies
  262. void DisableCollisionBetweenObjs(bool DisableCollision)
  263. {
  264. m_disable_a2b_collision = DisableCollision;
  265. }
  266. private:
  267. EasyPhysic* m_a_physobj;
  268. EasyPhysic* m_b_physobj;
  269. lol::mat4 m_a_transform;
  270. lol::mat4 m_b_transform;
  271. bool m_using_ref_a;
  272. bool m_disable_a2b_collision;
  273. };
  274. } /* namespace phys */
  275. } /* namespace lol */
  276. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */