b2RopeJoint.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2007-2011 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Dynamics/Joints/b2RopeJoint.h>
  19. #include <Box2D/Dynamics/b2Body.h>
  20. #include <Box2D/Dynamics/b2TimeStep.h>
  21. // Limit:
  22. // C = norm(pB - pA) - L
  23. // u = (pB - pA) / norm(pB - pA)
  24. // Cdot = dot(u, vB + cross(wB, rB) - vA - cross(wA, rA))
  25. // J = [-u -cross(rA, u) u cross(rB, u)]
  26. // K = J * invM * JT
  27. // = invMassA + invIA * cross(rA, u)^2 + invMassB + invIB * cross(rB, u)^2
  28. b2RopeJoint::b2RopeJoint(const b2RopeJointDef* def)
  29. : b2Joint(def)
  30. {
  31. m_localAnchorA = def->localAnchorA;
  32. m_localAnchorB = def->localAnchorB;
  33. m_maxLength = def->maxLength;
  34. m_mass = 0.0f;
  35. m_impulse = 0.0f;
  36. m_state = e_inactiveLimit;
  37. m_length = 0.0f;
  38. }
  39. void b2RopeJoint::InitVelocityConstraints(const b2SolverData& data)
  40. {
  41. m_indexA = m_bodyA->m_islandIndex;
  42. m_indexB = m_bodyB->m_islandIndex;
  43. m_localCenterA = m_bodyA->m_sweep.localCenter;
  44. m_localCenterB = m_bodyB->m_sweep.localCenter;
  45. m_invMassA = m_bodyA->m_invMass;
  46. m_invMassB = m_bodyB->m_invMass;
  47. m_invIA = m_bodyA->m_invI;
  48. m_invIB = m_bodyB->m_invI;
  49. b2Vec2 cA = data.positions[m_indexA].c;
  50. float32 aA = data.positions[m_indexA].a;
  51. b2Vec2 vA = data.velocities[m_indexA].v;
  52. float32 wA = data.velocities[m_indexA].w;
  53. b2Vec2 cB = data.positions[m_indexB].c;
  54. float32 aB = data.positions[m_indexB].a;
  55. b2Vec2 vB = data.velocities[m_indexB].v;
  56. float32 wB = data.velocities[m_indexB].w;
  57. b2Rot qA(aA), qB(aB);
  58. m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  59. m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  60. m_u = cB + m_rB - cA - m_rA;
  61. m_length = m_u.Length();
  62. float32 C = m_length - m_maxLength;
  63. if (C > 0.0f)
  64. {
  65. m_state = e_atUpperLimit;
  66. }
  67. else
  68. {
  69. m_state = e_inactiveLimit;
  70. }
  71. if (m_length > b2_linearSlop)
  72. {
  73. m_u *= 1.0f / m_length;
  74. }
  75. else
  76. {
  77. m_u.SetZero();
  78. m_mass = 0.0f;
  79. m_impulse = 0.0f;
  80. return;
  81. }
  82. // Compute effective mass.
  83. float32 crA = b2Cross(m_rA, m_u);
  84. float32 crB = b2Cross(m_rB, m_u);
  85. float32 invMass = m_invMassA + m_invIA * crA * crA + m_invMassB + m_invIB * crB * crB;
  86. m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  87. if (data.step.warmStarting)
  88. {
  89. // Scale the impulse to support a variable time step.
  90. m_impulse *= data.step.dtRatio;
  91. b2Vec2 P = m_impulse * m_u;
  92. vA -= m_invMassA * P;
  93. wA -= m_invIA * b2Cross(m_rA, P);
  94. vB += m_invMassB * P;
  95. wB += m_invIB * b2Cross(m_rB, P);
  96. }
  97. else
  98. {
  99. m_impulse = 0.0f;
  100. }
  101. data.velocities[m_indexA].v = vA;
  102. data.velocities[m_indexA].w = wA;
  103. data.velocities[m_indexB].v = vB;
  104. data.velocities[m_indexB].w = wB;
  105. }
  106. void b2RopeJoint::SolveVelocityConstraints(const b2SolverData& data)
  107. {
  108. b2Vec2 vA = data.velocities[m_indexA].v;
  109. float32 wA = data.velocities[m_indexA].w;
  110. b2Vec2 vB = data.velocities[m_indexB].v;
  111. float32 wB = data.velocities[m_indexB].w;
  112. // Cdot = dot(u, v + cross(w, r))
  113. b2Vec2 vpA = vA + b2Cross(wA, m_rA);
  114. b2Vec2 vpB = vB + b2Cross(wB, m_rB);
  115. float32 C = m_length - m_maxLength;
  116. float32 Cdot = b2Dot(m_u, vpB - vpA);
  117. // Predictive constraint.
  118. if (C < 0.0f)
  119. {
  120. Cdot += data.step.inv_dt * C;
  121. }
  122. float32 impulse = -m_mass * Cdot;
  123. float32 oldImpulse = m_impulse;
  124. m_impulse = b2Min(0.0f, m_impulse + impulse);
  125. impulse = m_impulse - oldImpulse;
  126. b2Vec2 P = impulse * m_u;
  127. vA -= m_invMassA * P;
  128. wA -= m_invIA * b2Cross(m_rA, P);
  129. vB += m_invMassB * P;
  130. wB += m_invIB * b2Cross(m_rB, P);
  131. data.velocities[m_indexA].v = vA;
  132. data.velocities[m_indexA].w = wA;
  133. data.velocities[m_indexB].v = vB;
  134. data.velocities[m_indexB].w = wB;
  135. }
  136. bool b2RopeJoint::SolvePositionConstraints(const b2SolverData& data)
  137. {
  138. b2Vec2 cA = data.positions[m_indexA].c;
  139. float32 aA = data.positions[m_indexA].a;
  140. b2Vec2 cB = data.positions[m_indexB].c;
  141. float32 aB = data.positions[m_indexB].a;
  142. b2Rot qA(aA), qB(aB);
  143. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  144. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  145. b2Vec2 u = cB + rB - cA - rA;
  146. float32 length = u.Normalize();
  147. float32 C = length - m_maxLength;
  148. C = b2Clamp(C, 0.0f, b2_maxLinearCorrection);
  149. float32 impulse = -m_mass * C;
  150. b2Vec2 P = impulse * u;
  151. cA -= m_invMassA * P;
  152. aA -= m_invIA * b2Cross(rA, P);
  153. cB += m_invMassB * P;
  154. aB += m_invIB * b2Cross(rB, P);
  155. data.positions[m_indexA].c = cA;
  156. data.positions[m_indexA].a = aA;
  157. data.positions[m_indexB].c = cB;
  158. data.positions[m_indexB].a = aB;
  159. return length - m_maxLength < b2_linearSlop;
  160. }
  161. b2Vec2 b2RopeJoint::GetAnchorA() const
  162. {
  163. return m_bodyA->GetWorldPoint(m_localAnchorA);
  164. }
  165. b2Vec2 b2RopeJoint::GetAnchorB() const
  166. {
  167. return m_bodyB->GetWorldPoint(m_localAnchorB);
  168. }
  169. b2Vec2 b2RopeJoint::GetReactionForce(float32 inv_dt) const
  170. {
  171. b2Vec2 F = (inv_dt * m_impulse) * m_u;
  172. return F;
  173. }
  174. float32 b2RopeJoint::GetReactionTorque(float32 inv_dt) const
  175. {
  176. B2_NOT_USED(inv_dt);
  177. return 0.0f;
  178. }
  179. float32 b2RopeJoint::GetMaxLength() const
  180. {
  181. return m_maxLength;
  182. }
  183. b2LimitState b2RopeJoint::GetLimitState() const
  184. {
  185. return m_state;
  186. }
  187. void b2RopeJoint::Dump()
  188. {
  189. int32 indexA = m_bodyA->m_islandIndex;
  190. int32 indexB = m_bodyB->m_islandIndex;
  191. b2Log(" b2RopeJointDef jd;\n");
  192. b2Log(" jd.bodyA = bodies[%d];\n", indexA);
  193. b2Log(" jd.bodyB = bodies[%d];\n", indexB);
  194. b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  195. b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
  196. b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
  197. b2Log(" jd.maxLength = %.15lef;\n", m_maxLength);
  198. b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  199. }