Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

185 lignes
6.8 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. #include <lol/main.h>
  18. #include "easyphysics.h"
  19. namespace lol
  20. {
  21. namespace phys
  22. {
  23. class EasyConstraint
  24. {
  25. friend class Simulation;
  26. friend class EasyPhysic;
  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_owner_simulation(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[3].xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform[3].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[3].xyz * LOL2BT_UNIT)),
  78. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform[3].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[3].xyz * LOL2BT_UNIT)),
  86. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform[3].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[3].xyz * LOL2BT_UNIT)),
  94. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform[3].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[3].xyz * LOL2BT_UNIT)),
  101. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform[3].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. public:
  112. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  113. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  114. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  115. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  116. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  117. //Set given physic object to the proper slot.
  118. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  119. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  120. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  121. {
  122. if (SetToB)
  123. {
  124. m_b_physobj = NewPhysObj;
  125. m_b_transform = NewTransform;
  126. }
  127. else
  128. {
  129. m_a_physobj = NewPhysObj;
  130. m_a_transform = NewTransform;
  131. }
  132. }
  133. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  134. void SetRefAsA(bool NewUseRefA)
  135. {
  136. m_using_ref_a = NewUseRefA;
  137. }
  138. //Set whether or not to disable the collision between the bodies
  139. void DisableCollisionBetweenObjs(bool DisableCollision)
  140. {
  141. m_disable_a2b_collision = DisableCollision;
  142. }
  143. private:
  144. Simulation* m_owner_simulation;
  145. EasyPhysic* m_a_physobj;
  146. EasyPhysic* m_b_physobj;
  147. lol::mat4 m_a_transform;
  148. lol::mat4 m_b_transform;
  149. bool m_using_ref_a;
  150. bool m_disable_a2b_collision;
  151. };
  152. } /* namespace phys */
  153. } /* namespace lol */
  154. #endif /* __EASYCONSTRAINT_EASYCONSTRAINT_H__ */