You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

325 lines
9.7 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. #endif
  23. namespace lol
  24. {
  25. namespace phys
  26. {
  27. class EasyPhysic
  28. {
  29. friend class EasyConstraint;
  30. #ifdef HAVE_PHYS_USE_BULLET
  31. public:
  32. EasyPhysic();
  33. ~EasyPhysic();
  34. virtual void SetShapeToBox(lol::vec3& box_size);
  35. virtual void SetShapeToSphere(float radius);
  36. virtual void SetShapeToCone(float radius, float height);
  37. virtual void SetShapeToCylinder(lol::vec3& cyl_size);
  38. virtual void SetShapeToCapsule(float radius, float height);
  39. virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  40. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  41. virtual void SetMass(float mass);
  42. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  43. virtual void InitBodyToGhost();
  44. virtual void AddToSimulation(class Simulation* current_simulation);
  45. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  46. virtual mat4 GetTransform();
  47. protected:
  48. virtual void SetLocalInertia(float mass);
  49. virtual void SetShapeTo(btCollisionShape* collision_shape);
  50. btCollisionObject* m_collision_object;
  51. btGhostObject* m_ghost_object;
  52. btRigidBody* m_rigid_body;
  53. btVector3 m_local_inertia;
  54. btCollisionShape* m_collision_shape;
  55. btMotionState* m_motion_state;
  56. #else // NO PHYSIC IMPLEMENTATION
  57. public:
  58. EasyPhysic() { }
  59. virtual void SetShapeToBox(lol::vec3& BoxSize) { }
  60. virtual void SetShapeToSphere(float radius) { }
  61. virtual void SetShapeToCone(float radius, float height) { }
  62. virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
  63. virtual void SetShapeToCapsule(float radius, float height) { }
  64. virtual bool CanChangeCollisionChannel() { return true; }
  65. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  66. virtual void SetMass(float mass) { }
  67. virtual void InitBodyToRigid() { }
  68. virtual void InitBodyToGhost() { }
  69. virtual void AddToSimulation(class Simulation* current_simulation) { }
  70. virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
  71. virtual mat4 GetTransform() { return mat4(1.0f); }
  72. #endif // PHYSIC IMPLEMENTATION
  73. public:
  74. //Sets the collision Group & Mask.
  75. //Mask can change at runtime, not group !
  76. virtual bool SetCollisionChannel(int NewGroup, int NewMask)
  77. {
  78. if (CanChangeCollisionChannel())
  79. {
  80. m_collision_group = (1<<NewGroup);
  81. m_collision_mask = NewMask;
  82. return true;
  83. }
  84. return false;
  85. }
  86. int GetCollisionGroup() { return m_collision_group; }
  87. int GetCollisionMask() { return m_collision_mask; }
  88. protected:
  89. lol::mat4 m_local_to_world;
  90. float m_mass;
  91. int m_collision_group;
  92. int m_collision_mask;
  93. };
  94. class EasyCharacterController : public EasyPhysic
  95. {
  96. #ifdef HAVE_PHYS_USE_BULLET
  97. public:
  98. EasyCharacterController();
  99. ~EasyCharacterController();
  100. virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  101. virtual void SetMass(float mass);
  102. virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  103. virtual void InitBodyToGhost();
  104. virtual void AddToSimulation(class Simulation* current_simulation);
  105. virtual void RemoveFromSimulation(class Simulation* current_simulation);
  106. virtual mat4 GetTransform();
  107. protected:
  108. btPairCachingGhostObject* m_pair_caching_object;
  109. #else // NO PHYSIC IMPLEMENTATION
  110. #endif // PHYSIC IMPLEMENTATION
  111. };
  112. class EasyConstraint
  113. {
  114. #ifdef HAVE_PHYS_USE_BULLET
  115. public:
  116. EasyConstraint() :
  117. m_typed_constraint(NULL),
  118. m_p2p_constraint(NULL),
  119. m_hinge_constraint(NULL),
  120. m_slider_constraint(NULL),
  121. m_cone_twist_constraint(NULL),
  122. m_6dof_constraint(NULL),
  123. m_a_physobj(NULL),
  124. m_b_physobj(NULL),
  125. m_a_transform(lol::mat4(1.f)),
  126. m_b_transform(lol::mat4(1.f)),
  127. m_using_ref_a(false),
  128. m_disable_a2b_collision(false)
  129. {
  130. }
  131. ~EasyConstraint()
  132. {
  133. delete m_typed_constraint;
  134. m_p2p_constraint = NULL;
  135. m_hinge_constraint = NULL;
  136. m_slider_constraint = NULL;
  137. m_cone_twist_constraint = NULL;
  138. m_6dof_constraint = NULL;
  139. }
  140. void AddToSimulation(class Simulation* current_simulation);
  141. void RemoveFromSimulation(class Simulation* current_simulation);
  142. private:
  143. //check if Init can be done
  144. bool CanProceedWithInit()
  145. {
  146. if (!m_a_physobj || !m_b_physobj)
  147. return false;
  148. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  149. return false;
  150. return true;
  151. }
  152. //-------------------------------------------------------------------------
  153. //Init constraint functions
  154. //--
  155. void CustomInitConstraintToPoint2Point()
  156. {
  157. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  158. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  159. m_typed_constraint = m_p2p_constraint;
  160. }
  161. void CustomInitConstraintToHinge()
  162. {
  163. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  164. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  165. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  166. m_using_ref_a);
  167. m_typed_constraint = m_hinge_constraint;
  168. }
  169. void CustomInitConstraintToSlider()
  170. {
  171. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  172. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  173. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  174. m_using_ref_a);
  175. m_typed_constraint = m_slider_constraint;
  176. }
  177. void CustomInitConstraintToConeTwist()
  178. {
  179. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  180. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  181. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  182. m_typed_constraint = m_cone_twist_constraint;
  183. }
  184. void CustomInitConstraintTo6Dof()
  185. {
  186. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  187. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  188. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  189. m_using_ref_a);
  190. m_typed_constraint = m_6dof_constraint;
  191. }
  192. btTypedConstraint* m_typed_constraint;
  193. btPoint2PointConstraint* m_p2p_constraint;
  194. btHingeConstraint* m_hinge_constraint;
  195. btSliderConstraint* m_slider_constraint;
  196. btConeTwistConstraint* m_cone_twist_constraint;
  197. btGeneric6DofConstraint* m_6dof_constraint;
  198. #else // NO PHYSIC IMPLEMENTATION
  199. public:
  200. EasyConstraint() :
  201. m_a_physobj(NULL),
  202. m_b_physobj(NULL),
  203. m_a_transform(lol::mat4(1.f)),
  204. m_b_transform(lol::mat4(1.f)),
  205. m_using_ref_a(false),
  206. m_disable_a2b_collision(false)
  207. {
  208. }
  209. private:
  210. void AddToSimulation(class Simulation* current_simulation) { }
  211. void RemoveFromSimulation(class Simulation* current_simulation) { }
  212. //check if Init can be done
  213. bool CanProceedWithInit() { return false; }
  214. void CustomInitConstraintToPoint2Point() { }
  215. void CustomInitConstraintToHinge() { }
  216. void CustomInitConstraintToSlider() { }
  217. void CustomInitConstraintToConeTwist() { }
  218. void CustomInitConstraintTo6Dof() { }
  219. #endif // PHYSIC IMPLEMENTATION
  220. public:
  221. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  222. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  223. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  224. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  225. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  226. //Set given physic object to the proper slot.
  227. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  228. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  229. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  230. {
  231. if (SetToB)
  232. {
  233. m_b_physobj = NewPhysObj;
  234. m_b_transform = NewTransform;
  235. }
  236. else
  237. {
  238. m_a_physobj = NewPhysObj;
  239. m_a_transform = NewTransform;
  240. }
  241. }
  242. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  243. void SetRefAsA(bool NewUseRefA)
  244. {
  245. m_using_ref_a = NewUseRefA;
  246. }
  247. //Set whether or not to disable the collision between the bodies
  248. void DisableCollisionBetweenObjs(bool DisableCollision)
  249. {
  250. m_disable_a2b_collision = DisableCollision;
  251. }
  252. private:
  253. EasyPhysic* m_a_physobj;
  254. EasyPhysic* m_b_physobj;
  255. lol::mat4 m_a_transform;
  256. lol::mat4 m_b_transform;
  257. bool m_using_ref_a;
  258. bool m_disable_a2b_collision;
  259. };
  260. } /* namespace phys */
  261. } /* namespace lol */
  262. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */