b2_wheel_joint.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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_wheel_joint.h"
  21. #include "box2d/b2_time_step.h"
  22. // Linear constraint (point-to-line)
  23. // d = pB - pA = xB + rB - xA - rA
  24. // C = dot(ay, d)
  25. // Cdot = dot(d, cross(wA, ay)) + dot(ay, vB + cross(wB, rB) - vA - cross(wA, rA))
  26. // = -dot(ay, vA) - dot(cross(d + rA, ay), wA) + dot(ay, vB) + dot(cross(rB, ay), vB)
  27. // J = [-ay, -cross(d + rA, ay), ay, cross(rB, ay)]
  28. // Spring linear constraint
  29. // C = dot(ax, d)
  30. // Cdot = = -dot(ax, vA) - dot(cross(d + rA, ax), wA) + dot(ax, vB) + dot(cross(rB, ax), vB)
  31. // J = [-ax -cross(d+rA, ax) ax cross(rB, ax)]
  32. // Motor rotational constraint
  33. // Cdot = wB - wA
  34. // J = [0 0 -1 0 0 1]
  35. void b2WheelJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor, const b2Vec2& axis)
  36. {
  37. bodyA = bA;
  38. bodyB = bB;
  39. localAnchorA = bodyA->GetLocalPoint(anchor);
  40. localAnchorB = bodyB->GetLocalPoint(anchor);
  41. localAxisA = bodyA->GetLocalVector(axis);
  42. }
  43. b2WheelJoint::b2WheelJoint(const b2WheelJointDef* def)
  44. : b2Joint(def)
  45. {
  46. m_localAnchorA = def->localAnchorA;
  47. m_localAnchorB = def->localAnchorB;
  48. m_localXAxisA = def->localAxisA;
  49. m_localYAxisA = b2Cross(1.0f, m_localXAxisA);
  50. m_mass = 0.0f;
  51. m_impulse = 0.0f;
  52. m_motorMass = 0.0f;
  53. m_motorImpulse = 0.0f;
  54. m_springMass = 0.0f;
  55. m_springImpulse = 0.0f;
  56. m_axialMass = 0.0f;
  57. m_lowerImpulse = 0.0f;
  58. m_upperImpulse = 0.0f;
  59. m_lowerTranslation = def->lowerTranslation;
  60. m_upperTranslation = def->upperTranslation;
  61. m_enableLimit = def->enableLimit;
  62. m_maxMotorTorque = def->maxMotorTorque;
  63. m_motorSpeed = def->motorSpeed;
  64. m_enableMotor = def->enableMotor;
  65. m_bias = 0.0f;
  66. m_gamma = 0.0f;
  67. m_ax.SetZero();
  68. m_ay.SetZero();
  69. m_stiffness = def->stiffness;
  70. m_damping = def->damping;
  71. }
  72. void b2WheelJoint::InitVelocityConstraints(const b2SolverData& data)
  73. {
  74. m_indexA = m_bodyA->m_islandIndex;
  75. m_indexB = m_bodyB->m_islandIndex;
  76. m_localCenterA = m_bodyA->m_sweep.localCenter;
  77. m_localCenterB = m_bodyB->m_sweep.localCenter;
  78. m_invMassA = m_bodyA->m_invMass;
  79. m_invMassB = m_bodyB->m_invMass;
  80. m_invIA = m_bodyA->m_invI;
  81. m_invIB = m_bodyB->m_invI;
  82. float mA = m_invMassA, mB = m_invMassB;
  83. float iA = m_invIA, iB = m_invIB;
  84. b2Vec2 cA = data.positions[m_indexA].c;
  85. float aA = data.positions[m_indexA].a;
  86. b2Vec2 vA = data.velocities[m_indexA].v;
  87. float wA = data.velocities[m_indexA].w;
  88. b2Vec2 cB = data.positions[m_indexB].c;
  89. float aB = data.positions[m_indexB].a;
  90. b2Vec2 vB = data.velocities[m_indexB].v;
  91. float wB = data.velocities[m_indexB].w;
  92. b2Rot qA(aA), qB(aB);
  93. // Compute the effective masses.
  94. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  95. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  96. b2Vec2 d = cB + rB - cA - rA;
  97. // Point to line constraint
  98. {
  99. m_ay = b2Mul(qA, m_localYAxisA);
  100. m_sAy = b2Cross(d + rA, m_ay);
  101. m_sBy = b2Cross(rB, m_ay);
  102. m_mass = mA + mB + iA * m_sAy * m_sAy + iB * m_sBy * m_sBy;
  103. if (m_mass > 0.0f)
  104. {
  105. m_mass = 1.0f / m_mass;
  106. }
  107. }
  108. // Spring constraint
  109. m_ax = b2Mul(qA, m_localXAxisA);
  110. m_sAx = b2Cross(d + rA, m_ax);
  111. m_sBx = b2Cross(rB, m_ax);
  112. const float invMass = mA + mB + iA * m_sAx * m_sAx + iB * m_sBx * m_sBx;
  113. if (invMass > 0.0f)
  114. {
  115. m_axialMass = 1.0f / invMass;
  116. }
  117. else
  118. {
  119. m_axialMass = 0.0f;
  120. }
  121. m_springMass = 0.0f;
  122. m_bias = 0.0f;
  123. m_gamma = 0.0f;
  124. if (m_stiffness > 0.0f && invMass > 0.0f)
  125. {
  126. m_springMass = 1.0f / invMass;
  127. float C = b2Dot(d, m_ax);
  128. // magic formulas
  129. float h = data.step.dt;
  130. m_gamma = h * (m_damping + h * m_stiffness);
  131. if (m_gamma > 0.0f)
  132. {
  133. m_gamma = 1.0f / m_gamma;
  134. }
  135. m_bias = C * h * m_stiffness * m_gamma;
  136. m_springMass = invMass + m_gamma;
  137. if (m_springMass > 0.0f)
  138. {
  139. m_springMass = 1.0f / m_springMass;
  140. }
  141. }
  142. else
  143. {
  144. m_springImpulse = 0.0f;
  145. }
  146. if (m_enableLimit)
  147. {
  148. m_translation = b2Dot(m_ax, d);
  149. }
  150. else
  151. {
  152. m_lowerImpulse = 0.0f;
  153. m_upperImpulse = 0.0f;
  154. }
  155. if (m_enableMotor)
  156. {
  157. m_motorMass = iA + iB;
  158. if (m_motorMass > 0.0f)
  159. {
  160. m_motorMass = 1.0f / m_motorMass;
  161. }
  162. }
  163. else
  164. {
  165. m_motorMass = 0.0f;
  166. m_motorImpulse = 0.0f;
  167. }
  168. if (data.step.warmStarting)
  169. {
  170. // Account for variable time step.
  171. m_impulse *= data.step.dtRatio;
  172. m_springImpulse *= data.step.dtRatio;
  173. m_motorImpulse *= data.step.dtRatio;
  174. float axialImpulse = m_springImpulse + m_lowerImpulse - m_upperImpulse;
  175. b2Vec2 P = m_impulse * m_ay + axialImpulse * m_ax;
  176. float LA = m_impulse * m_sAy + axialImpulse * m_sAx + m_motorImpulse;
  177. float LB = m_impulse * m_sBy + axialImpulse * m_sBx + m_motorImpulse;
  178. vA -= m_invMassA * P;
  179. wA -= m_invIA * LA;
  180. vB += m_invMassB * P;
  181. wB += m_invIB * LB;
  182. }
  183. else
  184. {
  185. m_impulse = 0.0f;
  186. m_springImpulse = 0.0f;
  187. m_motorImpulse = 0.0f;
  188. m_lowerImpulse = 0.0f;
  189. m_upperImpulse = 0.0f;
  190. }
  191. data.velocities[m_indexA].v = vA;
  192. data.velocities[m_indexA].w = wA;
  193. data.velocities[m_indexB].v = vB;
  194. data.velocities[m_indexB].w = wB;
  195. }
  196. void b2WheelJoint::SolveVelocityConstraints(const b2SolverData& data)
  197. {
  198. float mA = m_invMassA, mB = m_invMassB;
  199. float iA = m_invIA, iB = m_invIB;
  200. b2Vec2 vA = data.velocities[m_indexA].v;
  201. float wA = data.velocities[m_indexA].w;
  202. b2Vec2 vB = data.velocities[m_indexB].v;
  203. float wB = data.velocities[m_indexB].w;
  204. // Solve spring constraint
  205. {
  206. float Cdot = b2Dot(m_ax, vB - vA) + m_sBx * wB - m_sAx * wA;
  207. float impulse = -m_springMass * (Cdot + m_bias + m_gamma * m_springImpulse);
  208. m_springImpulse += impulse;
  209. b2Vec2 P = impulse * m_ax;
  210. float LA = impulse * m_sAx;
  211. float LB = impulse * m_sBx;
  212. vA -= mA * P;
  213. wA -= iA * LA;
  214. vB += mB * P;
  215. wB += iB * LB;
  216. }
  217. // Solve rotational motor constraint
  218. {
  219. float Cdot = wB - wA - m_motorSpeed;
  220. float impulse = -m_motorMass * Cdot;
  221. float oldImpulse = m_motorImpulse;
  222. float maxImpulse = data.step.dt * m_maxMotorTorque;
  223. m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  224. impulse = m_motorImpulse - oldImpulse;
  225. wA -= iA * impulse;
  226. wB += iB * impulse;
  227. }
  228. if (m_enableLimit)
  229. {
  230. // Lower limit
  231. {
  232. float C = m_translation - m_lowerTranslation;
  233. float Cdot = b2Dot(m_ax, vB - vA) + m_sBx * wB - m_sAx * wA;
  234. float impulse = -m_axialMass * (Cdot + b2Max(C, 0.0f) * data.step.inv_dt);
  235. float oldImpulse = m_lowerImpulse;
  236. m_lowerImpulse = b2Max(m_lowerImpulse + impulse, 0.0f);
  237. impulse = m_lowerImpulse - oldImpulse;
  238. b2Vec2 P = impulse * m_ax;
  239. float LA = impulse * m_sAx;
  240. float LB = impulse * m_sBx;
  241. vA -= mA * P;
  242. wA -= iA * LA;
  243. vB += mB * P;
  244. wB += iB * LB;
  245. }
  246. // Upper limit
  247. // Note: signs are flipped to keep C positive when the constraint is satisfied.
  248. // This also keeps the impulse positive when the limit is active.
  249. {
  250. float C = m_upperTranslation - m_translation;
  251. float Cdot = b2Dot(m_ax, vA - vB) + m_sAx * wA - m_sBx * wB;
  252. float impulse = -m_axialMass * (Cdot + b2Max(C, 0.0f) * data.step.inv_dt);
  253. float oldImpulse = m_upperImpulse;
  254. m_upperImpulse = b2Max(m_upperImpulse + impulse, 0.0f);
  255. impulse = m_upperImpulse - oldImpulse;
  256. b2Vec2 P = impulse * m_ax;
  257. float LA = impulse * m_sAx;
  258. float LB = impulse * m_sBx;
  259. vA += mA * P;
  260. wA += iA * LA;
  261. vB -= mB * P;
  262. wB -= iB * LB;
  263. }
  264. }
  265. // Solve point to line constraint
  266. {
  267. float Cdot = b2Dot(m_ay, vB - vA) + m_sBy * wB - m_sAy * wA;
  268. float impulse = -m_mass * Cdot;
  269. m_impulse += impulse;
  270. b2Vec2 P = impulse * m_ay;
  271. float LA = impulse * m_sAy;
  272. float LB = impulse * m_sBy;
  273. vA -= mA * P;
  274. wA -= iA * LA;
  275. vB += mB * P;
  276. wB += iB * LB;
  277. }
  278. data.velocities[m_indexA].v = vA;
  279. data.velocities[m_indexA].w = wA;
  280. data.velocities[m_indexB].v = vB;
  281. data.velocities[m_indexB].w = wB;
  282. }
  283. bool b2WheelJoint::SolvePositionConstraints(const b2SolverData& data)
  284. {
  285. b2Vec2 cA = data.positions[m_indexA].c;
  286. float aA = data.positions[m_indexA].a;
  287. b2Vec2 cB = data.positions[m_indexB].c;
  288. float aB = data.positions[m_indexB].a;
  289. float linearError = 0.0f;
  290. if (m_enableLimit)
  291. {
  292. b2Rot qA(aA), qB(aB);
  293. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  294. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  295. b2Vec2 d = (cB - cA) + rB - rA;
  296. b2Vec2 ax = b2Mul(qA, m_localXAxisA);
  297. float sAx = b2Cross(d + rA, m_ax);
  298. float sBx = b2Cross(rB, m_ax);
  299. float C = 0.0f;
  300. float translation = b2Dot(ax, d);
  301. if (b2Abs(m_upperTranslation - m_lowerTranslation) < 2.0f * b2_linearSlop)
  302. {
  303. C = translation;
  304. }
  305. else if (translation <= m_lowerTranslation)
  306. {
  307. C = b2Min(translation - m_lowerTranslation, 0.0f);
  308. }
  309. else if (translation >= m_upperTranslation)
  310. {
  311. C = b2Max(translation - m_upperTranslation, 0.0f);
  312. }
  313. if (C != 0.0f)
  314. {
  315. float invMass = m_invMassA + m_invMassB + m_invIA * sAx * sAx + m_invIB * sBx * sBx;
  316. float impulse = 0.0f;
  317. if (invMass != 0.0f)
  318. {
  319. impulse = -C / invMass;
  320. }
  321. b2Vec2 P = impulse * ax;
  322. float LA = impulse * sAx;
  323. float LB = impulse * sBx;
  324. cA -= m_invMassA * P;
  325. aA -= m_invIA * LA;
  326. cB += m_invMassB * P;
  327. aB += m_invIB * LB;
  328. linearError = b2Abs(C);
  329. }
  330. }
  331. // Solve perpendicular constraint
  332. {
  333. b2Rot qA(aA), qB(aB);
  334. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  335. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  336. b2Vec2 d = (cB - cA) + rB - rA;
  337. b2Vec2 ay = b2Mul(qA, m_localYAxisA);
  338. float sAy = b2Cross(d + rA, ay);
  339. float sBy = b2Cross(rB, ay);
  340. float C = b2Dot(d, ay);
  341. float invMass = m_invMassA + m_invMassB + m_invIA * m_sAy * m_sAy + m_invIB * m_sBy * m_sBy;
  342. float impulse = 0.0f;
  343. if (invMass != 0.0f)
  344. {
  345. impulse = - C / invMass;
  346. }
  347. b2Vec2 P = impulse * ay;
  348. float LA = impulse * sAy;
  349. float LB = impulse * sBy;
  350. cA -= m_invMassA * P;
  351. aA -= m_invIA * LA;
  352. cB += m_invMassB * P;
  353. aB += m_invIB * LB;
  354. linearError = b2Max(linearError, b2Abs(C));
  355. }
  356. data.positions[m_indexA].c = cA;
  357. data.positions[m_indexA].a = aA;
  358. data.positions[m_indexB].c = cB;
  359. data.positions[m_indexB].a = aB;
  360. return linearError <= b2_linearSlop;
  361. }
  362. b2Vec2 b2WheelJoint::GetAnchorA() const
  363. {
  364. return m_bodyA->GetWorldPoint(m_localAnchorA);
  365. }
  366. b2Vec2 b2WheelJoint::GetAnchorB() const
  367. {
  368. return m_bodyB->GetWorldPoint(m_localAnchorB);
  369. }
  370. b2Vec2 b2WheelJoint::GetReactionForce(float inv_dt) const
  371. {
  372. return inv_dt * (m_impulse * m_ay + (m_springImpulse + m_lowerImpulse - m_upperImpulse) * m_ax);
  373. }
  374. float b2WheelJoint::GetReactionTorque(float inv_dt) const
  375. {
  376. return inv_dt * m_motorImpulse;
  377. }
  378. float b2WheelJoint::GetJointTranslation() const
  379. {
  380. b2Body* bA = m_bodyA;
  381. b2Body* bB = m_bodyB;
  382. b2Vec2 pA = bA->GetWorldPoint(m_localAnchorA);
  383. b2Vec2 pB = bB->GetWorldPoint(m_localAnchorB);
  384. b2Vec2 d = pB - pA;
  385. b2Vec2 axis = bA->GetWorldVector(m_localXAxisA);
  386. float translation = b2Dot(d, axis);
  387. return translation;
  388. }
  389. float b2WheelJoint::GetJointLinearSpeed() const
  390. {
  391. b2Body* bA = m_bodyA;
  392. b2Body* bB = m_bodyB;
  393. b2Vec2 rA = b2Mul(bA->m_xf.q, m_localAnchorA - bA->m_sweep.localCenter);
  394. b2Vec2 rB = b2Mul(bB->m_xf.q, m_localAnchorB - bB->m_sweep.localCenter);
  395. b2Vec2 p1 = bA->m_sweep.c + rA;
  396. b2Vec2 p2 = bB->m_sweep.c + rB;
  397. b2Vec2 d = p2 - p1;
  398. b2Vec2 axis = b2Mul(bA->m_xf.q, m_localXAxisA);
  399. b2Vec2 vA = bA->m_linearVelocity;
  400. b2Vec2 vB = bB->m_linearVelocity;
  401. float wA = bA->m_angularVelocity;
  402. float wB = bB->m_angularVelocity;
  403. float speed = b2Dot(d, b2Cross(wA, axis)) + b2Dot(axis, vB + b2Cross(wB, rB) - vA - b2Cross(wA, rA));
  404. return speed;
  405. }
  406. float b2WheelJoint::GetJointAngle() const
  407. {
  408. b2Body* bA = m_bodyA;
  409. b2Body* bB = m_bodyB;
  410. return bB->m_sweep.a - bA->m_sweep.a;
  411. }
  412. float b2WheelJoint::GetJointAngularSpeed() const
  413. {
  414. float wA = m_bodyA->m_angularVelocity;
  415. float wB = m_bodyB->m_angularVelocity;
  416. return wB - wA;
  417. }
  418. bool b2WheelJoint::IsLimitEnabled() const
  419. {
  420. return m_enableLimit;
  421. }
  422. void b2WheelJoint::EnableLimit(bool flag)
  423. {
  424. if (flag != m_enableLimit)
  425. {
  426. m_bodyA->SetAwake(true);
  427. m_bodyB->SetAwake(true);
  428. m_enableLimit = flag;
  429. m_lowerImpulse = 0.0f;
  430. m_upperImpulse = 0.0f;
  431. }
  432. }
  433. float b2WheelJoint::GetLowerLimit() const
  434. {
  435. return m_lowerTranslation;
  436. }
  437. float b2WheelJoint::GetUpperLimit() const
  438. {
  439. return m_upperTranslation;
  440. }
  441. void b2WheelJoint::SetLimits(float lower, float upper)
  442. {
  443. b2Assert(lower <= upper);
  444. if (lower != m_lowerTranslation || upper != m_upperTranslation)
  445. {
  446. m_bodyA->SetAwake(true);
  447. m_bodyB->SetAwake(true);
  448. m_lowerTranslation = lower;
  449. m_upperTranslation = upper;
  450. m_lowerImpulse = 0.0f;
  451. m_upperImpulse = 0.0f;
  452. }
  453. }
  454. bool b2WheelJoint::IsMotorEnabled() const
  455. {
  456. return m_enableMotor;
  457. }
  458. void b2WheelJoint::EnableMotor(bool flag)
  459. {
  460. if (flag != m_enableMotor)
  461. {
  462. m_bodyA->SetAwake(true);
  463. m_bodyB->SetAwake(true);
  464. m_enableMotor = flag;
  465. }
  466. }
  467. void b2WheelJoint::SetMotorSpeed(float speed)
  468. {
  469. if (speed != m_motorSpeed)
  470. {
  471. m_bodyA->SetAwake(true);
  472. m_bodyB->SetAwake(true);
  473. m_motorSpeed = speed;
  474. }
  475. }
  476. void b2WheelJoint::SetMaxMotorTorque(float torque)
  477. {
  478. if (torque != m_maxMotorTorque)
  479. {
  480. m_bodyA->SetAwake(true);
  481. m_bodyB->SetAwake(true);
  482. m_maxMotorTorque = torque;
  483. }
  484. }
  485. float b2WheelJoint::GetMotorTorque(float inv_dt) const
  486. {
  487. return inv_dt * m_motorImpulse;
  488. }
  489. void b2WheelJoint::SetStiffness(float stiffness)
  490. {
  491. m_stiffness = stiffness;
  492. }
  493. float b2WheelJoint::GetStiffness() const
  494. {
  495. return m_stiffness;
  496. }
  497. void b2WheelJoint::SetDamping(float damping)
  498. {
  499. m_damping = damping;
  500. }
  501. float b2WheelJoint::GetDamping() const
  502. {
  503. return m_damping;
  504. }
  505. void b2WheelJoint::Dump()
  506. {
  507. // FLT_DECIMAL_DIG == 9
  508. int32 indexA = m_bodyA->m_islandIndex;
  509. int32 indexB = m_bodyB->m_islandIndex;
  510. b2Dump(" b2WheelJointDef jd;\n");
  511. b2Dump(" jd.bodyA = bodies[%d];\n", indexA);
  512. b2Dump(" jd.bodyB = bodies[%d];\n", indexB);
  513. b2Dump(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  514. b2Dump(" jd.localAnchorA.Set(%.9g, %.9g);\n", m_localAnchorA.x, m_localAnchorA.y);
  515. b2Dump(" jd.localAnchorB.Set(%.9g, %.9g);\n", m_localAnchorB.x, m_localAnchorB.y);
  516. b2Dump(" jd.localAxisA.Set(%.9g, %.9g);\n", m_localXAxisA.x, m_localXAxisA.y);
  517. b2Dump(" jd.enableMotor = bool(%d);\n", m_enableMotor);
  518. b2Dump(" jd.motorSpeed = %.9g;\n", m_motorSpeed);
  519. b2Dump(" jd.maxMotorTorque = %.9g;\n", m_maxMotorTorque);
  520. b2Dump(" jd.stiffness = %.9g;\n", m_stiffness);
  521. b2Dump(" jd.damping = %.9g;\n", m_damping);
  522. b2Dump(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  523. }
  524. ///
  525. void b2WheelJoint::Draw(b2Draw* draw) const
  526. {
  527. const b2Transform& xfA = m_bodyA->GetTransform();
  528. const b2Transform& xfB = m_bodyB->GetTransform();
  529. b2Vec2 pA = b2Mul(xfA, m_localAnchorA);
  530. b2Vec2 pB = b2Mul(xfB, m_localAnchorB);
  531. b2Vec2 axis = b2Mul(xfA.q, m_localXAxisA);
  532. b2Color c1(0.7f, 0.7f, 0.7f);
  533. b2Color c2(0.3f, 0.9f, 0.3f);
  534. b2Color c3(0.9f, 0.3f, 0.3f);
  535. b2Color c4(0.3f, 0.3f, 0.9f);
  536. b2Color c5(0.4f, 0.4f, 0.4f);
  537. draw->DrawSegment(pA, pB, c5);
  538. if (m_enableLimit)
  539. {
  540. b2Vec2 lower = pA + m_lowerTranslation * axis;
  541. b2Vec2 upper = pA + m_upperTranslation * axis;
  542. b2Vec2 perp = b2Mul(xfA.q, m_localYAxisA);
  543. draw->DrawSegment(lower, upper, c1);
  544. draw->DrawSegment(lower - 0.5f * perp, lower + 0.5f * perp, c2);
  545. draw->DrawSegment(upper - 0.5f * perp, upper + 0.5f * perp, c3);
  546. }
  547. else
  548. {
  549. draw->DrawSegment(pA - 1.0f * axis, pA + 1.0f * axis, c1);
  550. }
  551. draw->DrawPoint(pA, 5.0f, c1);
  552. draw->DrawPoint(pB, 5.0f, c4);
  553. }