b2WheelJoint.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2006-2007 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/b2WheelJoint.h>
  19. #include <Box2D/Dynamics/b2Body.h>
  20. #include <Box2D/Dynamics/b2TimeStep.h>
  21. // Linear constraint (point-to-line)
  22. // d = pB - pA = xB + rB - xA - rA
  23. // C = dot(ay, d)
  24. // Cdot = dot(d, cross(wA, ay)) + dot(ay, vB + cross(wB, rB) - vA - cross(wA, rA))
  25. // = -dot(ay, vA) - dot(cross(d + rA, ay), wA) + dot(ay, vB) + dot(cross(rB, ay), vB)
  26. // J = [-ay, -cross(d + rA, ay), ay, cross(rB, ay)]
  27. // Spring linear constraint
  28. // C = dot(ax, d)
  29. // Cdot = = -dot(ax, vA) - dot(cross(d + rA, ax), wA) + dot(ax, vB) + dot(cross(rB, ax), vB)
  30. // J = [-ax -cross(d+rA, ax) ax cross(rB, ax)]
  31. // Motor rotational constraint
  32. // Cdot = wB - wA
  33. // J = [0 0 -1 0 0 1]
  34. void b2WheelJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor, const b2Vec2& axis)
  35. {
  36. bodyA = bA;
  37. bodyB = bB;
  38. localAnchorA = bodyA->GetLocalPoint(anchor);
  39. localAnchorB = bodyB->GetLocalPoint(anchor);
  40. localAxisA = bodyA->GetLocalVector(axis);
  41. }
  42. b2WheelJoint::b2WheelJoint(const b2WheelJointDef* def)
  43. : b2Joint(def)
  44. {
  45. m_localAnchorA = def->localAnchorA;
  46. m_localAnchorB = def->localAnchorB;
  47. m_localXAxisA = def->localAxisA;
  48. m_localYAxisA = b2Cross(1.0f, m_localXAxisA);
  49. m_mass = 0.0f;
  50. m_impulse = 0.0f;
  51. m_motorMass = 0.0f;
  52. m_motorImpulse = 0.0f;
  53. m_springMass = 0.0f;
  54. m_springImpulse = 0.0f;
  55. m_maxMotorTorque = def->maxMotorTorque;
  56. m_motorSpeed = def->motorSpeed;
  57. m_enableMotor = def->enableMotor;
  58. m_frequencyHz = def->frequencyHz;
  59. m_dampingRatio = def->dampingRatio;
  60. m_bias = 0.0f;
  61. m_gamma = 0.0f;
  62. m_ax.SetZero();
  63. m_ay.SetZero();
  64. }
  65. void b2WheelJoint::InitVelocityConstraints(const b2SolverData& data)
  66. {
  67. m_indexA = m_bodyA->m_islandIndex;
  68. m_indexB = m_bodyB->m_islandIndex;
  69. m_localCenterA = m_bodyA->m_sweep.localCenter;
  70. m_localCenterB = m_bodyB->m_sweep.localCenter;
  71. m_invMassA = m_bodyA->m_invMass;
  72. m_invMassB = m_bodyB->m_invMass;
  73. m_invIA = m_bodyA->m_invI;
  74. m_invIB = m_bodyB->m_invI;
  75. float32 mA = m_invMassA, mB = m_invMassB;
  76. float32 iA = m_invIA, iB = m_invIB;
  77. b2Vec2 cA = data.positions[m_indexA].c;
  78. float32 aA = data.positions[m_indexA].a;
  79. b2Vec2 vA = data.velocities[m_indexA].v;
  80. float32 wA = data.velocities[m_indexA].w;
  81. b2Vec2 cB = data.positions[m_indexB].c;
  82. float32 aB = data.positions[m_indexB].a;
  83. b2Vec2 vB = data.velocities[m_indexB].v;
  84. float32 wB = data.velocities[m_indexB].w;
  85. b2Rot qA(aA), qB(aB);
  86. // Compute the effective masses.
  87. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  88. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  89. b2Vec2 d = cB + rB - cA - rA;
  90. // Point to line constraint
  91. {
  92. m_ay = b2Mul(qA, m_localYAxisA);
  93. m_sAy = b2Cross(d + rA, m_ay);
  94. m_sBy = b2Cross(rB, m_ay);
  95. m_mass = mA + mB + iA * m_sAy * m_sAy + iB * m_sBy * m_sBy;
  96. if (m_mass > 0.0f)
  97. {
  98. m_mass = 1.0f / m_mass;
  99. }
  100. }
  101. // Spring constraint
  102. m_springMass = 0.0f;
  103. m_bias = 0.0f;
  104. m_gamma = 0.0f;
  105. if (m_frequencyHz > 0.0f)
  106. {
  107. m_ax = b2Mul(qA, m_localXAxisA);
  108. m_sAx = b2Cross(d + rA, m_ax);
  109. m_sBx = b2Cross(rB, m_ax);
  110. float32 invMass = mA + mB + iA * m_sAx * m_sAx + iB * m_sBx * m_sBx;
  111. if (invMass > 0.0f)
  112. {
  113. m_springMass = 1.0f / invMass;
  114. float32 C = b2Dot(d, m_ax);
  115. // Frequency
  116. float32 omega = 2.0f * b2_pi * m_frequencyHz;
  117. // Damping coefficient
  118. float32 d = 2.0f * m_springMass * m_dampingRatio * omega;
  119. // Spring stiffness
  120. float32 k = m_springMass * omega * omega;
  121. // magic formulas
  122. float32 h = data.step.dt;
  123. m_gamma = h * (d + h * k);
  124. if (m_gamma > 0.0f)
  125. {
  126. m_gamma = 1.0f / m_gamma;
  127. }
  128. m_bias = C * h * k * m_gamma;
  129. m_springMass = invMass + m_gamma;
  130. if (m_springMass > 0.0f)
  131. {
  132. m_springMass = 1.0f / m_springMass;
  133. }
  134. }
  135. }
  136. else
  137. {
  138. m_springImpulse = 0.0f;
  139. }
  140. // Rotational motor
  141. if (m_enableMotor)
  142. {
  143. m_motorMass = iA + iB;
  144. if (m_motorMass > 0.0f)
  145. {
  146. m_motorMass = 1.0f / m_motorMass;
  147. }
  148. }
  149. else
  150. {
  151. m_motorMass = 0.0f;
  152. m_motorImpulse = 0.0f;
  153. }
  154. if (data.step.warmStarting)
  155. {
  156. // Account for variable time step.
  157. m_impulse *= data.step.dtRatio;
  158. m_springImpulse *= data.step.dtRatio;
  159. m_motorImpulse *= data.step.dtRatio;
  160. b2Vec2 P = m_impulse * m_ay + m_springImpulse * m_ax;
  161. float32 LA = m_impulse * m_sAy + m_springImpulse * m_sAx + m_motorImpulse;
  162. float32 LB = m_impulse * m_sBy + m_springImpulse * m_sBx + m_motorImpulse;
  163. vA -= m_invMassA * P;
  164. wA -= m_invIA * LA;
  165. vB += m_invMassB * P;
  166. wB += m_invIB * LB;
  167. }
  168. else
  169. {
  170. m_impulse = 0.0f;
  171. m_springImpulse = 0.0f;
  172. m_motorImpulse = 0.0f;
  173. }
  174. data.velocities[m_indexA].v = vA;
  175. data.velocities[m_indexA].w = wA;
  176. data.velocities[m_indexB].v = vB;
  177. data.velocities[m_indexB].w = wB;
  178. }
  179. void b2WheelJoint::SolveVelocityConstraints(const b2SolverData& data)
  180. {
  181. float32 mA = m_invMassA, mB = m_invMassB;
  182. float32 iA = m_invIA, iB = m_invIB;
  183. b2Vec2 vA = data.velocities[m_indexA].v;
  184. float32 wA = data.velocities[m_indexA].w;
  185. b2Vec2 vB = data.velocities[m_indexB].v;
  186. float32 wB = data.velocities[m_indexB].w;
  187. // Solve spring constraint
  188. {
  189. float32 Cdot = b2Dot(m_ax, vB - vA) + m_sBx * wB - m_sAx * wA;
  190. float32 impulse = -m_springMass * (Cdot + m_bias + m_gamma * m_springImpulse);
  191. m_springImpulse += impulse;
  192. b2Vec2 P = impulse * m_ax;
  193. float32 LA = impulse * m_sAx;
  194. float32 LB = impulse * m_sBx;
  195. vA -= mA * P;
  196. wA -= iA * LA;
  197. vB += mB * P;
  198. wB += iB * LB;
  199. }
  200. // Solve rotational motor constraint
  201. {
  202. float32 Cdot = wB - wA - m_motorSpeed;
  203. float32 impulse = -m_motorMass * Cdot;
  204. float32 oldImpulse = m_motorImpulse;
  205. float32 maxImpulse = data.step.dt * m_maxMotorTorque;
  206. m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
  207. impulse = m_motorImpulse - oldImpulse;
  208. wA -= iA * impulse;
  209. wB += iB * impulse;
  210. }
  211. // Solve point to line constraint
  212. {
  213. float32 Cdot = b2Dot(m_ay, vB - vA) + m_sBy * wB - m_sAy * wA;
  214. float32 impulse = -m_mass * Cdot;
  215. m_impulse += impulse;
  216. b2Vec2 P = impulse * m_ay;
  217. float32 LA = impulse * m_sAy;
  218. float32 LB = impulse * m_sBy;
  219. vA -= mA * P;
  220. wA -= iA * LA;
  221. vB += mB * P;
  222. wB += iB * LB;
  223. }
  224. data.velocities[m_indexA].v = vA;
  225. data.velocities[m_indexA].w = wA;
  226. data.velocities[m_indexB].v = vB;
  227. data.velocities[m_indexB].w = wB;
  228. }
  229. bool b2WheelJoint::SolvePositionConstraints(const b2SolverData& data)
  230. {
  231. b2Vec2 cA = data.positions[m_indexA].c;
  232. float32 aA = data.positions[m_indexA].a;
  233. b2Vec2 cB = data.positions[m_indexB].c;
  234. float32 aB = data.positions[m_indexB].a;
  235. b2Rot qA(aA), qB(aB);
  236. b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  237. b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
  238. b2Vec2 d = (cB - cA) + rB - rA;
  239. b2Vec2 ay = b2Mul(qA, m_localYAxisA);
  240. float32 sAy = b2Cross(d + rA, ay);
  241. float32 sBy = b2Cross(rB, ay);
  242. float32 C = b2Dot(d, ay);
  243. float32 k = m_invMassA + m_invMassB + m_invIA * m_sAy * m_sAy + m_invIB * m_sBy * m_sBy;
  244. float32 impulse;
  245. if (k != 0.0f)
  246. {
  247. impulse = - C / k;
  248. }
  249. else
  250. {
  251. impulse = 0.0f;
  252. }
  253. b2Vec2 P = impulse * ay;
  254. float32 LA = impulse * sAy;
  255. float32 LB = impulse * sBy;
  256. cA -= m_invMassA * P;
  257. aA -= m_invIA * LA;
  258. cB += m_invMassB * P;
  259. aB += m_invIB * LB;
  260. data.positions[m_indexA].c = cA;
  261. data.positions[m_indexA].a = aA;
  262. data.positions[m_indexB].c = cB;
  263. data.positions[m_indexB].a = aB;
  264. return b2Abs(C) <= b2_linearSlop;
  265. }
  266. b2Vec2 b2WheelJoint::GetAnchorA() const
  267. {
  268. return m_bodyA->GetWorldPoint(m_localAnchorA);
  269. }
  270. b2Vec2 b2WheelJoint::GetAnchorB() const
  271. {
  272. return m_bodyB->GetWorldPoint(m_localAnchorB);
  273. }
  274. b2Vec2 b2WheelJoint::GetReactionForce(float32 inv_dt) const
  275. {
  276. return inv_dt * (m_impulse * m_ay + m_springImpulse * m_ax);
  277. }
  278. float32 b2WheelJoint::GetReactionTorque(float32 inv_dt) const
  279. {
  280. return inv_dt * m_motorImpulse;
  281. }
  282. float32 b2WheelJoint::GetJointTranslation() const
  283. {
  284. b2Body* bA = m_bodyA;
  285. b2Body* bB = m_bodyB;
  286. b2Vec2 pA = bA->GetWorldPoint(m_localAnchorA);
  287. b2Vec2 pB = bB->GetWorldPoint(m_localAnchorB);
  288. b2Vec2 d = pB - pA;
  289. b2Vec2 axis = bA->GetWorldVector(m_localXAxisA);
  290. float32 translation = b2Dot(d, axis);
  291. return translation;
  292. }
  293. float32 b2WheelJoint::GetJointSpeed() const
  294. {
  295. float32 wA = m_bodyA->m_angularVelocity;
  296. float32 wB = m_bodyB->m_angularVelocity;
  297. return wB - wA;
  298. }
  299. bool b2WheelJoint::IsMotorEnabled() const
  300. {
  301. return m_enableMotor;
  302. }
  303. void b2WheelJoint::EnableMotor(bool flag)
  304. {
  305. m_bodyA->SetAwake(true);
  306. m_bodyB->SetAwake(true);
  307. m_enableMotor = flag;
  308. }
  309. void b2WheelJoint::SetMotorSpeed(float32 speed)
  310. {
  311. m_bodyA->SetAwake(true);
  312. m_bodyB->SetAwake(true);
  313. m_motorSpeed = speed;
  314. }
  315. void b2WheelJoint::SetMaxMotorTorque(float32 torque)
  316. {
  317. m_bodyA->SetAwake(true);
  318. m_bodyB->SetAwake(true);
  319. m_maxMotorTorque = torque;
  320. }
  321. float32 b2WheelJoint::GetMotorTorque(float32 inv_dt) const
  322. {
  323. return inv_dt * m_motorImpulse;
  324. }
  325. void b2WheelJoint::Dump()
  326. {
  327. int32 indexA = m_bodyA->m_islandIndex;
  328. int32 indexB = m_bodyB->m_islandIndex;
  329. b2Log(" b2WheelJointDef jd;\n");
  330. b2Log(" jd.bodyA = bodies[%d];\n", indexA);
  331. b2Log(" jd.bodyB = bodies[%d];\n", indexB);
  332. b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
  333. b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
  334. b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
  335. b2Log(" jd.localAxisA.Set(%.15lef, %.15lef);\n", m_localXAxisA.x, m_localXAxisA.y);
  336. b2Log(" jd.enableMotor = bool(%d);\n", m_enableMotor);
  337. b2Log(" jd.motorSpeed = %.15lef;\n", m_motorSpeed);
  338. b2Log(" jd.maxMotorTorque = %.15lef;\n", m_maxMotorTorque);
  339. b2Log(" jd.frequencyHz = %.15lef;\n", m_frequencyHz);
  340. b2Log(" jd.dampingRatio = %.15lef;\n", m_dampingRatio);
  341. b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
  342. }