b2PulleyJoint.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 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 "b2PulleyJoint.h"
  19. #include "../b2Body.h"
  20. #include "../b2World.h"
  21. // Pulley:
  22. // length1 = norm(p1 - s1)
  23. // length2 = norm(p2 - s2)
  24. // C0 = (length1 + ratio * length2)_initial
  25. // C = C0 - (length1 + ratio * length2) >= 0
  26. // u1 = (p1 - s1) / norm(p1 - s1)
  27. // u2 = (p2 - s2) / norm(p2 - s2)
  28. // Cdot = -dot(u1, v1 + cross(w1, r1)) - ratio * dot(u2, v2 + cross(w2, r2))
  29. // J = -[u1 cross(r1, u1) ratio * u2 ratio * cross(r2, u2)]
  30. // K = J * invM * JT
  31. // = invMass1 + invI1 * cross(r1, u1)^2 + ratio^2 * (invMass2 + invI2 * cross(r2, u2)^2)
  32. //
  33. // Limit:
  34. // C = maxLength - length
  35. // u = (p - s) / norm(p - s)
  36. // Cdot = -dot(u, v + cross(w, r))
  37. // K = invMass + invI * cross(r, u)^2
  38. // 0 <= impulse
  39. void b2PulleyJointDef::Initialize(b2Body* b1, b2Body* b2,
  40. const b2Vec2& ga1, const b2Vec2& ga2,
  41. const b2Vec2& anchor1, const b2Vec2& anchor2,
  42. float32 r)
  43. {
  44. body1 = b1;
  45. body2 = b2;
  46. groundAnchor1 = ga1;
  47. groundAnchor2 = ga2;
  48. localAnchor1 = body1->GetLocalPoint(anchor1);
  49. localAnchor2 = body2->GetLocalPoint(anchor2);
  50. b2Vec2 d1 = anchor1 - ga1;
  51. length1 = d1.Length();
  52. b2Vec2 d2 = anchor2 - ga2;
  53. length2 = d2.Length();
  54. ratio = r;
  55. b2Assert(ratio > B2_FLT_EPSILON);
  56. float32 C = length1 + ratio * length2;
  57. maxLength1 = C - ratio * b2_minPulleyLength;
  58. maxLength2 = (C - b2_minPulleyLength) / ratio;
  59. }
  60. b2PulleyJoint::b2PulleyJoint(const b2PulleyJointDef* def)
  61. : b2Joint(def)
  62. {
  63. m_ground = m_body1->GetWorld()->GetGroundBody();
  64. m_groundAnchor1 = def->groundAnchor1 - m_ground->GetXForm().position;
  65. m_groundAnchor2 = def->groundAnchor2 - m_ground->GetXForm().position;
  66. m_localAnchor1 = def->localAnchor1;
  67. m_localAnchor2 = def->localAnchor2;
  68. b2Assert(def->ratio != 0.0f);
  69. m_ratio = def->ratio;
  70. m_constant = def->length1 + m_ratio * def->length2;
  71. m_maxLength1 = b2Min(def->maxLength1, m_constant - m_ratio * b2_minPulleyLength);
  72. m_maxLength2 = b2Min(def->maxLength2, (m_constant - b2_minPulleyLength) / m_ratio);
  73. m_impulse = 0.0f;
  74. m_limitImpulse1 = 0.0f;
  75. m_limitImpulse2 = 0.0f;
  76. }
  77. void b2PulleyJoint::InitVelocityConstraints(const b2TimeStep& step)
  78. {
  79. b2Body* b1 = m_body1;
  80. b2Body* b2 = m_body2;
  81. b2Vec2 r1 = b2Mul(b1->GetXForm().R, m_localAnchor1 - b1->GetLocalCenter());
  82. b2Vec2 r2 = b2Mul(b2->GetXForm().R, m_localAnchor2 - b2->GetLocalCenter());
  83. b2Vec2 p1 = b1->m_sweep.c + r1;
  84. b2Vec2 p2 = b2->m_sweep.c + r2;
  85. b2Vec2 s1 = m_ground->GetXForm().position + m_groundAnchor1;
  86. b2Vec2 s2 = m_ground->GetXForm().position + m_groundAnchor2;
  87. // Get the pulley axes.
  88. m_u1 = p1 - s1;
  89. m_u2 = p2 - s2;
  90. float32 length1 = m_u1.Length();
  91. float32 length2 = m_u2.Length();
  92. if (length1 > b2_linearSlop)
  93. {
  94. m_u1 *= 1.0f / length1;
  95. }
  96. else
  97. {
  98. m_u1.SetZero();
  99. }
  100. if (length2 > b2_linearSlop)
  101. {
  102. m_u2 *= 1.0f / length2;
  103. }
  104. else
  105. {
  106. m_u2.SetZero();
  107. }
  108. float32 C = m_constant - length1 - m_ratio * length2;
  109. if (C > 0.0f)
  110. {
  111. m_state = e_inactiveLimit;
  112. m_impulse = 0.0f;
  113. }
  114. else
  115. {
  116. m_state = e_atUpperLimit;
  117. }
  118. if (length1 < m_maxLength1)
  119. {
  120. m_limitState1 = e_inactiveLimit;
  121. m_limitImpulse1 = 0.0f;
  122. }
  123. else
  124. {
  125. m_limitState1 = e_atUpperLimit;
  126. }
  127. if (length2 < m_maxLength2)
  128. {
  129. m_limitState2 = e_inactiveLimit;
  130. m_limitImpulse2 = 0.0f;
  131. }
  132. else
  133. {
  134. m_limitState2 = e_atUpperLimit;
  135. }
  136. // Compute effective mass.
  137. float32 cr1u1 = b2Cross(r1, m_u1);
  138. float32 cr2u2 = b2Cross(r2, m_u2);
  139. m_limitMass1 = b1->m_invMass + b1->m_invI * cr1u1 * cr1u1;
  140. m_limitMass2 = b2->m_invMass + b2->m_invI * cr2u2 * cr2u2;
  141. m_pulleyMass = m_limitMass1 + m_ratio * m_ratio * m_limitMass2;
  142. b2Assert(m_limitMass1 > B2_FLT_EPSILON);
  143. b2Assert(m_limitMass2 > B2_FLT_EPSILON);
  144. b2Assert(m_pulleyMass > B2_FLT_EPSILON);
  145. m_limitMass1 = 1.0f / m_limitMass1;
  146. m_limitMass2 = 1.0f / m_limitMass2;
  147. m_pulleyMass = 1.0f / m_pulleyMass;
  148. if (step.warmStarting)
  149. {
  150. // Scale impulses to support variable time steps.
  151. m_impulse *= step.dtRatio;
  152. m_limitImpulse1 *= step.dtRatio;
  153. m_limitImpulse2 *= step.dtRatio;
  154. // Warm starting.
  155. b2Vec2 P1 = -(m_impulse + m_limitImpulse1) * m_u1;
  156. b2Vec2 P2 = (-m_ratio * m_impulse - m_limitImpulse2) * m_u2;
  157. b1->m_linearVelocity += b1->m_invMass * P1;
  158. b1->m_angularVelocity += b1->m_invI * b2Cross(r1, P1);
  159. b2->m_linearVelocity += b2->m_invMass * P2;
  160. b2->m_angularVelocity += b2->m_invI * b2Cross(r2, P2);
  161. }
  162. else
  163. {
  164. m_impulse = 0.0f;
  165. m_limitImpulse1 = 0.0f;
  166. m_limitImpulse2 = 0.0f;
  167. }
  168. }
  169. void b2PulleyJoint::SolveVelocityConstraints(const b2TimeStep& step)
  170. {
  171. B2_NOT_USED(step);
  172. b2Body* b1 = m_body1;
  173. b2Body* b2 = m_body2;
  174. b2Vec2 r1 = b2Mul(b1->GetXForm().R, m_localAnchor1 - b1->GetLocalCenter());
  175. b2Vec2 r2 = b2Mul(b2->GetXForm().R, m_localAnchor2 - b2->GetLocalCenter());
  176. if (m_state == e_atUpperLimit)
  177. {
  178. b2Vec2 v1 = b1->m_linearVelocity + b2Cross(b1->m_angularVelocity, r1);
  179. b2Vec2 v2 = b2->m_linearVelocity + b2Cross(b2->m_angularVelocity, r2);
  180. float32 Cdot = -b2Dot(m_u1, v1) - m_ratio * b2Dot(m_u2, v2);
  181. float32 impulse = m_pulleyMass * (-Cdot);
  182. float32 oldImpulse = m_impulse;
  183. m_impulse = b2Max(0.0f, m_impulse + impulse);
  184. impulse = m_impulse - oldImpulse;
  185. b2Vec2 P1 = -impulse * m_u1;
  186. b2Vec2 P2 = -m_ratio * impulse * m_u2;
  187. b1->m_linearVelocity += b1->m_invMass * P1;
  188. b1->m_angularVelocity += b1->m_invI * b2Cross(r1, P1);
  189. b2->m_linearVelocity += b2->m_invMass * P2;
  190. b2->m_angularVelocity += b2->m_invI * b2Cross(r2, P2);
  191. }
  192. if (m_limitState1 == e_atUpperLimit)
  193. {
  194. b2Vec2 v1 = b1->m_linearVelocity + b2Cross(b1->m_angularVelocity, r1);
  195. float32 Cdot = -b2Dot(m_u1, v1);
  196. float32 impulse = -m_limitMass1 * Cdot;
  197. float32 oldImpulse = m_limitImpulse1;
  198. m_limitImpulse1 = b2Max(0.0f, m_limitImpulse1 + impulse);
  199. impulse = m_limitImpulse1 - oldImpulse;
  200. b2Vec2 P1 = -impulse * m_u1;
  201. b1->m_linearVelocity += b1->m_invMass * P1;
  202. b1->m_angularVelocity += b1->m_invI * b2Cross(r1, P1);
  203. }
  204. if (m_limitState2 == e_atUpperLimit)
  205. {
  206. b2Vec2 v2 = b2->m_linearVelocity + b2Cross(b2->m_angularVelocity, r2);
  207. float32 Cdot = -b2Dot(m_u2, v2);
  208. float32 impulse = -m_limitMass2 * Cdot;
  209. float32 oldImpulse = m_limitImpulse2;
  210. m_limitImpulse2 = b2Max(0.0f, m_limitImpulse2 + impulse);
  211. impulse = m_limitImpulse2 - oldImpulse;
  212. b2Vec2 P2 = -impulse * m_u2;
  213. b2->m_linearVelocity += b2->m_invMass * P2;
  214. b2->m_angularVelocity += b2->m_invI * b2Cross(r2, P2);
  215. }
  216. }
  217. bool b2PulleyJoint::SolvePositionConstraints(float32 baumgarte)
  218. {
  219. B2_NOT_USED(baumgarte);
  220. b2Body* b1 = m_body1;
  221. b2Body* b2 = m_body2;
  222. b2Vec2 s1 = m_ground->GetXForm().position + m_groundAnchor1;
  223. b2Vec2 s2 = m_ground->GetXForm().position + m_groundAnchor2;
  224. float32 linearError = 0.0f;
  225. if (m_state == e_atUpperLimit)
  226. {
  227. b2Vec2 r1 = b2Mul(b1->GetXForm().R, m_localAnchor1 - b1->GetLocalCenter());
  228. b2Vec2 r2 = b2Mul(b2->GetXForm().R, m_localAnchor2 - b2->GetLocalCenter());
  229. b2Vec2 p1 = b1->m_sweep.c + r1;
  230. b2Vec2 p2 = b2->m_sweep.c + r2;
  231. // Get the pulley axes.
  232. m_u1 = p1 - s1;
  233. m_u2 = p2 - s2;
  234. float32 length1 = m_u1.Length();
  235. float32 length2 = m_u2.Length();
  236. if (length1 > b2_linearSlop)
  237. {
  238. m_u1 *= 1.0f / length1;
  239. }
  240. else
  241. {
  242. m_u1.SetZero();
  243. }
  244. if (length2 > b2_linearSlop)
  245. {
  246. m_u2 *= 1.0f / length2;
  247. }
  248. else
  249. {
  250. m_u2.SetZero();
  251. }
  252. float32 C = m_constant - length1 - m_ratio * length2;
  253. linearError = b2Max(linearError, -C);
  254. C = b2Clamp(C + b2_linearSlop, -b2_maxLinearCorrection, 0.0f);
  255. float32 impulse = -m_pulleyMass * C;
  256. b2Vec2 P1 = -impulse * m_u1;
  257. b2Vec2 P2 = -m_ratio * impulse * m_u2;
  258. b1->m_sweep.c += b1->m_invMass * P1;
  259. b1->m_sweep.a += b1->m_invI * b2Cross(r1, P1);
  260. b2->m_sweep.c += b2->m_invMass * P2;
  261. b2->m_sweep.a += b2->m_invI * b2Cross(r2, P2);
  262. b1->SynchronizeTransform();
  263. b2->SynchronizeTransform();
  264. }
  265. if (m_limitState1 == e_atUpperLimit)
  266. {
  267. b2Vec2 r1 = b2Mul(b1->GetXForm().R, m_localAnchor1 - b1->GetLocalCenter());
  268. b2Vec2 p1 = b1->m_sweep.c + r1;
  269. m_u1 = p1 - s1;
  270. float32 length1 = m_u1.Length();
  271. if (length1 > b2_linearSlop)
  272. {
  273. m_u1 *= 1.0f / length1;
  274. }
  275. else
  276. {
  277. m_u1.SetZero();
  278. }
  279. float32 C = m_maxLength1 - length1;
  280. linearError = b2Max(linearError, -C);
  281. C = b2Clamp(C + b2_linearSlop, -b2_maxLinearCorrection, 0.0f);
  282. float32 impulse = -m_limitMass1 * C;
  283. b2Vec2 P1 = -impulse * m_u1;
  284. b1->m_sweep.c += b1->m_invMass * P1;
  285. b1->m_sweep.a += b1->m_invI * b2Cross(r1, P1);
  286. b1->SynchronizeTransform();
  287. }
  288. if (m_limitState2 == e_atUpperLimit)
  289. {
  290. b2Vec2 r2 = b2Mul(b2->GetXForm().R, m_localAnchor2 - b2->GetLocalCenter());
  291. b2Vec2 p2 = b2->m_sweep.c + r2;
  292. m_u2 = p2 - s2;
  293. float32 length2 = m_u2.Length();
  294. if (length2 > b2_linearSlop)
  295. {
  296. m_u2 *= 1.0f / length2;
  297. }
  298. else
  299. {
  300. m_u2.SetZero();
  301. }
  302. float32 C = m_maxLength2 - length2;
  303. linearError = b2Max(linearError, -C);
  304. C = b2Clamp(C + b2_linearSlop, -b2_maxLinearCorrection, 0.0f);
  305. float32 impulse = -m_limitMass2 * C;
  306. b2Vec2 P2 = -impulse * m_u2;
  307. b2->m_sweep.c += b2->m_invMass * P2;
  308. b2->m_sweep.a += b2->m_invI * b2Cross(r2, P2);
  309. b2->SynchronizeTransform();
  310. }
  311. return linearError < b2_linearSlop;
  312. }
  313. b2Vec2 b2PulleyJoint::GetAnchor1() const
  314. {
  315. return m_body1->GetWorldPoint(m_localAnchor1);
  316. }
  317. b2Vec2 b2PulleyJoint::GetAnchor2() const
  318. {
  319. return m_body2->GetWorldPoint(m_localAnchor2);
  320. }
  321. b2Vec2 b2PulleyJoint::GetReactionForce(float32 inv_dt) const
  322. {
  323. b2Vec2 P = m_impulse * m_u2;
  324. return inv_dt * P;
  325. }
  326. float32 b2PulleyJoint::GetReactionTorque(float32 inv_dt) const
  327. {
  328. B2_NOT_USED(inv_dt);
  329. return 0.0f;
  330. }
  331. b2Vec2 b2PulleyJoint::GetGroundAnchor1() const
  332. {
  333. return m_ground->GetXForm().position + m_groundAnchor1;
  334. }
  335. b2Vec2 b2PulleyJoint::GetGroundAnchor2() const
  336. {
  337. return m_ground->GetXForm().position + m_groundAnchor2;
  338. }
  339. float32 b2PulleyJoint::GetLength1() const
  340. {
  341. b2Vec2 p = m_body1->GetWorldPoint(m_localAnchor1);
  342. b2Vec2 s = m_ground->GetXForm().position + m_groundAnchor1;
  343. b2Vec2 d = p - s;
  344. return d.Length();
  345. }
  346. float32 b2PulleyJoint::GetLength2() const
  347. {
  348. b2Vec2 p = m_body2->GetWorldPoint(m_localAnchor2);
  349. b2Vec2 s = m_ground->GetXForm().position + m_groundAnchor2;
  350. b2Vec2 d = p - s;
  351. return d.Length();
  352. }
  353. float32 b2PulleyJoint::GetRatio() const
  354. {
  355. return m_ratio;
  356. }