b2WeldJoint.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/b2WeldJoint.h>
  19. #include <Box2D/Dynamics/b2Body.h>
  20. #include <Box2D/Dynamics/b2TimeStep.h>
  21. // Point-to-point constraint
  22. // C = p2 - p1
  23. // Cdot = v2 - v1
  24. // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
  25. // J = [-I -r1_skew I r2_skew ]
  26. // Identity used:
  27. // w k % (rx i + ry j) = w * (-ry i + rx j)
  28. // Angle constraint
  29. // C = angle2 - angle1 - referenceAngle
  30. // Cdot = w2 - w1
  31. // J = [0 0 -1 0 0 1]
  32. // K = invI1 + invI2
  33. void b2WeldJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
  34. {
  35. bodyA = bA;
  36. bodyB = bB;
  37. localAnchorA = bodyA->GetLocalPoint(anchor);
  38. localAnchorB = bodyB->GetLocalPoint(anchor);
  39. referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
  40. }
  41. b2WeldJoint::b2WeldJoint(const b2WeldJointDef* def)
  42. : b2Joint(def)
  43. {
  44. m_localAnchorA = def->localAnchorA;
  45. m_localAnchorB = def->localAnchorB;
  46. m_referenceAngle = def->referenceAngle;
  47. m_frequencyHz = def->frequencyHz;
  48. m_dampingRatio = def->dampingRatio;
  49. m_impulse.SetZero();
  50. }
  51. void b2WeldJoint::InitVelocityConstraints(const b2SolverData& data)
  52. {
  53. m_indexA = m_bodyA->m_islandIndex;
  54. m_indexB = m_bodyB->m_islandIndex;
  55. m_localCenterA = m_bodyA->m_sweep.localCenter;
  56. m_localCenterB = m_bodyB->m_sweep.localCenter;
  57. m_invMassA = m_bodyA->m_invMass;
  58. m_invMassB = m_bodyB->m_invMass;
  59. m_invIA = m_bodyA->m_invI;
  60. m_invIB = m_bodyB->m_invI;
  61. float32 aA = data.positions[m_indexA].a;
  62. b2Vec2 vA = data.velocities[m_indexA].v;
  63. float32 wA = data.velocities[m_indexA].w;
  64. float32 aB = data.positions[m_indexB].a;
  65. b2Vec2 vB = data.velocities[m_indexB].v;
  66. float32 wB = data.velocities[m_indexB].w;
  67. b2Rot qA(aA), qB(aB);
  68. m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  69. m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  70. // J = [-I -r1_skew I r2_skew]
  71. // [ 0 -1 0 1]
  72. // r_skew = [-ry; rx]
  73. // Matlab
  74. // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  75. // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  76. // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
  77. float32 mA = m_invMassA, mB = m_invMassB;
  78. float32 iA = m_invIA, iB = m_invIB;
  79. b2Mat33 K;
  80. K.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
  81. K.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
  82. K.ez.x = -m_rA.y * iA - m_rB.y * iB;
  83. K.ex.y = K.ey.x;
  84. K.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
  85. K.ez.y = m_rA.x * iA + m_rB.x * iB;
  86. K.ex.z = K.ez.x;
  87. K.ey.z = K.ez.y;
  88. K.ez.z = iA + iB;
  89. if (m_frequencyHz > 0.0f)
  90. {
  91. K.GetInverse22(&m_mass);
  92. float32 invM = iA + iB;
  93. float32 m = invM > 0.0f ? 1.0f / invM : 0.0f;
  94. float32 C = aB - aA - m_referenceAngle;
  95. // Frequency
  96. float32 omega = 2.0f * b2_pi * m_frequencyHz;
  97. // Damping coefficient
  98. float32 d = 2.0f * m * m_dampingRatio * omega;
  99. // Spring stiffness
  100. float32 k = m * 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. invM += m_gamma;
  107. m_mass.ez.z = invM != 0.0f ? 1.0f / invM : 0.0f;
  108. }
  109. else if (K.ez.z == 0.0f)
  110. {
  111. K.GetInverse22(&m_mass);
  112. m_gamma = 0.0f;
  113. m_bias = 0.0f;
  114. }
  115. else
  116. {
  117. K.GetSymInverse33(&m_mass);
  118. m_gamma = 0.0f;
  119. m_bias = 0.0f;
  120. }
  121. if (data.step.warmStarting)
  122. {
  123. // Scale impulses to support a variable time step.
  124. m_impulse *= data.step.dtRatio;
  125. b2Vec2 P(m_impulse.x, m_impulse.y);
  126. vA -= mA * P;
  127. wA -= iA * (b2Cross(m_rA, P) + m_impulse.z);
  128. vB += mB * P;
  129. wB += iB * (b2Cross(m_rB, P) + m_impulse.z);
  130. }
  131. else
  132. {
  133. m_impulse.SetZero();
  134. }
  135. data.velocities[m_indexA].v = vA;
  136. data.velocities[m_indexA].w = wA;
  137. data.velocities[m_indexB].v = vB;
  138. data.velocities[m_indexB].w = wB;
  139. }
  140. void b2WeldJoint::SolveVelocityConstraints(const b2SolverData& data)
  141. {
  142. b2Vec2 vA = data.velocities[m_indexA].v;
  143. float32 wA = data.velocities[m_indexA].w;
  144. b2Vec2 vB = data.velocities[m_indexB].v;
  145. float32 wB = data.velocities[m_indexB].w;
  146. float32 mA = m_invMassA, mB = m_invMassB;
  147. float32 iA = m_invIA, iB = m_invIB;
  148. if (m_frequencyHz > 0.0f)
  149. {
  150. float32 Cdot2 = wB - wA;
  151. float32 impulse2 = -m_mass.ez.z * (Cdot2 + m_bias + m_gamma * m_impulse.z);
  152. m_impulse.z += impulse2;
  153. wA -= iA * impulse2;
  154. wB += iB * impulse2;
  155. b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
  156. b2Vec2 impulse1 = -b2Mul22(m_mass, Cdot1);
  157. m_impulse.x += impulse1.x;
  158. m_impulse.y += impulse1.y;
  159. b2Vec2 P = impulse1;
  160. vA -= mA * P;
  161. wA -= iA * b2Cross(m_rA, P);
  162. vB += mB * P;
  163. wB += iB * b2Cross(m_rB, P);
  164. }
  165. else
  166. {
  167. b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
  168. float32 Cdot2 = wB - wA;
  169. b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2);
  170. b2Vec3 impulse = -b2Mul(m_mass, Cdot);
  171. m_impulse += impulse;
  172. b2Vec2 P(impulse.x, impulse.y);
  173. vA -= mA * P;
  174. wA -= iA * (b2Cross(m_rA, P) + impulse.z);
  175. vB += mB * P;
  176. wB += iB * (b2Cross(m_rB, P) + impulse.z);
  177. }
  178. data.velocities[m_indexA].v = vA;
  179. data.velocities[m_indexA].w = wA;
  180. data.velocities[m_indexB].v = vB;
  181. data.velocities[m_indexB].w = wB;
  182. }
  183. bool b2WeldJoint::SolvePositionConstraints(const b2SolverData& data)
  184. {
  185. b2Vec2 cA = data.positions[m_indexA].c;
  186. float32 aA = data.positions[m_indexA].a;
  187. b2Vec2 cB = data.positions[m_indexB].c;
  188. float32 aB = data.positions[m_indexB].a;
  189. b2Rot qA(aA), qB(aB);
  190. float32 mA = m_invMassA, mB = m_invMassB;
  191. float32 iA = m_invIA, iB = m_invIB;
  192. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  193. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  194. float32 positionError, angularError;
  195. b2Mat33 K;
  196. K.ex.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
  197. K.ey.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
  198. K.ez.x = -rA.y * iA - rB.y * iB;
  199. K.ex.y = K.ey.x;
  200. K.ey.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
  201. K.ez.y = rA.x * iA + rB.x * iB;
  202. K.ex.z = K.ez.x;
  203. K.ey.z = K.ez.y;
  204. K.ez.z = iA + iB;
  205. if (m_frequencyHz > 0.0f)
  206. {
  207. b2Vec2 C1 = cB + rB - cA - rA;
  208. positionError = C1.Length();
  209. angularError = 0.0f;
  210. b2Vec2 P = -K.Solve22(C1);
  211. cA -= mA * P;
  212. aA -= iA * b2Cross(rA, P);
  213. cB += mB * P;
  214. aB += iB * b2Cross(rB, P);
  215. }
  216. else
  217. {
  218. b2Vec2 C1 = cB + rB - cA - rA;
  219. float32 C2 = aB - aA - m_referenceAngle;
  220. positionError = C1.Length();
  221. angularError = b2Abs(C2);
  222. b2Vec3 C(C1.x, C1.y, C2);
  223. b2Vec3 impulse;
  224. if (K.ez.z > 0.0f)
  225. {
  226. impulse = -K.Solve33(C);
  227. }
  228. else
  229. {
  230. b2Vec2 impulse2 = -K.Solve22(C1);
  231. impulse.Set(impulse2.x, impulse2.y, 0.0f);
  232. }
  233. b2Vec2 P(impulse.x, impulse.y);
  234. cA -= mA * P;
  235. aA -= iA * (b2Cross(rA, P) + impulse.z);
  236. cB += mB * P;
  237. aB += iB * (b2Cross(rB, P) + impulse.z);
  238. }
  239. data.positions[m_indexA].c = cA;
  240. data.positions[m_indexA].a = aA;
  241. data.positions[m_indexB].c = cB;
  242. data.positions[m_indexB].a = aB;
  243. return positionError <= b2_linearSlop && angularError <= b2_angularSlop;
  244. }
  245. b2Vec2 b2WeldJoint::GetAnchorA() const
  246. {
  247. return m_bodyA->GetWorldPoint(m_localAnchorA);
  248. }
  249. b2Vec2 b2WeldJoint::GetAnchorB() const
  250. {
  251. return m_bodyB->GetWorldPoint(m_localAnchorB);
  252. }
  253. b2Vec2 b2WeldJoint::GetReactionForce(float32 inv_dt) const
  254. {
  255. b2Vec2 P(m_impulse.x, m_impulse.y);
  256. return inv_dt * P;
  257. }
  258. float32 b2WeldJoint::GetReactionTorque(float32 inv_dt) const
  259. {
  260. return inv_dt * m_impulse.z;
  261. }
  262. void b2WeldJoint::Dump()
  263. {
  264. int32 indexA = m_bodyA->m_islandIndex;
  265. int32 indexB = m_bodyB->m_islandIndex;
  266. b2Log(" b2WeldJointDef jd;\n");
  267. b2Log(" jd.bodyA = bodies[%d];\n", indexA);
  268. b2Log(" jd.bodyB = bodies[%d];\n", indexB);
  269. b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  270. b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
  271. b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
  272. b2Log(" jd.referenceAngle = %.15lef;\n", m_referenceAngle);
  273. b2Log(" jd.frequencyHz = %.15lef;\n", m_frequencyHz);
  274. b2Log(" jd.dampingRatio = %.15lef;\n", m_dampingRatio);
  275. b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  276. }