Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12 лет назад
12 лет назад
12 лет назад
12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "core.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.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.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.v3.xyz * LOL2BT_UNIT)),
  78. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.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.v3.xyz * LOL2BT_UNIT)),
  86. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.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.v3.xyz * LOL2BT_UNIT)),
  94. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.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.v3.xyz * LOL2BT_UNIT)),
  101. btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.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__ */