b2LineJoint.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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 "b2LineJoint.h"
  19. #include "../b2Body.h"
  20. #include "../b2World.h"
  21. // Linear constraint (point-to-line)
  22. // d = p2 - p1 = x2 + r2 - x1 - r1
  23. // C = dot(perp, d)
  24. // Cdot = dot(d, cross(w1, perp)) + dot(perp, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  25. // = -dot(perp, v1) - dot(cross(d + r1, perp), w1) + dot(perp, v2) + dot(cross(r2, perp), v2)
  26. // J = [-perp, -cross(d + r1, perp), perp, cross(r2,perp)]
  27. //
  28. // K = J * invM * JT
  29. //
  30. // J = [-a -s1 a s2]
  31. // a = perp
  32. // s1 = cross(d + r1, a) = cross(p2 - x1, a)
  33. // s2 = cross(r2, a) = cross(p2 - x2, a)
  34. // Motor/Limit linear constraint
  35. // C = dot(ax1, d)
  36. // Cdot = = -dot(ax1, v1) - dot(cross(d + r1, ax1), w1) + dot(ax1, v2) + dot(cross(r2, ax1), v2)
  37. // J = [-ax1 -cross(d+r1,ax1) ax1 cross(r2,ax1)]
  38. // Block Solver
  39. // We develop a block solver that includes the joint limit. This makes the limit stiff (inelastic) even
  40. // when the mass has poor distribution (leading to large torques about the joint anchor points).
  41. //
  42. // The Jacobian has 3 rows:
  43. // J = [-uT -s1 uT s2] // linear
  44. // [-vT -a1 vT a2] // limit
  45. //
  46. // u = perp
  47. // v = axis
  48. // s1 = cross(d + r1, u), s2 = cross(r2, u)
  49. // a1 = cross(d + r1, v), a2 = cross(r2, v)
  50. // M * (v2 - v1) = JT * df
  51. // J * v2 = bias
  52. //
  53. // v2 = v1 + invM * JT * df
  54. // J * (v1 + invM * JT * df) = bias
  55. // K * df = bias - J * v1 = -Cdot
  56. // K = J * invM * JT
  57. // Cdot = J * v1 - bias
  58. //
  59. // Now solve for f2.
  60. // df = f2 - f1
  61. // K * (f2 - f1) = -Cdot
  62. // f2 = invK * (-Cdot) + f1
  63. //
  64. // Clamp accumulated limit impulse.
  65. // lower: f2(2) = max(f2(2), 0)
  66. // upper: f2(2) = min(f2(2), 0)
  67. //
  68. // Solve for correct f2(1)
  69. // K(1,1) * f2(1) = -Cdot(1) - K(1,2) * f2(2) + K(1,1:2) * f1
  70. // = -Cdot(1) - K(1,2) * f2(2) + K(1,1) * f1(1) + K(1,2) * f1(2)
  71. // K(1,1) * f2(1) = -Cdot(1) - K(1,2) * (f2(2) - f1(2)) + K(1,1) * f1(1)
  72. // f2(1) = invK(1,1) * (-Cdot(1) - K(1,2) * (f2(2) - f1(2))) + f1(1)
  73. //
  74. // Now compute impulse to be applied:
  75. // df = f2 - f1
  76. void b2LineJointDef::Initialize(b2Body* b1, b2Body* b2, const b2Vec2& anchor, const b2Vec2& axis)
  77. {
  78. body1 = b1;
  79. body2 = b2;
  80. localAnchor1 = body1->GetLocalPoint(anchor);
  81. localAnchor2 = body2->GetLocalPoint(anchor);
  82. localAxis1 = body1->GetLocalVector(axis);
  83. }
  84. b2LineJoint::b2LineJoint(const b2LineJointDef* def)
  85. : b2Joint(def)
  86. {
  87. m_localAnchor1 = def->localAnchor1;
  88. m_localAnchor2 = def->localAnchor2;
  89. m_localXAxis1 = def->localAxis1;
  90. m_localYAxis1 = b2Cross(1.0f, m_localXAxis1);
  91. m_impulse.SetZero();
  92. m_motorMass = 0.0;
  93. m_motorImpulse = 0.0f;
  94. m_lowerTranslation = def->lowerTranslation;
  95. m_upperTranslation = def->upperTranslation;
  96. m_maxMotorForce = B2FORCE_INV_SCALE(def->maxMotorForce);
  97. m_motorSpeed = def->motorSpeed;
  98. m_enableLimit = def->enableLimit;
  99. m_enableMotor = def->enableMotor;
  100. m_limitState = e_inactiveLimit;
  101. m_axis.SetZero();
  102. m_perp.SetZero();
  103. }
  104. void b2LineJoint::InitVelocityConstraints(const b2TimeStep& step)
  105. {
  106. b2Body* b1 = m_body1;
  107. b2Body* b2 = m_body2;
  108. m_localCenter1 = b1->GetLocalCenter();
  109. m_localCenter2 = b2->GetLocalCenter();
  110. b2XForm xf1 = b1->GetXForm();
  111. b2XForm xf2 = b2->GetXForm();
  112. // Compute the effective masses.
  113. b2Vec2 r1 = b2Mul(xf1.R, m_localAnchor1 - m_localCenter1);
  114. b2Vec2 r2 = b2Mul(xf2.R, m_localAnchor2 - m_localCenter2);
  115. b2Vec2 d = b2->m_sweep.c + r2 - b1->m_sweep.c - r1;
  116. m_invMass1 = b1->m_invMass;
  117. m_invI1 = b1->m_invI;
  118. m_invMass2 = b2->m_invMass;
  119. m_invI2 = b2->m_invI;
  120. // Compute motor Jacobian and effective mass.
  121. {
  122. m_axis = b2Mul(xf1.R, m_localXAxis1);
  123. m_a1 = b2Cross(d + r1, m_axis);
  124. m_a2 = b2Cross(r2, m_axis);
  125. m_motorMass = m_invMass1 + m_invMass2 + m_invI1 * m_a1 * m_a1 + m_invI2 * m_a2 * m_a2;
  126. b2Assert(m_motorMass > B2_FLT_EPSILON);
  127. m_motorMass = 1.0f / m_motorMass;
  128. }
  129. // Prismatic constraint.
  130. {
  131. m_perp = b2Mul(xf1.R, m_localYAxis1);
  132. m_s1 = b2Cross(d + r1, m_perp);
  133. m_s2 = b2Cross(r2, m_perp);
  134. float32 m1 = m_invMass1, m2 = m_invMass2;
  135. float32 i1 = m_invI1, i2 = m_invI2;
  136. float32 k11 = m1 + m2 + i1 * m_s1 * m_s1 + i2 * m_s2 * m_s2;
  137. float32 k12 = i1 * m_s1 * m_a1 + i2 * m_s2 * m_a2;
  138. float32 k22 = m1 + m2 + i1 * m_a1 * m_a1 + i2 * m_a2 * m_a2;
  139. m_K.col1.Set(k11, k12);
  140. m_K.col2.Set(k12, k22);
  141. }
  142. // Compute motor and limit terms.
  143. if (m_enableLimit)
  144. {
  145. float32 jointTranslation = b2Dot(m_axis, d);
  146. if (b2Abs(m_upperTranslation - m_lowerTranslation) < 2.0f * b2_linearSlop)
  147. {
  148. m_limitState = e_equalLimits;
  149. }
  150. else if (jointTranslation <= m_lowerTranslation)
  151. {
  152. if (m_limitState != e_atLowerLimit)
  153. {
  154. m_limitState = e_atLowerLimit;
  155. m_impulse.y = 0.0f;
  156. }
  157. }
  158. else if (jointTranslation >= m_upperTranslation)
  159. {
  160. if (m_limitState != e_atUpperLimit)
  161. {
  162. m_limitState = e_atUpperLimit;
  163. m_impulse.y = 0.0f;
  164. }
  165. }
  166. else
  167. {
  168. m_limitState = e_inactiveLimit;
  169. m_impulse.y = 0.0f;
  170. }
  171. }
  172. else
  173. {
  174. m_limitState = e_inactiveLimit;
  175. }
  176. if (m_enableMotor == false)
  177. {
  178. m_motorImpulse = 0.0f;
  179. }
  180. if (step.warmStarting)
  181. {
  182. // Account for variable time step.
  183. m_impulse *= step.dtRatio;
  184. m_motorImpulse *= step.dtRatio;
  185. b2Vec2 P = m_impulse.x * m_perp + (m_motorImpulse + m_impulse.y) * m_axis;
  186. float32 L1 = m_impulse.x * m_s1 + (m_motorImpulse + m_impulse.y) * m_a1;
  187. float32 L2 = m_impulse.x * m_s2 + (m_motorImpulse + m_impulse.y) * m_a2;
  188. b1->m_linearVelocity -= m_invMass1 * P;
  189. b1->m_angularVelocity -= m_invI1 * L1;
  190. b2->m_linearVelocity += m_invMass2 * P;
  191. b2->m_angularVelocity += m_invI2 * L2;
  192. }
  193. else
  194. {
  195. m_impulse.SetZero();
  196. m_motorImpulse = 0.0f;
  197. }
  198. }
  199. void b2LineJoint::SolveVelocityConstraints(const b2TimeStep& step)
  200. {
  201. b2Body* b1 = m_body1;
  202. b2Body* b2 = m_body2;
  203. b2Vec2 v1 = b1->m_linearVelocity;
  204. float32 w1 = b1->m_angularVelocity;
  205. b2Vec2 v2 = b2->m_linearVelocity;
  206. float32 w2 = b2->m_angularVelocity;
  207. // Solve linear motor constraint.
  208. if (m_enableMotor && m_limitState != e_equalLimits)
  209. {
  210. float32 Cdot = b2Dot(m_axis, v2 - v1) + m_a2 * w2 - m_a1 * w1;
  211. float32 impulse = m_motorMass * (m_motorSpeed - Cdot);
  212. float32 oldImpulse = m_motorImpulse;
  213. float32 maxImpulse = step.dt * m_maxMotorForce;
  214. m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  215. impulse = m_motorImpulse - oldImpulse;
  216. b2Vec2 P = impulse * m_axis;
  217. float32 L1 = impulse * m_a1;
  218. float32 L2 = impulse * m_a2;
  219. v1 -= m_invMass1 * P;
  220. w1 -= m_invI1 * L1;
  221. v2 += m_invMass2 * P;
  222. w2 += m_invI2 * L2;
  223. }
  224. float32 Cdot1 = b2Dot(m_perp, v2 - v1) + m_s2 * w2 - m_s1 * w1;
  225. if (m_enableLimit && m_limitState != e_inactiveLimit)
  226. {
  227. // Solve prismatic and limit constraint in block form.
  228. float32 Cdot2 = b2Dot(m_axis, v2 - v1) + m_a2 * w2 - m_a1 * w1;
  229. b2Vec2 Cdot(Cdot1, Cdot2);
  230. b2Vec2 f1 = m_impulse;
  231. b2Vec2 df = m_K.Solve(-Cdot);
  232. m_impulse += df;
  233. if (m_limitState == e_atLowerLimit)
  234. {
  235. m_impulse.y = b2Max(m_impulse.y, 0.0f);
  236. }
  237. else if (m_limitState == e_atUpperLimit)
  238. {
  239. m_impulse.y = b2Min(m_impulse.y, 0.0f);
  240. }
  241. // f2(1) = invK(1,1) * (-Cdot(1) - K(1,2) * (f2(2) - f1(2))) + f1(1)
  242. float32 b = -Cdot1 - (m_impulse.y - f1.y) * m_K.col2.x;
  243. float32 f2r = b / m_K.col1.x + f1.x;
  244. m_impulse.x = f2r;
  245. df = m_impulse - f1;
  246. b2Vec2 P = df.x * m_perp + df.y * m_axis;
  247. float32 L1 = df.x * m_s1 + df.y * m_a1;
  248. float32 L2 = df.x * m_s2 + df.y * m_a2;
  249. v1 -= m_invMass1 * P;
  250. w1 -= m_invI1 * L1;
  251. v2 += m_invMass2 * P;
  252. w2 += m_invI2 * L2;
  253. }
  254. else
  255. {
  256. // Limit is inactive, just solve the prismatic constraint in block form.
  257. float32 df = (-Cdot1) / m_K.col1.x;
  258. m_impulse.x += df;
  259. b2Vec2 P = df * m_perp;
  260. float32 L1 = df * m_s1;
  261. float32 L2 = df * m_s2;
  262. v1 -= m_invMass1 * P;
  263. w1 -= m_invI1 * L1;
  264. v2 += m_invMass2 * P;
  265. w2 += m_invI2 * L2;
  266. }
  267. b1->m_linearVelocity = v1;
  268. b1->m_angularVelocity = w1;
  269. b2->m_linearVelocity = v2;
  270. b2->m_angularVelocity = w2;
  271. }
  272. bool b2LineJoint::SolvePositionConstraints(float32 baumgarte)
  273. {
  274. B2_NOT_USED(baumgarte);
  275. b2Body* b1 = m_body1;
  276. b2Body* b2 = m_body2;
  277. b2Vec2 c1 = b1->m_sweep.c;
  278. float32 a1 = b1->m_sweep.a;
  279. b2Vec2 c2 = b2->m_sweep.c;
  280. float32 a2 = b2->m_sweep.a;
  281. // Solve linear limit constraint.
  282. float32 linearError = 0.0f, angularError = 0.0f;
  283. bool active = false;
  284. float32 C2 = 0.0f;
  285. b2Mat22 R1(a1), R2(a2);
  286. b2Vec2 r1 = b2Mul(R1, m_localAnchor1 - m_localCenter1);
  287. b2Vec2 r2 = b2Mul(R2, m_localAnchor2 - m_localCenter2);
  288. b2Vec2 d = c2 + r2 - c1 - r1;
  289. if (m_enableLimit)
  290. {
  291. m_axis = b2Mul(R1, m_localXAxis1);
  292. m_a1 = b2Cross(d + r1, m_axis);
  293. m_a2 = b2Cross(r2, m_axis);
  294. float32 translation = b2Dot(m_axis, d);
  295. if (b2Abs(m_upperTranslation - m_lowerTranslation) < 2.0f * b2_linearSlop)
  296. {
  297. // Prevent large angular corrections
  298. C2 = b2Clamp(translation, -b2_maxLinearCorrection, b2_maxLinearCorrection);
  299. linearError = b2Abs(translation);
  300. active = true;
  301. }
  302. else if (translation <= m_lowerTranslation)
  303. {
  304. // Prevent large linear corrections and allow some slop.
  305. C2 = b2Clamp(translation - m_lowerTranslation + b2_linearSlop, -b2_maxLinearCorrection, 0.0f);
  306. linearError = m_lowerTranslation - translation;
  307. active = true;
  308. }
  309. else if (translation >= m_upperTranslation)
  310. {
  311. // Prevent large linear corrections and allow some slop.
  312. C2 = b2Clamp(translation - m_upperTranslation - b2_linearSlop, 0.0f, b2_maxLinearCorrection);
  313. linearError = translation - m_upperTranslation;
  314. active = true;
  315. }
  316. }
  317. m_perp = b2Mul(R1, m_localYAxis1);
  318. m_s1 = b2Cross(d + r1, m_perp);
  319. m_s2 = b2Cross(r2, m_perp);
  320. b2Vec2 impulse;
  321. float32 C1;
  322. C1 = b2Dot(m_perp, d);
  323. linearError = b2Max(linearError, b2Abs(C1));
  324. angularError = 0.0f;
  325. if (active)
  326. {
  327. float32 m1 = m_invMass1, m2 = m_invMass2;
  328. float32 i1 = m_invI1, i2 = m_invI2;
  329. float32 k11 = m1 + m2 + i1 * m_s1 * m_s1 + i2 * m_s2 * m_s2;
  330. float32 k12 = i1 * m_s1 * m_a1 + i2 * m_s2 * m_a2;
  331. float32 k22 = m1 + m2 + i1 * m_a1 * m_a1 + i2 * m_a2 * m_a2;
  332. m_K.col1.Set(k11, k12);
  333. m_K.col2.Set(k12, k22);
  334. b2Vec2 C;
  335. C.x = C1;
  336. C.y = C2;
  337. impulse = m_K.Solve(-C);
  338. }
  339. else
  340. {
  341. float32 m1 = m_invMass1, m2 = m_invMass2;
  342. float32 i1 = m_invI1, i2 = m_invI2;
  343. float32 k11 = m1 + m2 + i1 * m_s1 * m_s1 + i2 * m_s2 * m_s2;
  344. float32 impulse1 = (-C1) / k11;
  345. impulse.x = impulse1;
  346. impulse.y = 0.0f;
  347. }
  348. b2Vec2 P = impulse.x * m_perp + impulse.y * m_axis;
  349. float32 L1 = impulse.x * m_s1 + impulse.y * m_a1;
  350. float32 L2 = impulse.x * m_s2 + impulse.y * m_a2;
  351. c1 -= m_invMass1 * P;
  352. a1 -= m_invI1 * L1;
  353. c2 += m_invMass2 * P;
  354. a2 += m_invI2 * L2;
  355. // TODO_ERIN remove need for this.
  356. b1->m_sweep.c = c1;
  357. b1->m_sweep.a = a1;
  358. b2->m_sweep.c = c2;
  359. b2->m_sweep.a = a2;
  360. b1->SynchronizeTransform();
  361. b2->SynchronizeTransform();
  362. return linearError <= b2_linearSlop && angularError <= b2_angularSlop;
  363. }
  364. b2Vec2 b2LineJoint::GetAnchor1() const
  365. {
  366. return m_body1->GetWorldPoint(m_localAnchor1);
  367. }
  368. b2Vec2 b2LineJoint::GetAnchor2() const
  369. {
  370. return m_body2->GetWorldPoint(m_localAnchor2);
  371. }
  372. b2Vec2 b2LineJoint::GetReactionForce(float32 inv_dt) const
  373. {
  374. return inv_dt * (m_impulse.x * m_perp + (m_motorImpulse + m_impulse.y) * m_axis);
  375. }
  376. float32 b2LineJoint::GetReactionTorque(float32 inv_dt) const
  377. {
  378. B2_NOT_USED(inv_dt);
  379. return 0.0f;
  380. }
  381. float32 b2LineJoint::GetJointTranslation() const
  382. {
  383. b2Body* b1 = m_body1;
  384. b2Body* b2 = m_body2;
  385. b2Vec2 p1 = b1->GetWorldPoint(m_localAnchor1);
  386. b2Vec2 p2 = b2->GetWorldPoint(m_localAnchor2);
  387. b2Vec2 d = p2 - p1;
  388. b2Vec2 axis = b1->GetWorldVector(m_localXAxis1);
  389. float32 translation = b2Dot(d, axis);
  390. return translation;
  391. }
  392. float32 b2LineJoint::GetJointSpeed() const
  393. {
  394. b2Body* b1 = m_body1;
  395. b2Body* b2 = m_body2;
  396. b2Vec2 r1 = b2Mul(b1->GetXForm().R, m_localAnchor1 - b1->GetLocalCenter());
  397. b2Vec2 r2 = b2Mul(b2->GetXForm().R, m_localAnchor2 - b2->GetLocalCenter());
  398. b2Vec2 p1 = b1->m_sweep.c + r1;
  399. b2Vec2 p2 = b2->m_sweep.c + r2;
  400. b2Vec2 d = p2 - p1;
  401. b2Vec2 axis = b1->GetWorldVector(m_localXAxis1);
  402. b2Vec2 v1 = b1->m_linearVelocity;
  403. b2Vec2 v2 = b2->m_linearVelocity;
  404. float32 w1 = b1->m_angularVelocity;
  405. float32 w2 = b2->m_angularVelocity;
  406. float32 speed = b2Dot(d, b2Cross(w1, axis)) + b2Dot(axis, v2 + b2Cross(w2, r2) - v1 - b2Cross(w1, r1));
  407. return speed;
  408. }
  409. bool b2LineJoint::IsLimitEnabled() const
  410. {
  411. return m_enableLimit;
  412. }
  413. void b2LineJoint::EnableLimit(bool flag)
  414. {
  415. m_body1->WakeUp();
  416. m_body2->WakeUp();
  417. m_enableLimit = flag;
  418. }
  419. float32 b2LineJoint::GetLowerLimit() const
  420. {
  421. return m_lowerTranslation;
  422. }
  423. float32 b2LineJoint::GetUpperLimit() const
  424. {
  425. return m_upperTranslation;
  426. }
  427. void b2LineJoint::SetLimits(float32 lower, float32 upper)
  428. {
  429. b2Assert(lower <= upper);
  430. m_body1->WakeUp();
  431. m_body2->WakeUp();
  432. m_lowerTranslation = lower;
  433. m_upperTranslation = upper;
  434. }
  435. bool b2LineJoint::IsMotorEnabled() const
  436. {
  437. return m_enableMotor;
  438. }
  439. void b2LineJoint::EnableMotor(bool flag)
  440. {
  441. m_body1->WakeUp();
  442. m_body2->WakeUp();
  443. m_enableMotor = flag;
  444. }
  445. void b2LineJoint::SetMotorSpeed(float32 speed)
  446. {
  447. m_body1->WakeUp();
  448. m_body2->WakeUp();
  449. m_motorSpeed = speed;
  450. }
  451. void b2LineJoint::SetMaxMotorForce(float32 force)
  452. {
  453. m_body1->WakeUp();
  454. m_body2->WakeUp();
  455. m_maxMotorForce = B2FORCE_SCALE(float32(1.0))*force;
  456. }
  457. float32 b2LineJoint::GetMotorForce() const
  458. {
  459. return m_motorImpulse;
  460. }