b2_prismatic_joint.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #include "box2d/b2_body.h"
  19. #include "box2d/b2_draw.h"
  20. #include "box2d/b2_prismatic_joint.h"
  21. #include "box2d/b2_time_step.h"
  22. // Linear constraint (point-to-line)
  23. // d = p2 - p1 = x2 + r2 - x1 - r1
  24. // C = dot(perp, d)
  25. // Cdot = dot(d, cross(w1, perp)) + dot(perp, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  26. // = -dot(perp, v1) - dot(cross(d + r1, perp), w1) + dot(perp, v2) + dot(cross(r2, perp), v2)
  27. // J = [-perp, -cross(d + r1, perp), perp, cross(r2,perp)]
  28. //
  29. // Angular constraint
  30. // C = a2 - a1 + a_initial
  31. // Cdot = w2 - w1
  32. // J = [0 0 -1 0 0 1]
  33. //
  34. // K = J * invM * JT
  35. //
  36. // J = [-a -s1 a s2]
  37. // [0 -1 0 1]
  38. // a = perp
  39. // s1 = cross(d + r1, a) = cross(p2 - x1, a)
  40. // s2 = cross(r2, a) = cross(p2 - x2, a)
  41. // Motor/Limit linear constraint
  42. // C = dot(ax1, d)
  43. // Cdot = -dot(ax1, v1) - dot(cross(d + r1, ax1), w1) + dot(ax1, v2) + dot(cross(r2, ax1), v2)
  44. // J = [-ax1 -cross(d+r1,ax1) ax1 cross(r2,ax1)]
  45. // Predictive limit is applied even when the limit is not active.
  46. // Prevents a constraint speed that can lead to a constraint error in one time step.
  47. // Want C2 = C1 + h * Cdot >= 0
  48. // Or:
  49. // Cdot + C1/h >= 0
  50. // I do not apply a negative constraint error because that is handled in position correction.
  51. // So:
  52. // Cdot + max(C1, 0)/h >= 0
  53. // Block Solver
  54. // We develop a block solver that includes the angular and linear constraints. This makes the limit stiffer.
  55. //
  56. // The Jacobian has 2 rows:
  57. // J = [-uT -s1 uT s2] // linear
  58. // [0 -1 0 1] // angular
  59. //
  60. // u = perp
  61. // s1 = cross(d + r1, u), s2 = cross(r2, u)
  62. // a1 = cross(d + r1, v), a2 = cross(r2, v)
  63. void b2PrismaticJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor, const b2Vec2& axis)
  64. {
  65. bodyA = bA;
  66. bodyB = bB;
  67. localAnchorA = bodyA->GetLocalPoint(anchor);
  68. localAnchorB = bodyB->GetLocalPoint(anchor);
  69. localAxisA = bodyA->GetLocalVector(axis);
  70. referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
  71. }
  72. b2PrismaticJoint::b2PrismaticJoint(const b2PrismaticJointDef* def)
  73. : b2Joint(def)
  74. {
  75. m_localAnchorA = def->localAnchorA;
  76. m_localAnchorB = def->localAnchorB;
  77. m_localXAxisA = def->localAxisA;
  78. m_localXAxisA.Normalize();
  79. m_localYAxisA = b2Cross(1.0f, m_localXAxisA);
  80. m_referenceAngle = def->referenceAngle;
  81. m_impulse.SetZero();
  82. m_axialMass = 0.0f;
  83. m_motorImpulse = 0.0f;
  84. m_lowerImpulse = 0.0f;
  85. m_upperImpulse = 0.0f;
  86. m_lowerTranslation = def->lowerTranslation;
  87. m_upperTranslation = def->upperTranslation;
  88. b2Assert(m_lowerTranslation <= m_upperTranslation);
  89. m_maxMotorForce = def->maxMotorForce;
  90. m_motorSpeed = def->motorSpeed;
  91. m_enableLimit = def->enableLimit;
  92. m_enableMotor = def->enableMotor;
  93. m_translation = 0.0f;
  94. m_axis.SetZero();
  95. m_perp.SetZero();
  96. }
  97. void b2PrismaticJoint::InitVelocityConstraints(const b2SolverData& data)
  98. {
  99. m_indexA = m_bodyA->m_islandIndex;
  100. m_indexB = m_bodyB->m_islandIndex;
  101. m_localCenterA = m_bodyA->m_sweep.localCenter;
  102. m_localCenterB = m_bodyB->m_sweep.localCenter;
  103. m_invMassA = m_bodyA->m_invMass;
  104. m_invMassB = m_bodyB->m_invMass;
  105. m_invIA = m_bodyA->m_invI;
  106. m_invIB = m_bodyB->m_invI;
  107. b2Vec2 cA = data.positions[m_indexA].c;
  108. float aA = data.positions[m_indexA].a;
  109. b2Vec2 vA = data.velocities[m_indexA].v;
  110. float wA = data.velocities[m_indexA].w;
  111. b2Vec2 cB = data.positions[m_indexB].c;
  112. float aB = data.positions[m_indexB].a;
  113. b2Vec2 vB = data.velocities[m_indexB].v;
  114. float wB = data.velocities[m_indexB].w;
  115. b2Rot qA(aA), qB(aB);
  116. // Compute the effective masses.
  117. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  118. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  119. b2Vec2 d = (cB - cA) + rB - rA;
  120. float mA = m_invMassA, mB = m_invMassB;
  121. float iA = m_invIA, iB = m_invIB;
  122. // Compute motor Jacobian and effective mass.
  123. {
  124. m_axis = b2Mul(qA, m_localXAxisA);
  125. m_a1 = b2Cross(d + rA, m_axis);
  126. m_a2 = b2Cross(rB, m_axis);
  127. m_axialMass = mA + mB + iA * m_a1 * m_a1 + iB * m_a2 * m_a2;
  128. if (m_axialMass > 0.0f)
  129. {
  130. m_axialMass = 1.0f / m_axialMass;
  131. }
  132. }
  133. // Prismatic constraint.
  134. {
  135. m_perp = b2Mul(qA, m_localYAxisA);
  136. m_s1 = b2Cross(d + rA, m_perp);
  137. m_s2 = b2Cross(rB, m_perp);
  138. float k11 = mA + mB + iA * m_s1 * m_s1 + iB * m_s2 * m_s2;
  139. float k12 = iA * m_s1 + iB * m_s2;
  140. float k22 = iA + iB;
  141. if (k22 == 0.0f)
  142. {
  143. // For bodies with fixed rotation.
  144. k22 = 1.0f;
  145. }
  146. m_K.ex.Set(k11, k12);
  147. m_K.ey.Set(k12, k22);
  148. }
  149. if (m_enableLimit)
  150. {
  151. m_translation = b2Dot(m_axis, d);
  152. }
  153. else
  154. {
  155. m_lowerImpulse = 0.0f;
  156. m_upperImpulse = 0.0f;
  157. }
  158. if (m_enableMotor == false)
  159. {
  160. m_motorImpulse = 0.0f;
  161. }
  162. if (data.step.warmStarting)
  163. {
  164. // Account for variable time step.
  165. m_impulse *= data.step.dtRatio;
  166. m_motorImpulse *= data.step.dtRatio;
  167. m_lowerImpulse *= data.step.dtRatio;
  168. m_upperImpulse *= data.step.dtRatio;
  169. float axialImpulse = m_motorImpulse + m_lowerImpulse - m_upperImpulse;
  170. b2Vec2 P = m_impulse.x * m_perp + axialImpulse * m_axis;
  171. float LA = m_impulse.x * m_s1 + m_impulse.y + axialImpulse * m_a1;
  172. float LB = m_impulse.x * m_s2 + m_impulse.y + axialImpulse * m_a2;
  173. vA -= mA * P;
  174. wA -= iA * LA;
  175. vB += mB * P;
  176. wB += iB * LB;
  177. }
  178. else
  179. {
  180. m_impulse.SetZero();
  181. m_motorImpulse = 0.0f;
  182. m_lowerImpulse = 0.0f;
  183. m_upperImpulse = 0.0f;
  184. }
  185. data.velocities[m_indexA].v = vA;
  186. data.velocities[m_indexA].w = wA;
  187. data.velocities[m_indexB].v = vB;
  188. data.velocities[m_indexB].w = wB;
  189. }
  190. void b2PrismaticJoint::SolveVelocityConstraints(const b2SolverData& data)
  191. {
  192. b2Vec2 vA = data.velocities[m_indexA].v;
  193. float wA = data.velocities[m_indexA].w;
  194. b2Vec2 vB = data.velocities[m_indexB].v;
  195. float wB = data.velocities[m_indexB].w;
  196. float mA = m_invMassA, mB = m_invMassB;
  197. float iA = m_invIA, iB = m_invIB;
  198. // Solve linear motor constraint
  199. if (m_enableMotor)
  200. {
  201. float Cdot = b2Dot(m_axis, vB - vA) + m_a2 * wB - m_a1 * wA;
  202. float impulse = m_axialMass * (m_motorSpeed - Cdot);
  203. float oldImpulse = m_motorImpulse;
  204. float maxImpulse = data.step.dt * m_maxMotorForce;
  205. m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  206. impulse = m_motorImpulse - oldImpulse;
  207. b2Vec2 P = impulse * m_axis;
  208. float LA = impulse * m_a1;
  209. float LB = impulse * m_a2;
  210. vA -= mA * P;
  211. wA -= iA * LA;
  212. vB += mB * P;
  213. wB += iB * LB;
  214. }
  215. if (m_enableLimit)
  216. {
  217. // Lower limit
  218. {
  219. float C = m_translation - m_lowerTranslation;
  220. float Cdot = b2Dot(m_axis, vB - vA) + m_a2 * wB - m_a1 * wA;
  221. float impulse = -m_axialMass * (Cdot + b2Max(C, 0.0f) * data.step.inv_dt);
  222. float oldImpulse = m_lowerImpulse;
  223. m_lowerImpulse = b2Max(m_lowerImpulse + impulse, 0.0f);
  224. impulse = m_lowerImpulse - oldImpulse;
  225. b2Vec2 P = impulse * m_axis;
  226. float LA = impulse * m_a1;
  227. float LB = impulse * m_a2;
  228. vA -= mA * P;
  229. wA -= iA * LA;
  230. vB += mB * P;
  231. wB += iB * LB;
  232. }
  233. // Upper limit
  234. // Note: signs are flipped to keep C positive when the constraint is satisfied.
  235. // This also keeps the impulse positive when the limit is active.
  236. {
  237. float C = m_upperTranslation - m_translation;
  238. float Cdot = b2Dot(m_axis, vA - vB) + m_a1 * wA - m_a2 * wB;
  239. float impulse = -m_axialMass * (Cdot + b2Max(C, 0.0f) * data.step.inv_dt);
  240. float oldImpulse = m_upperImpulse;
  241. m_upperImpulse = b2Max(m_upperImpulse + impulse, 0.0f);
  242. impulse = m_upperImpulse - oldImpulse;
  243. b2Vec2 P = impulse * m_axis;
  244. float LA = impulse * m_a1;
  245. float LB = impulse * m_a2;
  246. vA += mA * P;
  247. wA += iA * LA;
  248. vB -= mB * P;
  249. wB -= iB * LB;
  250. }
  251. }
  252. // Solve the prismatic constraint in block form.
  253. {
  254. b2Vec2 Cdot;
  255. Cdot.x = b2Dot(m_perp, vB - vA) + m_s2 * wB - m_s1 * wA;
  256. Cdot.y = wB - wA;
  257. b2Vec2 df = m_K.Solve(-Cdot);
  258. m_impulse += df;
  259. b2Vec2 P = df.x * m_perp;
  260. float LA = df.x * m_s1 + df.y;
  261. float LB = df.x * m_s2 + df.y;
  262. vA -= mA * P;
  263. wA -= iA * LA;
  264. vB += mB * P;
  265. wB += iB * LB;
  266. }
  267. data.velocities[m_indexA].v = vA;
  268. data.velocities[m_indexA].w = wA;
  269. data.velocities[m_indexB].v = vB;
  270. data.velocities[m_indexB].w = wB;
  271. }
  272. // A velocity based solver computes reaction forces(impulses) using the velocity constraint solver.Under this context,
  273. // the position solver is not there to resolve forces.It is only there to cope with integration error.
  274. //
  275. // Therefore, the pseudo impulses in the position solver do not have any physical meaning.Thus it is okay if they suck.
  276. //
  277. // We could take the active state from the velocity solver.However, the joint might push past the limit when the velocity
  278. // solver indicates the limit is inactive.
  279. bool b2PrismaticJoint::SolvePositionConstraints(const b2SolverData& data)
  280. {
  281. b2Vec2 cA = data.positions[m_indexA].c;
  282. float aA = data.positions[m_indexA].a;
  283. b2Vec2 cB = data.positions[m_indexB].c;
  284. float aB = data.positions[m_indexB].a;
  285. b2Rot qA(aA), qB(aB);
  286. float mA = m_invMassA, mB = m_invMassB;
  287. float iA = m_invIA, iB = m_invIB;
  288. // Compute fresh Jacobians
  289. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  290. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  291. b2Vec2 d = cB + rB - cA - rA;
  292. b2Vec2 axis = b2Mul(qA, m_localXAxisA);
  293. float a1 = b2Cross(d + rA, axis);
  294. float a2 = b2Cross(rB, axis);
  295. b2Vec2 perp = b2Mul(qA, m_localYAxisA);
  296. float s1 = b2Cross(d + rA, perp);
  297. float s2 = b2Cross(rB, perp);
  298. b2Vec3 impulse;
  299. b2Vec2 C1;
  300. C1.x = b2Dot(perp, d);
  301. C1.y = aB - aA - m_referenceAngle;
  302. float linearError = b2Abs(C1.x);
  303. float angularError = b2Abs(C1.y);
  304. bool active = false;
  305. float C2 = 0.0f;
  306. if (m_enableLimit)
  307. {
  308. float translation = b2Dot(axis, d);
  309. if (b2Abs(m_upperTranslation - m_lowerTranslation) < 2.0f * b2_linearSlop)
  310. {
  311. C2 = translation;
  312. linearError = b2Max(linearError, b2Abs(translation));
  313. active = true;
  314. }
  315. else if (translation <= m_lowerTranslation)
  316. {
  317. C2 = b2Min(translation - m_lowerTranslation, 0.0f);
  318. linearError = b2Max(linearError, m_lowerTranslation - translation);
  319. active = true;
  320. }
  321. else if (translation >= m_upperTranslation)
  322. {
  323. C2 = b2Max(translation - m_upperTranslation, 0.0f);
  324. linearError = b2Max(linearError, translation - m_upperTranslation);
  325. active = true;
  326. }
  327. }
  328. if (active)
  329. {
  330. float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
  331. float k12 = iA * s1 + iB * s2;
  332. float k13 = iA * s1 * a1 + iB * s2 * a2;
  333. float k22 = iA + iB;
  334. if (k22 == 0.0f)
  335. {
  336. // For fixed rotation
  337. k22 = 1.0f;
  338. }
  339. float k23 = iA * a1 + iB * a2;
  340. float k33 = mA + mB + iA * a1 * a1 + iB * a2 * a2;
  341. b2Mat33 K;
  342. K.ex.Set(k11, k12, k13);
  343. K.ey.Set(k12, k22, k23);
  344. K.ez.Set(k13, k23, k33);
  345. b2Vec3 C;
  346. C.x = C1.x;
  347. C.y = C1.y;
  348. C.z = C2;
  349. impulse = K.Solve33(-C);
  350. }
  351. else
  352. {
  353. float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
  354. float k12 = iA * s1 + iB * s2;
  355. float k22 = iA + iB;
  356. if (k22 == 0.0f)
  357. {
  358. k22 = 1.0f;
  359. }
  360. b2Mat22 K;
  361. K.ex.Set(k11, k12);
  362. K.ey.Set(k12, k22);
  363. b2Vec2 impulse1 = K.Solve(-C1);
  364. impulse.x = impulse1.x;
  365. impulse.y = impulse1.y;
  366. impulse.z = 0.0f;
  367. }
  368. b2Vec2 P = impulse.x * perp + impulse.z * axis;
  369. float LA = impulse.x * s1 + impulse.y + impulse.z * a1;
  370. float LB = impulse.x * s2 + impulse.y + impulse.z * a2;
  371. cA -= mA * P;
  372. aA -= iA * LA;
  373. cB += mB * P;
  374. aB += iB * LB;
  375. data.positions[m_indexA].c = cA;
  376. data.positions[m_indexA].a = aA;
  377. data.positions[m_indexB].c = cB;
  378. data.positions[m_indexB].a = aB;
  379. return linearError <= b2_linearSlop && angularError <= b2_angularSlop;
  380. }
  381. b2Vec2 b2PrismaticJoint::GetAnchorA() const
  382. {
  383. return m_bodyA->GetWorldPoint(m_localAnchorA);
  384. }
  385. b2Vec2 b2PrismaticJoint::GetAnchorB() const
  386. {
  387. return m_bodyB->GetWorldPoint(m_localAnchorB);
  388. }
  389. b2Vec2 b2PrismaticJoint::GetReactionForce(float inv_dt) const
  390. {
  391. return inv_dt * (m_impulse.x * m_perp + (m_motorImpulse + m_lowerImpulse - m_upperImpulse) * m_axis);
  392. }
  393. float b2PrismaticJoint::GetReactionTorque(float inv_dt) const
  394. {
  395. return inv_dt * m_impulse.y;
  396. }
  397. float b2PrismaticJoint::GetJointTranslation() const
  398. {
  399. b2Vec2 pA = m_bodyA->GetWorldPoint(m_localAnchorA);
  400. b2Vec2 pB = m_bodyB->GetWorldPoint(m_localAnchorB);
  401. b2Vec2 d = pB - pA;
  402. b2Vec2 axis = m_bodyA->GetWorldVector(m_localXAxisA);
  403. float translation = b2Dot(d, axis);
  404. return translation;
  405. }
  406. float b2PrismaticJoint::GetJointSpeed() const
  407. {
  408. b2Body* bA = m_bodyA;
  409. b2Body* bB = m_bodyB;
  410. b2Vec2 rA = b2Mul(bA->m_xf.q, m_localAnchorA - bA->m_sweep.localCenter);
  411. b2Vec2 rB = b2Mul(bB->m_xf.q, m_localAnchorB - bB->m_sweep.localCenter);
  412. b2Vec2 p1 = bA->m_sweep.c + rA;
  413. b2Vec2 p2 = bB->m_sweep.c + rB;
  414. b2Vec2 d = p2 - p1;
  415. b2Vec2 axis = b2Mul(bA->m_xf.q, m_localXAxisA);
  416. b2Vec2 vA = bA->m_linearVelocity;
  417. b2Vec2 vB = bB->m_linearVelocity;
  418. float wA = bA->m_angularVelocity;
  419. float wB = bB->m_angularVelocity;
  420. float speed = b2Dot(d, b2Cross(wA, axis)) + b2Dot(axis, vB + b2Cross(wB, rB) - vA - b2Cross(wA, rA));
  421. return speed;
  422. }
  423. bool b2PrismaticJoint::IsLimitEnabled() const
  424. {
  425. return m_enableLimit;
  426. }
  427. void b2PrismaticJoint::EnableLimit(bool flag)
  428. {
  429. if (flag != m_enableLimit)
  430. {
  431. m_bodyA->SetAwake(true);
  432. m_bodyB->SetAwake(true);
  433. m_enableLimit = flag;
  434. m_lowerImpulse = 0.0f;
  435. m_upperImpulse = 0.0f;
  436. }
  437. }
  438. float b2PrismaticJoint::GetLowerLimit() const
  439. {
  440. return m_lowerTranslation;
  441. }
  442. float b2PrismaticJoint::GetUpperLimit() const
  443. {
  444. return m_upperTranslation;
  445. }
  446. void b2PrismaticJoint::SetLimits(float lower, float upper)
  447. {
  448. b2Assert(lower <= upper);
  449. if (lower != m_lowerTranslation || upper != m_upperTranslation)
  450. {
  451. m_bodyA->SetAwake(true);
  452. m_bodyB->SetAwake(true);
  453. m_lowerTranslation = lower;
  454. m_upperTranslation = upper;
  455. m_lowerImpulse = 0.0f;
  456. m_upperImpulse = 0.0f;
  457. }
  458. }
  459. bool b2PrismaticJoint::IsMotorEnabled() const
  460. {
  461. return m_enableMotor;
  462. }
  463. void b2PrismaticJoint::EnableMotor(bool flag)
  464. {
  465. if (flag != m_enableMotor)
  466. {
  467. m_bodyA->SetAwake(true);
  468. m_bodyB->SetAwake(true);
  469. m_enableMotor = flag;
  470. }
  471. }
  472. void b2PrismaticJoint::SetMotorSpeed(float speed)
  473. {
  474. if (speed != m_motorSpeed)
  475. {
  476. m_bodyA->SetAwake(true);
  477. m_bodyB->SetAwake(true);
  478. m_motorSpeed = speed;
  479. }
  480. }
  481. void b2PrismaticJoint::SetMaxMotorForce(float force)
  482. {
  483. if (force != m_maxMotorForce)
  484. {
  485. m_bodyA->SetAwake(true);
  486. m_bodyB->SetAwake(true);
  487. m_maxMotorForce = force;
  488. }
  489. }
  490. float b2PrismaticJoint::GetMotorForce(float inv_dt) const
  491. {
  492. return inv_dt * m_motorImpulse;
  493. }
  494. void b2PrismaticJoint::Dump()
  495. {
  496. // FLT_DECIMAL_DIG == 9
  497. int32 indexA = m_bodyA->m_islandIndex;
  498. int32 indexB = m_bodyB->m_islandIndex;
  499. b2Dump(" b2PrismaticJointDef jd;\n");
  500. b2Dump(" jd.bodyA = bodies[%d];\n", indexA);
  501. b2Dump(" jd.bodyB = bodies[%d];\n", indexB);
  502. b2Dump(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  503. b2Dump(" jd.localAnchorA.Set(%.9g, %.9g);\n", m_localAnchorA.x, m_localAnchorA.y);
  504. b2Dump(" jd.localAnchorB.Set(%.9g, %.9g);\n", m_localAnchorB.x, m_localAnchorB.y);
  505. b2Dump(" jd.localAxisA.Set(%.9g, %.9g);\n", m_localXAxisA.x, m_localXAxisA.y);
  506. b2Dump(" jd.referenceAngle = %.9g;\n", m_referenceAngle);
  507. b2Dump(" jd.enableLimit = bool(%d);\n", m_enableLimit);
  508. b2Dump(" jd.lowerTranslation = %.9g;\n", m_lowerTranslation);
  509. b2Dump(" jd.upperTranslation = %.9g;\n", m_upperTranslation);
  510. b2Dump(" jd.enableMotor = bool(%d);\n", m_enableMotor);
  511. b2Dump(" jd.motorSpeed = %.9g;\n", m_motorSpeed);
  512. b2Dump(" jd.maxMotorForce = %.9g;\n", m_maxMotorForce);
  513. b2Dump(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  514. }
  515. void b2PrismaticJoint::Draw(b2Draw* draw) const
  516. {
  517. const b2Transform& xfA = m_bodyA->GetTransform();
  518. const b2Transform& xfB = m_bodyB->GetTransform();
  519. b2Vec2 pA = b2Mul(xfA, m_localAnchorA);
  520. b2Vec2 pB = b2Mul(xfB, m_localAnchorB);
  521. b2Vec2 axis = b2Mul(xfA.q, m_localXAxisA);
  522. b2Color c1(0.7f, 0.7f, 0.7f);
  523. b2Color c2(0.3f, 0.9f, 0.3f);
  524. b2Color c3(0.9f, 0.3f, 0.3f);
  525. b2Color c4(0.3f, 0.3f, 0.9f);
  526. b2Color c5(0.4f, 0.4f, 0.4f);
  527. draw->DrawSegment(pA, pB, c5);
  528. if (m_enableLimit)
  529. {
  530. b2Vec2 lower = pA + m_lowerTranslation * axis;
  531. b2Vec2 upper = pA + m_upperTranslation * axis;
  532. b2Vec2 perp = b2Mul(xfA.q, m_localYAxisA);
  533. draw->DrawSegment(lower, upper, c1);
  534. draw->DrawSegment(lower - 0.5f * perp, lower + 0.5f * perp, c2);
  535. draw->DrawSegment(upper - 0.5f * perp, upper + 0.5f * perp, c3);
  536. }
  537. else
  538. {
  539. draw->DrawSegment(pA - 1.0f * axis, pA + 1.0f * axis, c1);
  540. }
  541. draw->DrawPoint(pA, 5.0f, c1);
  542. draw->DrawPoint(pB, 5.0f, c4);
  543. }