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ů.
 
 
 

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