You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

easyconstraint.h 6.8 KiB

пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // This library is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. //
  15. // The EasyPhysic class
  16. // ------------------
  17. //
  18. #include <lol/engine.h>
  19. #include "easyphysics.h"
  20. namespace lol
  21. {
  22. namespace phys
  23. {
  24. class EasyConstraint
  25. {
  26. friend class Simulation;
  27. friend class EasyPhysic;
  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_owner_simulation(NULL),
  37. m_a_physobj(NULL),
  38. m_b_physobj(NULL),
  39. m_a_transform(lol::mat4(1.f)),
  40. m_b_transform(lol::mat4(1.f)),
  41. m_using_ref_a(false),
  42. m_disable_a2b_collision(false)
  43. {
  44. }
  45. ~EasyConstraint()
  46. {
  47. delete m_typed_constraint;
  48. m_p2p_constraint = NULL;
  49. m_hinge_constraint = NULL;
  50. m_slider_constraint = NULL;
  51. m_cone_twist_constraint = NULL;
  52. m_6dof_constraint = NULL;
  53. }
  54. void AddToSimulation(class Simulation* current_simulation);
  55. void RemoveFromSimulation(class Simulation* current_simulation);
  56. private:
  57. //check if Init can be done
  58. bool CanProceedWithInit()
  59. {
  60. if (!m_a_physobj || !m_b_physobj)
  61. return false;
  62. if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body)
  63. return false;
  64. return true;
  65. }
  66. //-------------------------------------------------------------------------
  67. //Init constraint functions
  68. //--
  69. void CustomInitConstraintToPoint2Point()
  70. {
  71. m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  72. LOL2BT_VEC3(m_a_transform[3].xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform[3].xyz * LOL2BT_UNIT));
  73. m_typed_constraint = m_p2p_constraint;
  74. }
  75. void CustomInitConstraintToHinge()
  76. {
  77. m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  78. btTransform(LOL2BT_QUAT(quat(mat3(m_a_transform))), LOL2BT_VEC3(m_a_transform[3].xyz * LOL2BT_UNIT)),
  79. btTransform(LOL2BT_QUAT(quat(mat3(m_b_transform))), LOL2BT_VEC3(m_b_transform[3].xyz * LOL2BT_UNIT)),
  80. m_using_ref_a);
  81. m_typed_constraint = m_hinge_constraint;
  82. }
  83. void CustomInitConstraintToSlider()
  84. {
  85. m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  86. btTransform(LOL2BT_QUAT(quat(mat3(m_a_transform))), LOL2BT_VEC3(m_a_transform[3].xyz * LOL2BT_UNIT)),
  87. btTransform(LOL2BT_QUAT(quat(mat3(m_b_transform))), LOL2BT_VEC3(m_b_transform[3].xyz * LOL2BT_UNIT)),
  88. m_using_ref_a);
  89. m_typed_constraint = m_slider_constraint;
  90. }
  91. void CustomInitConstraintToConeTwist()
  92. {
  93. m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  94. btTransform(LOL2BT_QUAT(quat(mat3(m_a_transform))), LOL2BT_VEC3(m_a_transform[3].xyz * LOL2BT_UNIT)),
  95. btTransform(LOL2BT_QUAT(quat(mat3(m_b_transform))), LOL2BT_VEC3(m_b_transform[3].xyz * LOL2BT_UNIT)));
  96. m_typed_constraint = m_cone_twist_constraint;
  97. }
  98. void CustomInitConstraintTo6Dof()
  99. {
  100. m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body,
  101. btTransform(LOL2BT_QUAT(quat(mat3(m_a_transform))), LOL2BT_VEC3(m_a_transform[3].xyz * LOL2BT_UNIT)),
  102. btTransform(LOL2BT_QUAT(quat(mat3(m_b_transform))), LOL2BT_VEC3(m_b_transform[3].xyz * LOL2BT_UNIT)),
  103. m_using_ref_a);
  104. m_typed_constraint = m_6dof_constraint;
  105. }
  106. btTypedConstraint* m_typed_constraint;
  107. btPoint2PointConstraint* m_p2p_constraint;
  108. btHingeConstraint* m_hinge_constraint;
  109. btSliderConstraint* m_slider_constraint;
  110. btConeTwistConstraint* m_cone_twist_constraint;
  111. btGeneric6DofConstraint* m_6dof_constraint;
  112. public:
  113. void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }
  114. void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); }
  115. void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); }
  116. void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }
  117. void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }
  118. //Set given physic object to the proper slot.
  119. void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); }
  120. void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); }
  121. void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform)
  122. {
  123. if (SetToB)
  124. {
  125. m_b_physobj = NewPhysObj;
  126. m_b_transform = NewTransform;
  127. }
  128. else
  129. {
  130. m_a_physobj = NewPhysObj;
  131. m_a_transform = NewTransform;
  132. }
  133. }
  134. //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local).
  135. void SetRefAsA(bool NewUseRefA)
  136. {
  137. m_using_ref_a = NewUseRefA;
  138. }
  139. //Set whether or not to disable the collision between the bodies
  140. void DisableCollisionBetweenObjs(bool DisableCollision)
  141. {
  142. m_disable_a2b_collision = DisableCollision;
  143. }
  144. private:
  145. Simulation* m_owner_simulation;
  146. EasyPhysic* m_a_physobj;
  147. EasyPhysic* m_b_physobj;
  148. lol::mat4 m_a_transform;
  149. lol::mat4 m_b_transform;
  150. bool m_using_ref_a;
  151. bool m_disable_a2b_collision;
  152. };
  153. } /* namespace phys */
  154. } /* namespace lol */