Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

easyconstraint.h 6.7 KiB

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