b2RevoluteJoint.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/b2RevoluteJoint.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. // Motor constraint
  29. // Cdot = w2 - w1
  30. // J = [0 0 -1 0 0 1]
  31. // K = invI1 + invI2
  32. void b2RevoluteJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
  33. {
  34. bodyA = bA;
  35. bodyB = bB;
  36. localAnchorA = bodyA->GetLocalPoint(anchor);
  37. localAnchorB = bodyB->GetLocalPoint(anchor);
  38. referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
  39. }
  40. b2RevoluteJoint::b2RevoluteJoint(const b2RevoluteJointDef* def)
  41. : b2Joint(def)
  42. {
  43. m_localAnchorA = def->localAnchorA;
  44. m_localAnchorB = def->localAnchorB;
  45. m_referenceAngle = def->referenceAngle;
  46. m_impulse.SetZero();
  47. m_motorImpulse = 0.0f;
  48. m_lowerAngle = def->lowerAngle;
  49. m_upperAngle = def->upperAngle;
  50. m_maxMotorTorque = def->maxMotorTorque;
  51. m_motorSpeed = def->motorSpeed;
  52. m_enableLimit = def->enableLimit;
  53. m_enableMotor = def->enableMotor;
  54. m_limitState = e_inactiveLimit;
  55. }
  56. void b2RevoluteJoint::InitVelocityConstraints(const b2SolverData& data)
  57. {
  58. m_indexA = m_bodyA->m_islandIndex;
  59. m_indexB = m_bodyB->m_islandIndex;
  60. m_localCenterA = m_bodyA->m_sweep.localCenter;
  61. m_localCenterB = m_bodyB->m_sweep.localCenter;
  62. m_invMassA = m_bodyA->m_invMass;
  63. m_invMassB = m_bodyB->m_invMass;
  64. m_invIA = m_bodyA->m_invI;
  65. m_invIB = m_bodyB->m_invI;
  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. float32 aB = data.positions[m_indexB].a;
  70. b2Vec2 vB = data.velocities[m_indexB].v;
  71. float32 wB = data.velocities[m_indexB].w;
  72. b2Rot qA(aA), qB(aB);
  73. m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  74. m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  75. // J = [-I -r1_skew I r2_skew]
  76. // [ 0 -1 0 1]
  77. // r_skew = [-ry; rx]
  78. // Matlab
  79. // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  80. // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  81. // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
  82. float32 mA = m_invMassA, mB = m_invMassB;
  83. float32 iA = m_invIA, iB = m_invIB;
  84. bool fixedRotation = (iA + iB == 0.0f);
  85. m_mass.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
  86. m_mass.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
  87. m_mass.ez.x = -m_rA.y * iA - m_rB.y * iB;
  88. m_mass.ex.y = m_mass.ey.x;
  89. m_mass.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
  90. m_mass.ez.y = m_rA.x * iA + m_rB.x * iB;
  91. m_mass.ex.z = m_mass.ez.x;
  92. m_mass.ey.z = m_mass.ez.y;
  93. m_mass.ez.z = iA + iB;
  94. m_motorMass = iA + iB;
  95. if (m_motorMass > 0.0f)
  96. {
  97. m_motorMass = 1.0f / m_motorMass;
  98. }
  99. if (m_enableMotor == false || fixedRotation)
  100. {
  101. m_motorImpulse = 0.0f;
  102. }
  103. if (m_enableLimit && fixedRotation == false)
  104. {
  105. float32 jointAngle = aB - aA - m_referenceAngle;
  106. if (b2Abs(m_upperAngle - m_lowerAngle) < 2.0f * b2_angularSlop)
  107. {
  108. m_limitState = e_equalLimits;
  109. }
  110. else if (jointAngle <= m_lowerAngle)
  111. {
  112. if (m_limitState != e_atLowerLimit)
  113. {
  114. m_impulse.z = 0.0f;
  115. }
  116. m_limitState = e_atLowerLimit;
  117. }
  118. else if (jointAngle >= m_upperAngle)
  119. {
  120. if (m_limitState != e_atUpperLimit)
  121. {
  122. m_impulse.z = 0.0f;
  123. }
  124. m_limitState = e_atUpperLimit;
  125. }
  126. else
  127. {
  128. m_limitState = e_inactiveLimit;
  129. m_impulse.z = 0.0f;
  130. }
  131. }
  132. else
  133. {
  134. m_limitState = e_inactiveLimit;
  135. }
  136. if (data.step.warmStarting)
  137. {
  138. // Scale impulses to support a variable time step.
  139. m_impulse *= data.step.dtRatio;
  140. m_motorImpulse *= data.step.dtRatio;
  141. b2Vec2 P(m_impulse.x, m_impulse.y);
  142. vA -= mA * P;
  143. wA -= iA * (b2Cross(m_rA, P) + m_motorImpulse + m_impulse.z);
  144. vB += mB * P;
  145. wB += iB * (b2Cross(m_rB, P) + m_motorImpulse + m_impulse.z);
  146. }
  147. else
  148. {
  149. m_impulse.SetZero();
  150. m_motorImpulse = 0.0f;
  151. }
  152. data.velocities[m_indexA].v = vA;
  153. data.velocities[m_indexA].w = wA;
  154. data.velocities[m_indexB].v = vB;
  155. data.velocities[m_indexB].w = wB;
  156. }
  157. void b2RevoluteJoint::SolveVelocityConstraints(const b2SolverData& data)
  158. {
  159. b2Vec2 vA = data.velocities[m_indexA].v;
  160. float32 wA = data.velocities[m_indexA].w;
  161. b2Vec2 vB = data.velocities[m_indexB].v;
  162. float32 wB = data.velocities[m_indexB].w;
  163. float32 mA = m_invMassA, mB = m_invMassB;
  164. float32 iA = m_invIA, iB = m_invIB;
  165. bool fixedRotation = (iA + iB == 0.0f);
  166. // Solve motor constraint.
  167. if (m_enableMotor && m_limitState != e_equalLimits && fixedRotation == false)
  168. {
  169. float32 Cdot = wB - wA - m_motorSpeed;
  170. float32 impulse = -m_motorMass * Cdot;
  171. float32 oldImpulse = m_motorImpulse;
  172. float32 maxImpulse = data.step.dt * m_maxMotorTorque;
  173. m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  174. impulse = m_motorImpulse - oldImpulse;
  175. wA -= iA * impulse;
  176. wB += iB * impulse;
  177. }
  178. // Solve limit constraint.
  179. if (m_enableLimit && m_limitState != e_inactiveLimit && fixedRotation == false)
  180. {
  181. b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
  182. float32 Cdot2 = wB - wA;
  183. b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2);
  184. b2Vec3 impulse = -m_mass.Solve33(Cdot);
  185. if (m_limitState == e_equalLimits)
  186. {
  187. m_impulse += impulse;
  188. }
  189. else if (m_limitState == e_atLowerLimit)
  190. {
  191. float32 newImpulse = m_impulse.z + impulse.z;
  192. if (newImpulse < 0.0f)
  193. {
  194. b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y);
  195. b2Vec2 reduced = m_mass.Solve22(rhs);
  196. impulse.x = reduced.x;
  197. impulse.y = reduced.y;
  198. impulse.z = -m_impulse.z;
  199. m_impulse.x += reduced.x;
  200. m_impulse.y += reduced.y;
  201. m_impulse.z = 0.0f;
  202. }
  203. else
  204. {
  205. m_impulse += impulse;
  206. }
  207. }
  208. else if (m_limitState == e_atUpperLimit)
  209. {
  210. float32 newImpulse = m_impulse.z + impulse.z;
  211. if (newImpulse > 0.0f)
  212. {
  213. b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y);
  214. b2Vec2 reduced = m_mass.Solve22(rhs);
  215. impulse.x = reduced.x;
  216. impulse.y = reduced.y;
  217. impulse.z = -m_impulse.z;
  218. m_impulse.x += reduced.x;
  219. m_impulse.y += reduced.y;
  220. m_impulse.z = 0.0f;
  221. }
  222. else
  223. {
  224. m_impulse += impulse;
  225. }
  226. }
  227. b2Vec2 P(impulse.x, impulse.y);
  228. vA -= mA * P;
  229. wA -= iA * (b2Cross(m_rA, P) + impulse.z);
  230. vB += mB * P;
  231. wB += iB * (b2Cross(m_rB, P) + impulse.z);
  232. }
  233. else
  234. {
  235. // Solve point-to-point constraint
  236. b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
  237. b2Vec2 impulse = m_mass.Solve22(-Cdot);
  238. m_impulse.x += impulse.x;
  239. m_impulse.y += impulse.y;
  240. vA -= mA * impulse;
  241. wA -= iA * b2Cross(m_rA, impulse);
  242. vB += mB * impulse;
  243. wB += iB * b2Cross(m_rB, impulse);
  244. }
  245. data.velocities[m_indexA].v = vA;
  246. data.velocities[m_indexA].w = wA;
  247. data.velocities[m_indexB].v = vB;
  248. data.velocities[m_indexB].w = wB;
  249. }
  250. bool b2RevoluteJoint::SolvePositionConstraints(const b2SolverData& data)
  251. {
  252. b2Vec2 cA = data.positions[m_indexA].c;
  253. float32 aA = data.positions[m_indexA].a;
  254. b2Vec2 cB = data.positions[m_indexB].c;
  255. float32 aB = data.positions[m_indexB].a;
  256. b2Rot qA(aA), qB(aB);
  257. float32 angularError = 0.0f;
  258. float32 positionError = 0.0f;
  259. bool fixedRotation = (m_invIA + m_invIB == 0.0f);
  260. // Solve angular limit constraint.
  261. if (m_enableLimit && m_limitState != e_inactiveLimit && fixedRotation == false)
  262. {
  263. float32 angle = aB - aA - m_referenceAngle;
  264. float32 limitImpulse = 0.0f;
  265. if (m_limitState == e_equalLimits)
  266. {
  267. // Prevent large angular corrections
  268. float32 C = b2Clamp(angle - m_lowerAngle, -b2_maxAngularCorrection, b2_maxAngularCorrection);
  269. limitImpulse = -m_motorMass * C;
  270. angularError = b2Abs(C);
  271. }
  272. else if (m_limitState == e_atLowerLimit)
  273. {
  274. float32 C = angle - m_lowerAngle;
  275. angularError = -C;
  276. // Prevent large angular corrections and allow some slop.
  277. C = b2Clamp(C + b2_angularSlop, -b2_maxAngularCorrection, 0.0f);
  278. limitImpulse = -m_motorMass * C;
  279. }
  280. else if (m_limitState == e_atUpperLimit)
  281. {
  282. float32 C = angle - m_upperAngle;
  283. angularError = C;
  284. // Prevent large angular corrections and allow some slop.
  285. C = b2Clamp(C - b2_angularSlop, 0.0f, b2_maxAngularCorrection);
  286. limitImpulse = -m_motorMass * C;
  287. }
  288. aA -= m_invIA * limitImpulse;
  289. aB += m_invIB * limitImpulse;
  290. }
  291. // Solve point-to-point constraint.
  292. {
  293. qA.Set(aA);
  294. qB.Set(aB);
  295. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  296. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  297. b2Vec2 C = cB + rB - cA - rA;
  298. positionError = C.Length();
  299. float32 mA = m_invMassA, mB = m_invMassB;
  300. float32 iA = m_invIA, iB = m_invIB;
  301. b2Mat22 K;
  302. K.ex.x = mA + mB + iA * rA.y * rA.y + iB * rB.y * rB.y;
  303. K.ex.y = -iA * rA.x * rA.y - iB * rB.x * rB.y;
  304. K.ey.x = K.ex.y;
  305. K.ey.y = mA + mB + iA * rA.x * rA.x + iB * rB.x * rB.x;
  306. b2Vec2 impulse = -K.Solve(C);
  307. cA -= mA * impulse;
  308. aA -= iA * b2Cross(rA, impulse);
  309. cB += mB * impulse;
  310. aB += iB * b2Cross(rB, impulse);
  311. }
  312. data.positions[m_indexA].c = cA;
  313. data.positions[m_indexA].a = aA;
  314. data.positions[m_indexB].c = cB;
  315. data.positions[m_indexB].a = aB;
  316. return positionError <= b2_linearSlop && angularError <= b2_angularSlop;
  317. }
  318. b2Vec2 b2RevoluteJoint::GetAnchorA() const
  319. {
  320. return m_bodyA->GetWorldPoint(m_localAnchorA);
  321. }
  322. b2Vec2 b2RevoluteJoint::GetAnchorB() const
  323. {
  324. return m_bodyB->GetWorldPoint(m_localAnchorB);
  325. }
  326. b2Vec2 b2RevoluteJoint::GetReactionForce(float32 inv_dt) const
  327. {
  328. b2Vec2 P(m_impulse.x, m_impulse.y);
  329. return inv_dt * P;
  330. }
  331. float32 b2RevoluteJoint::GetReactionTorque(float32 inv_dt) const
  332. {
  333. return inv_dt * m_impulse.z;
  334. }
  335. float32 b2RevoluteJoint::GetJointAngle() const
  336. {
  337. b2Body* bA = m_bodyA;
  338. b2Body* bB = m_bodyB;
  339. return bB->m_sweep.a - bA->m_sweep.a - m_referenceAngle;
  340. }
  341. float32 b2RevoluteJoint::GetJointSpeed() const
  342. {
  343. b2Body* bA = m_bodyA;
  344. b2Body* bB = m_bodyB;
  345. return bB->m_angularVelocity - bA->m_angularVelocity;
  346. }
  347. bool b2RevoluteJoint::IsMotorEnabled() const
  348. {
  349. return m_enableMotor;
  350. }
  351. void b2RevoluteJoint::EnableMotor(bool flag)
  352. {
  353. m_bodyA->SetAwake(true);
  354. m_bodyB->SetAwake(true);
  355. m_enableMotor = flag;
  356. }
  357. float32 b2RevoluteJoint::GetMotorTorque(float32 inv_dt) const
  358. {
  359. return inv_dt * m_motorImpulse;
  360. }
  361. void b2RevoluteJoint::SetMotorSpeed(float32 speed)
  362. {
  363. m_bodyA->SetAwake(true);
  364. m_bodyB->SetAwake(true);
  365. m_motorSpeed = speed;
  366. }
  367. void b2RevoluteJoint::SetMaxMotorTorque(float32 torque)
  368. {
  369. m_bodyA->SetAwake(true);
  370. m_bodyB->SetAwake(true);
  371. m_maxMotorTorque = torque;
  372. }
  373. bool b2RevoluteJoint::IsLimitEnabled() const
  374. {
  375. return m_enableLimit;
  376. }
  377. void b2RevoluteJoint::EnableLimit(bool flag)
  378. {
  379. if (flag != m_enableLimit)
  380. {
  381. m_bodyA->SetAwake(true);
  382. m_bodyB->SetAwake(true);
  383. m_enableLimit = flag;
  384. m_impulse.z = 0.0f;
  385. }
  386. }
  387. float32 b2RevoluteJoint::GetLowerLimit() const
  388. {
  389. return m_lowerAngle;
  390. }
  391. float32 b2RevoluteJoint::GetUpperLimit() const
  392. {
  393. return m_upperAngle;
  394. }
  395. void b2RevoluteJoint::SetLimits(float32 lower, float32 upper)
  396. {
  397. b2Assert(lower <= upper);
  398. if (lower != m_lowerAngle || upper != m_upperAngle)
  399. {
  400. m_bodyA->SetAwake(true);
  401. m_bodyB->SetAwake(true);
  402. m_impulse.z = 0.0f;
  403. m_lowerAngle = lower;
  404. m_upperAngle = upper;
  405. }
  406. }
  407. void b2RevoluteJoint::Dump()
  408. {
  409. int32 indexA = m_bodyA->m_islandIndex;
  410. int32 indexB = m_bodyB->m_islandIndex;
  411. b2Log(" b2RevoluteJointDef jd;\n");
  412. b2Log(" jd.bodyA = bodies[%d];\n", indexA);
  413. b2Log(" jd.bodyB = bodies[%d];\n", indexB);
  414. b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  415. b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
  416. b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
  417. b2Log(" jd.referenceAngle = %.15lef;\n", m_referenceAngle);
  418. b2Log(" jd.enableLimit = bool(%d);\n", m_enableLimit);
  419. b2Log(" jd.lowerAngle = %.15lef;\n", m_lowerAngle);
  420. b2Log(" jd.upperAngle = %.15lef;\n", m_upperAngle);
  421. b2Log(" jd.enableMotor = bool(%d);\n", m_enableMotor);
  422. b2Log(" jd.motorSpeed = %.15lef;\n", m_motorSpeed);
  423. b2Log(" jd.maxMotorTorque = %.15lef;\n", m_maxMotorTorque);
  424. b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  425. }