25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

217 lines
7.6 KiB

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