210 行
6.3 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 __EASYCONSTRAINT_EASYCONSTRAINT_H__
  16. #define __EASYCONSTRAINT_EASYCONSTRAINT_H__
  17. #ifdef HAVE_PHYS_USE_BULLET
  18. #include "EasyPhysics.h"
  19. #endif
  20. namespace lol
  21. {
  22. namespace phys
  23. {
  24. class EasyConstraint
  25. {
  26. #ifdef HAVE_PHYS_USE_BULLET
  27. public:
  28. EasyConstraint() :
  29. m_typed_constraint(NULL),
  30. m_p2p_constraint(NULL),
  31. m_hinge_constraint(NULL),
  32. m_slider_constraint(NULL),
  33. m_cone_twist_constraint(NULL),
  34. m_6dof_constraint(NULL),
  35. m_a_physobj(NULL),
  36. m_b_physobj(NULL),
  37. m_a_transform(lol::mat4(1.f)),
  38. m_b_transform(lol::mat4(1.f)),
  39. m_using_ref_a(false),
  40. m_disable_a2b_collision(false)
  41. {
  42. }
  43. ~EasyConstraint()
  44. {
  45. delete m_typed_constraint;
  46. m_p2p_constraint = NULL;
  47. m_hinge_constraint = NULL;
  48. m_slider_constraint = NULL;
  49. m_cone_twist_constraint = NULL;
  50. m_6dof_constraint = NULL;
  51. }
  52. void AddToSimulation(class Simulation* current_simulation);
  53. void RemoveFromSimulation(class Simulation* current_simulation);
  54. private:
  55. //check if Init can be done
  56. bool CanProceedWithInit()
  57. {
  58. if (!m_a_physobj || !m_b_physobj)
  59. return false;
  60. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  61. return false;
  62. return true;
  63. }
  64. //-------------------------------------------------------------------------
  65. //Init constraint functions
  66. //--
  67. void CustomInitConstraintToPoint2Point()
  68. {
  69. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  70. LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT));
  71. m_typed_constraint = m_p2p_constraint;
  72. }
  73. void CustomInitConstraintToHinge()
  74. {
  75. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  76. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  77. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  78. m_using_ref_a);
  79. m_typed_constraint = m_hinge_constraint;
  80. }
  81. void CustomInitConstraintToSlider()
  82. {
  83. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  84. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  85. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  86. m_using_ref_a);
  87. m_typed_constraint = m_slider_constraint;
  88. }
  89. void CustomInitConstraintToConeTwist()
  90. {
  91. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  92. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  93. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)));
  94. m_typed_constraint = m_cone_twist_constraint;
  95. }
  96. void CustomInitConstraintTo6Dof()
  97. {
  98. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  99. btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)),
  100. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)),
  101. m_using_ref_a);
  102. m_typed_constraint = m_6dof_constraint;
  103. }
  104. btTypedConstraint* m_typed_constraint;
  105. btPoint2PointConstraint* m_p2p_constraint;
  106. btHingeConstraint* m_hinge_constraint;
  107. btSliderConstraint* m_slider_constraint;
  108. btConeTwistConstraint* m_cone_twist_constraint;
  109. btGeneric6DofConstraint* m_6dof_constraint;
  110. #else // NO PHYSIC IMPLEMENTATION
  111. public:
  112. EasyConstraint() :
  113. m_a_physobj(NULL),
  114. m_b_physobj(NULL),
  115. m_a_transform(lol::mat4(1.f)),
  116. m_b_transform(lol::mat4(1.f)),
  117. m_using_ref_a(false),
  118. m_disable_a2b_collision(false)
  119. {
  120. }
  121. private:
  122. void AddToSimulation(class Simulation* current_simulation) { }
  123. void RemoveFromSimulation(class Simulation* current_simulation) { }
  124. //check if Init can be done
  125. bool CanProceedWithInit() { return false; }
  126. void CustomInitConstraintToPoint2Point() { }
  127. void CustomInitConstraintToHinge() { }
  128. void CustomInitConstraintToSlider() { }
  129. void CustomInitConstraintToConeTwist() { }
  130. void CustomInitConstraintTo6Dof() { }
  131. #endif // PHYSIC IMPLEMENTATION
  132. public:
  133. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  134. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  135. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  136. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  137. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  138. //Set given physic object to the proper slot.
  139. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  140. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  141. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  142. {
  143. if (SetToB)
  144. {
  145. m_b_physobj = NewPhysObj;
  146. m_b_transform = NewTransform;
  147. }
  148. else
  149. {
  150. m_a_physobj = NewPhysObj;
  151. m_a_transform = NewTransform;
  152. }
  153. }
  154. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  155. void SetRefAsA(bool NewUseRefA)
  156. {
  157. m_using_ref_a = NewUseRefA;
  158. }
  159. //Set whether or not to disable the collision between the bodies
  160. void DisableCollisionBetweenObjs(bool DisableCollision)
  161. {
  162. m_disable_a2b_collision = DisableCollision;
  163. }
  164. private:
  165. EasyPhysic* m_a_physobj;
  166. EasyPhysic* m_b_physobj;
  167. lol::mat4 m_a_transform;
  168. lol::mat4 m_b_transform;
  169. bool m_using_ref_a;
  170. bool m_disable_a2b_collision;
  171. };
  172. } /* namespace phys */
  173. } /* namespace lol */
  174. #endif /* __EASYCONSTRAINT_EASYCONSTRAINT_H__ */