Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

298 řádky
8.8 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. void SetShapeToBox(lol::vec3& box_size);
  35. void SetShapeToSphere(float radius);
  36. void SetShapeToCone(float radius, float height);
  37. void SetShapeToCylinder(lol::vec3& cyl_size);
  38. void SetShapeToCapsule(float radius, float height);
  39. bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
  40. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
  41. void SetMass(float mass);
  42. void InitBodyToRigid(bool ZeroMassIsKinematic=false);
  43. void InitBodyToGhost();
  44. void AddToSimulation(class Simulation* current_simulation);
  45. void RemoveFromSimulation(class Simulation* current_simulation);
  46. mat4 GetTransform();
  47. protected:
  48. void SetLocalInertia(float mass);
  49. 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. void SetShapeToBox(lol::vec3& BoxSize) { }
  60. void SetShapeToSphere(float radius) { }
  61. void SetShapeToCone(float radius, float height) { }
  62. void SetShapeToCylinder(lol::vec3& cyl_size) { }
  63. void SetShapeToCapsule(float radius, float height) { }
  64. bool CanChangeCollisionChannel() { return true; }
  65. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
  66. void SetMass(float mass) { }
  67. void InitBodyToRigid() { }
  68. void InitBodyToGhost() { }
  69. void AddToSimulation(class Simulation* current_simulation) { }
  70. void RemoveFromSimulation(class Simulation* current_simulation) { }
  71. 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. 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 EasyConstraint
  95. {
  96. #ifdef HAVE_PHYS_USE_BULLET
  97. public:
  98. EasyConstraint() :
  99. m_typed_constraint(NULL),
  100. m_p2p_constraint(NULL),
  101. m_hinge_constraint(NULL),
  102. m_slider_constraint(NULL),
  103. m_cone_twist_constraint(NULL),
  104. m_6dof_constraint(NULL),
  105. m_a_physobj(NULL),
  106. m_b_physobj(NULL),
  107. m_a_transform(lol::mat4(1.f)),
  108. m_b_transform(lol::mat4(1.f)),
  109. m_using_ref_a(false),
  110. m_disable_a2b_collision(false)
  111. {
  112. }
  113. ~EasyConstraint()
  114. {
  115. delete m_typed_constraint;
  116. m_p2p_constraint = NULL;
  117. m_hinge_constraint = NULL;
  118. m_slider_constraint = NULL;
  119. m_cone_twist_constraint = NULL;
  120. m_6dof_constraint = NULL;
  121. }
  122. void AddToSimulation(class Simulation* current_simulation);
  123. void RemoveFromSimulation(class Simulation* current_simulation);
  124. private:
  125. //check if Init can be done
  126. bool CanProceedWithInit()
  127. {
  128. if (!m_a_physobj || !m_b_physobj)
  129. return false;
  130. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  131. return false;
  132. return true;
  133. }
  134. //-------------------------------------------------------------------------
  135. //Init constraint functions
  136. //--
  137. void CustomInitConstraintToPoint2Point()
  138. {
  139. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  140. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  141. m_typed_constraint = m_p2p_constraint;
  142. }
  143. void CustomInitConstraintToHinge()
  144. {
  145. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  146. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  147. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  148. m_using_ref_a);
  149. m_typed_constraint = m_hinge_constraint;
  150. }
  151. void CustomInitConstraintToSlider()
  152. {
  153. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  154. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  155. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  156. m_using_ref_a);
  157. m_typed_constraint = m_slider_constraint;
  158. }
  159. void CustomInitConstraintToConeTwist()
  160. {
  161. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  162. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  163. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  164. m_typed_constraint = m_cone_twist_constraint;
  165. }
  166. void CustomInitConstraintTo6Dof()
  167. {
  168. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  169. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  170. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  171. m_using_ref_a);
  172. m_typed_constraint = m_6dof_constraint;
  173. }
  174. btTypedConstraint* m_typed_constraint;
  175. btPoint2PointConstraint* m_p2p_constraint;
  176. btHingeConstraint* m_hinge_constraint;
  177. btSliderConstraint* m_slider_constraint;
  178. btConeTwistConstraint* m_cone_twist_constraint;
  179. btGeneric6DofConstraint* m_6dof_constraint;
  180. #else // NO PHYSIC IMPLEMENTATION
  181. public:
  182. EasyConstraint() :
  183. m_a_physobj(NULL),
  184. m_b_physobj(NULL),
  185. m_a_transform(lol::mat4(1.f)),
  186. m_b_transform(lol::mat4(1.f)),
  187. m_using_ref_a(false),
  188. m_disable_a2b_collision(false)
  189. {
  190. }
  191. private:
  192. void AddToSimulation(class Simulation* current_simulation) { }
  193. void RemoveFromSimulation(class Simulation* current_simulation) { }
  194. //check if Init can be done
  195. bool CanProceedWithInit() { return false; }
  196. void CustomInitConstraintToPoint2Point() { }
  197. void CustomInitConstraintToHinge() { }
  198. void CustomInitConstraintToSlider() { }
  199. void CustomInitConstraintToConeTwist() { }
  200. void CustomInitConstraintTo6Dof() { }
  201. #endif // PHYSIC IMPLEMENTATION
  202. public:
  203. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  204. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  205. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  206. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  207. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  208. //Set given physic object to the proper slot.
  209. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  210. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  211. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  212. {
  213. if (SetToB)
  214. {
  215. m_b_physobj = NewPhysObj;
  216. m_b_transform = NewTransform;
  217. }
  218. else
  219. {
  220. m_a_physobj = NewPhysObj;
  221. m_a_transform = NewTransform;
  222. }
  223. }
  224. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  225. void SetRefAsA(bool NewUseRefA)
  226. {
  227. m_using_ref_a = NewUseRefA;
  228. }
  229. //Set whether or not to disable the collision between the bodies
  230. void DisableCollisionBetweenObjs(bool DisableCollision)
  231. {
  232. m_disable_a2b_collision = DisableCollision;
  233. }
  234. private:
  235. EasyPhysic* m_a_physobj;
  236. EasyPhysic* m_b_physobj;
  237. lol::mat4 m_a_transform;
  238. lol::mat4 m_b_transform;
  239. bool m_using_ref_a;
  240. bool m_disable_a2b_collision;
  241. };
  242. } /* namespace phys */
  243. } /* namespace lol */
  244. #endif /* __EASYPHYSICS_EASYPHYSICS_H__ */