b2DistanceJoint.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-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/b2DistanceJoint.h>
  19. #include <Box2D/Dynamics/b2Body.h>
  20. #include <Box2D/Dynamics/b2TimeStep.h>
  21. // 1-D constrained system
  22. // m (v2 - v1) = lambda
  23. // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
  24. // x2 = x1 + h * v2
  25. // 1-D mass-damper-spring system
  26. // m (v2 - v1) + h * d * v2 + h * k *
  27. // C = norm(p2 - p1) - L
  28. // u = (p2 - p1) / norm(p2 - p1)
  29. // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  30. // J = [-u -cross(r1, u) u cross(r2, u)]
  31. // K = J * invM * JT
  32. // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
  33. void b2DistanceJointDef::Initialize(b2Body* b1, b2Body* b2,
  34. const b2Vec2& anchor1, const b2Vec2& anchor2)
  35. {
  36. bodyA = b1;
  37. bodyB = b2;
  38. localAnchorA = bodyA->GetLocalPoint(anchor1);
  39. localAnchorB = bodyB->GetLocalPoint(anchor2);
  40. b2Vec2 d = anchor2 - anchor1;
  41. length = d.Length();
  42. }
  43. b2DistanceJoint::b2DistanceJoint(const b2DistanceJointDef* def)
  44. : b2Joint(def)
  45. {
  46. m_localAnchorA = def->localAnchorA;
  47. m_localAnchorB = def->localAnchorB;
  48. m_length = def->length;
  49. m_frequencyHz = def->frequencyHz;
  50. m_dampingRatio = def->dampingRatio;
  51. m_impulse = 0.0f;
  52. m_gamma = 0.0f;
  53. m_bias = 0.0f;
  54. }
  55. void b2DistanceJoint::InitVelocityConstraints(const b2SolverData& data)
  56. {
  57. m_indexA = m_bodyA->m_islandIndex;
  58. m_indexB = m_bodyB->m_islandIndex;
  59. m_localCenterA = m_bodyA->m_sweep.localCenter;
  60. m_localCenterB = m_bodyB->m_sweep.localCenter;
  61. m_invMassA = m_bodyA->m_invMass;
  62. m_invMassB = m_bodyB->m_invMass;
  63. m_invIA = m_bodyA->m_invI;
  64. m_invIB = m_bodyB->m_invI;
  65. b2Vec2 cA = data.positions[m_indexA].c;
  66. float32 aA = data.positions[m_indexA].a;
  67. b2Vec2 vA = data.velocities[m_indexA].v;
  68. float32 wA = data.velocities[m_indexA].w;
  69. b2Vec2 cB = data.positions[m_indexB].c;
  70. float32 aB = data.positions[m_indexB].a;
  71. b2Vec2 vB = data.velocities[m_indexB].v;
  72. float32 wB = data.velocities[m_indexB].w;
  73. b2Rot qA(aA), qB(aB);
  74. m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  75. m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  76. m_u = cB + m_rB - cA - m_rA;
  77. // Handle singularity.
  78. float32 length = m_u.Length();
  79. if (length > b2_linearSlop)
  80. {
  81. m_u *= 1.0f / length;
  82. }
  83. else
  84. {
  85. m_u.Set(0.0f, 0.0f);
  86. }
  87. float32 crAu = b2Cross(m_rA, m_u);
  88. float32 crBu = b2Cross(m_rB, m_u);
  89. float32 invMass = m_invMassA + m_invIA * crAu * crAu + m_invMassB + m_invIB * crBu * crBu;
  90. // Compute the effective mass matrix.
  91. m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  92. if (m_frequencyHz > 0.0f)
  93. {
  94. float32 C = length - m_length;
  95. // Frequency
  96. float32 omega = 2.0f * b2_pi * m_frequencyHz;
  97. // Damping coefficient
  98. float32 d = 2.0f * m_mass * m_dampingRatio * omega;
  99. // Spring stiffness
  100. float32 k = m_mass * omega * omega;
  101. // magic formulas
  102. float32 h = data.step.dt;
  103. m_gamma = h * (d + h * k);
  104. m_gamma = m_gamma != 0.0f ? 1.0f / m_gamma : 0.0f;
  105. m_bias = C * h * k * m_gamma;
  106. invMass += m_gamma;
  107. m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  108. }
  109. else
  110. {
  111. m_gamma = 0.0f;
  112. m_bias = 0.0f;
  113. }
  114. if (data.step.warmStarting)
  115. {
  116. // Scale the impulse to support a variable time step.
  117. m_impulse *= data.step.dtRatio;
  118. b2Vec2 P = m_impulse * m_u;
  119. vA -= m_invMassA * P;
  120. wA -= m_invIA * b2Cross(m_rA, P);
  121. vB += m_invMassB * P;
  122. wB += m_invIB * b2Cross(m_rB, P);
  123. }
  124. else
  125. {
  126. m_impulse = 0.0f;
  127. }
  128. data.velocities[m_indexA].v = vA;
  129. data.velocities[m_indexA].w = wA;
  130. data.velocities[m_indexB].v = vB;
  131. data.velocities[m_indexB].w = wB;
  132. }
  133. void b2DistanceJoint::SolveVelocityConstraints(const b2SolverData& data)
  134. {
  135. b2Vec2 vA = data.velocities[m_indexA].v;
  136. float32 wA = data.velocities[m_indexA].w;
  137. b2Vec2 vB = data.velocities[m_indexB].v;
  138. float32 wB = data.velocities[m_indexB].w;
  139. // Cdot = dot(u, v + cross(w, r))
  140. b2Vec2 vpA = vA + b2Cross(wA, m_rA);
  141. b2Vec2 vpB = vB + b2Cross(wB, m_rB);
  142. float32 Cdot = b2Dot(m_u, vpB - vpA);
  143. float32 impulse = -m_mass * (Cdot + m_bias + m_gamma * m_impulse);
  144. m_impulse += impulse;
  145. b2Vec2 P = impulse * m_u;
  146. vA -= m_invMassA * P;
  147. wA -= m_invIA * b2Cross(m_rA, P);
  148. vB += m_invMassB * P;
  149. wB += m_invIB * b2Cross(m_rB, P);
  150. data.velocities[m_indexA].v = vA;
  151. data.velocities[m_indexA].w = wA;
  152. data.velocities[m_indexB].v = vB;
  153. data.velocities[m_indexB].w = wB;
  154. }
  155. bool b2DistanceJoint::SolvePositionConstraints(const b2SolverData& data)
  156. {
  157. if (m_frequencyHz > 0.0f)
  158. {
  159. // There is no position correction for soft distance constraints.
  160. return true;
  161. }
  162. b2Vec2 cA = data.positions[m_indexA].c;
  163. float32 aA = data.positions[m_indexA].a;
  164. b2Vec2 cB = data.positions[m_indexB].c;
  165. float32 aB = data.positions[m_indexB].a;
  166. b2Rot qA(aA), qB(aB);
  167. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  168. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  169. b2Vec2 u = cB + rB - cA - rA;
  170. float32 length = u.Normalize();
  171. float32 C = length - m_length;
  172. C = b2Clamp(C, -b2_maxLinearCorrection, b2_maxLinearCorrection);
  173. float32 impulse = -m_mass * C;
  174. b2Vec2 P = impulse * u;
  175. cA -= m_invMassA * P;
  176. aA -= m_invIA * b2Cross(rA, P);
  177. cB += m_invMassB * P;
  178. aB += m_invIB * b2Cross(rB, P);
  179. data.positions[m_indexA].c = cA;
  180. data.positions[m_indexA].a = aA;
  181. data.positions[m_indexB].c = cB;
  182. data.positions[m_indexB].a = aB;
  183. return b2Abs(C) < b2_linearSlop;
  184. }
  185. b2Vec2 b2DistanceJoint::GetAnchorA() const
  186. {
  187. return m_bodyA->GetWorldPoint(m_localAnchorA);
  188. }
  189. b2Vec2 b2DistanceJoint::GetAnchorB() const
  190. {
  191. return m_bodyB->GetWorldPoint(m_localAnchorB);
  192. }
  193. b2Vec2 b2DistanceJoint::GetReactionForce(float32 inv_dt) const
  194. {
  195. b2Vec2 F = (inv_dt * m_impulse) * m_u;
  196. return F;
  197. }
  198. float32 b2DistanceJoint::GetReactionTorque(float32 inv_dt) const
  199. {
  200. B2_NOT_USED(inv_dt);
  201. return 0.0f;
  202. }
  203. void b2DistanceJoint::Dump()
  204. {
  205. int32 indexA = m_bodyA->m_islandIndex;
  206. int32 indexB = m_bodyB->m_islandIndex;
  207. b2Log(" b2DistanceJointDef jd;\n");
  208. b2Log(" jd.bodyA = bodies[%d];\n", indexA);
  209. b2Log(" jd.bodyB = bodies[%d];\n", indexB);
  210. b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  211. b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
  212. b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
  213. b2Log(" jd.length = %.15lef;\n", m_length);
  214. b2Log(" jd.frequencyHz = %.15lef;\n", m_frequencyHz);
  215. b2Log(" jd.dampingRatio = %.15lef;\n", m_dampingRatio);
  216. b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  217. }