Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

185 рядки
6.9 KiB

  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(nullptr),
  31. m_p2p_constraint(nullptr),
  32. m_hinge_constraint(nullptr),
  33. m_slider_constraint(nullptr),
  34. m_cone_twist_constraint(nullptr),
  35. m_6dof_constraint(nullptr),
  36. m_owner_simulation(nullptr),
  37. m_a_physobj(nullptr),
  38. m_b_physobj(nullptr),
  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 = nullptr;
  49. m_hinge_constraint = nullptr;
  50. m_slider_constraint = nullptr;
  51. m_cone_twist_constraint = nullptr;
  52. m_6dof_constraint = nullptr;
  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 */