b2ContactSolver.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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/Contacts/b2ContactSolver.h>
  19. #include <Box2D/Dynamics/Contacts/b2Contact.h>
  20. #include <Box2D/Dynamics/b2Body.h>
  21. #include <Box2D/Dynamics/b2Fixture.h>
  22. #include <Box2D/Dynamics/b2World.h>
  23. #include <Box2D/Common/b2StackAllocator.h>
  24. #define B2_DEBUG_SOLVER 0
  25. struct b2ContactPositionConstraint
  26. {
  27. b2Vec2 localPoints[b2_maxManifoldPoints];
  28. b2Vec2 localNormal;
  29. b2Vec2 localPoint;
  30. int32 indexA;
  31. int32 indexB;
  32. float32 invMassA, invMassB;
  33. b2Vec2 localCenterA, localCenterB;
  34. float32 invIA, invIB;
  35. b2Manifold::Type type;
  36. float32 radiusA, radiusB;
  37. int32 pointCount;
  38. };
  39. b2ContactSolver::b2ContactSolver(b2ContactSolverDef* def)
  40. {
  41. m_step = def->step;
  42. m_allocator = def->allocator;
  43. m_count = def->count;
  44. m_positionConstraints = (b2ContactPositionConstraint*)m_allocator->Allocate(m_count * sizeof(b2ContactPositionConstraint));
  45. m_velocityConstraints = (b2ContactVelocityConstraint*)m_allocator->Allocate(m_count * sizeof(b2ContactVelocityConstraint));
  46. m_positions = def->positions;
  47. m_velocities = def->velocities;
  48. m_contacts = def->contacts;
  49. // Initialize position independent portions of the constraints.
  50. for (int32 i = 0; i < m_count; ++i)
  51. {
  52. b2Contact* contact = m_contacts[i];
  53. b2Fixture* fixtureA = contact->m_fixtureA;
  54. b2Fixture* fixtureB = contact->m_fixtureB;
  55. b2Shape* shapeA = fixtureA->GetShape();
  56. b2Shape* shapeB = fixtureB->GetShape();
  57. float32 radiusA = shapeA->m_radius;
  58. float32 radiusB = shapeB->m_radius;
  59. b2Body* bodyA = fixtureA->GetBody();
  60. b2Body* bodyB = fixtureB->GetBody();
  61. b2Manifold* manifold = contact->GetManifold();
  62. int32 pointCount = manifold->pointCount;
  63. b2Assert(pointCount > 0);
  64. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  65. vc->friction = contact->m_friction;
  66. vc->restitution = contact->m_restitution;
  67. vc->tangentSpeed = contact->m_tangentSpeed;
  68. vc->indexA = bodyA->m_islandIndex;
  69. vc->indexB = bodyB->m_islandIndex;
  70. vc->invMassA = bodyA->m_invMass;
  71. vc->invMassB = bodyB->m_invMass;
  72. vc->invIA = bodyA->m_invI;
  73. vc->invIB = bodyB->m_invI;
  74. vc->contactIndex = i;
  75. vc->pointCount = pointCount;
  76. vc->K.SetZero();
  77. vc->normalMass.SetZero();
  78. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  79. pc->indexA = bodyA->m_islandIndex;
  80. pc->indexB = bodyB->m_islandIndex;
  81. pc->invMassA = bodyA->m_invMass;
  82. pc->invMassB = bodyB->m_invMass;
  83. pc->localCenterA = bodyA->m_sweep.localCenter;
  84. pc->localCenterB = bodyB->m_sweep.localCenter;
  85. pc->invIA = bodyA->m_invI;
  86. pc->invIB = bodyB->m_invI;
  87. pc->localNormal = manifold->localNormal;
  88. pc->localPoint = manifold->localPoint;
  89. pc->pointCount = pointCount;
  90. pc->radiusA = radiusA;
  91. pc->radiusB = radiusB;
  92. pc->type = manifold->type;
  93. for (int32 j = 0; j < pointCount; ++j)
  94. {
  95. b2ManifoldPoint* cp = manifold->points + j;
  96. b2VelocityConstraintPoint* vcp = vc->points + j;
  97. if (m_step.warmStarting)
  98. {
  99. vcp->normalImpulse = m_step.dtRatio * cp->normalImpulse;
  100. vcp->tangentImpulse = m_step.dtRatio * cp->tangentImpulse;
  101. }
  102. else
  103. {
  104. vcp->normalImpulse = 0.0f;
  105. vcp->tangentImpulse = 0.0f;
  106. }
  107. vcp->rA.SetZero();
  108. vcp->rB.SetZero();
  109. vcp->normalMass = 0.0f;
  110. vcp->tangentMass = 0.0f;
  111. vcp->velocityBias = 0.0f;
  112. pc->localPoints[j] = cp->localPoint;
  113. }
  114. }
  115. }
  116. b2ContactSolver::~b2ContactSolver()
  117. {
  118. m_allocator->Free(m_velocityConstraints);
  119. m_allocator->Free(m_positionConstraints);
  120. }
  121. // Initialize position dependent portions of the velocity constraints.
  122. void b2ContactSolver::InitializeVelocityConstraints()
  123. {
  124. for (int32 i = 0; i < m_count; ++i)
  125. {
  126. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  127. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  128. float32 radiusA = pc->radiusA;
  129. float32 radiusB = pc->radiusB;
  130. b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
  131. int32 indexA = vc->indexA;
  132. int32 indexB = vc->indexB;
  133. float32 mA = vc->invMassA;
  134. float32 mB = vc->invMassB;
  135. float32 iA = vc->invIA;
  136. float32 iB = vc->invIB;
  137. b2Vec2 localCenterA = pc->localCenterA;
  138. b2Vec2 localCenterB = pc->localCenterB;
  139. b2Vec2 cA = m_positions[indexA].c;
  140. float32 aA = m_positions[indexA].a;
  141. b2Vec2 vA = m_velocities[indexA].v;
  142. float32 wA = m_velocities[indexA].w;
  143. b2Vec2 cB = m_positions[indexB].c;
  144. float32 aB = m_positions[indexB].a;
  145. b2Vec2 vB = m_velocities[indexB].v;
  146. float32 wB = m_velocities[indexB].w;
  147. b2Assert(manifold->pointCount > 0);
  148. b2Transform xfA, xfB;
  149. xfA.q.Set(aA);
  150. xfB.q.Set(aB);
  151. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  152. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  153. b2WorldManifold worldManifold;
  154. worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB);
  155. vc->normal = worldManifold.normal;
  156. int32 pointCount = vc->pointCount;
  157. for (int32 j = 0; j < pointCount; ++j)
  158. {
  159. b2VelocityConstraintPoint* vcp = vc->points + j;
  160. vcp->rA = worldManifold.points[j] - cA;
  161. vcp->rB = worldManifold.points[j] - cB;
  162. float32 rnA = b2Cross(vcp->rA, vc->normal);
  163. float32 rnB = b2Cross(vcp->rB, vc->normal);
  164. float32 kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  165. vcp->normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f;
  166. b2Vec2 tangent = b2Cross(vc->normal, 1.0f);
  167. float32 rtA = b2Cross(vcp->rA, tangent);
  168. float32 rtB = b2Cross(vcp->rB, tangent);
  169. float32 kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB;
  170. vcp->tangentMass = kTangent > 0.0f ? 1.0f / kTangent : 0.0f;
  171. // Setup a velocity bias for restitution.
  172. vcp->velocityBias = 0.0f;
  173. float32 vRel = b2Dot(vc->normal, vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA));
  174. if (vRel < -b2_velocityThreshold)
  175. {
  176. vcp->velocityBias = -vc->restitution * vRel;
  177. }
  178. }
  179. // If we have two points, then prepare the block solver.
  180. if (vc->pointCount == 2)
  181. {
  182. b2VelocityConstraintPoint* vcp1 = vc->points + 0;
  183. b2VelocityConstraintPoint* vcp2 = vc->points + 1;
  184. float32 rn1A = b2Cross(vcp1->rA, vc->normal);
  185. float32 rn1B = b2Cross(vcp1->rB, vc->normal);
  186. float32 rn2A = b2Cross(vcp2->rA, vc->normal);
  187. float32 rn2B = b2Cross(vcp2->rB, vc->normal);
  188. float32 k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
  189. float32 k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
  190. float32 k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B;
  191. // Ensure a reasonable condition number.
  192. const float32 k_maxConditionNumber = 1000.0f;
  193. if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
  194. {
  195. // K is safe to invert.
  196. vc->K.ex.Set(k11, k12);
  197. vc->K.ey.Set(k12, k22);
  198. vc->normalMass = vc->K.GetInverse();
  199. }
  200. else
  201. {
  202. // The constraints are redundant, just use one.
  203. // TODO_ERIN use deepest?
  204. vc->pointCount = 1;
  205. }
  206. }
  207. }
  208. }
  209. void b2ContactSolver::WarmStart()
  210. {
  211. // Warm start.
  212. for (int32 i = 0; i < m_count; ++i)
  213. {
  214. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  215. int32 indexA = vc->indexA;
  216. int32 indexB = vc->indexB;
  217. float32 mA = vc->invMassA;
  218. float32 iA = vc->invIA;
  219. float32 mB = vc->invMassB;
  220. float32 iB = vc->invIB;
  221. int32 pointCount = vc->pointCount;
  222. b2Vec2 vA = m_velocities[indexA].v;
  223. float32 wA = m_velocities[indexA].w;
  224. b2Vec2 vB = m_velocities[indexB].v;
  225. float32 wB = m_velocities[indexB].w;
  226. b2Vec2 normal = vc->normal;
  227. b2Vec2 tangent = b2Cross(normal, 1.0f);
  228. for (int32 j = 0; j < pointCount; ++j)
  229. {
  230. b2VelocityConstraintPoint* vcp = vc->points + j;
  231. b2Vec2 P = vcp->normalImpulse * normal + vcp->tangentImpulse * tangent;
  232. wA -= iA * b2Cross(vcp->rA, P);
  233. vA -= mA * P;
  234. wB += iB * b2Cross(vcp->rB, P);
  235. vB += mB * P;
  236. }
  237. m_velocities[indexA].v = vA;
  238. m_velocities[indexA].w = wA;
  239. m_velocities[indexB].v = vB;
  240. m_velocities[indexB].w = wB;
  241. }
  242. }
  243. void b2ContactSolver::SolveVelocityConstraints()
  244. {
  245. for (int32 i = 0; i < m_count; ++i)
  246. {
  247. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  248. int32 indexA = vc->indexA;
  249. int32 indexB = vc->indexB;
  250. float32 mA = vc->invMassA;
  251. float32 iA = vc->invIA;
  252. float32 mB = vc->invMassB;
  253. float32 iB = vc->invIB;
  254. int32 pointCount = vc->pointCount;
  255. b2Vec2 vA = m_velocities[indexA].v;
  256. float32 wA = m_velocities[indexA].w;
  257. b2Vec2 vB = m_velocities[indexB].v;
  258. float32 wB = m_velocities[indexB].w;
  259. b2Vec2 normal = vc->normal;
  260. b2Vec2 tangent = b2Cross(normal, 1.0f);
  261. float32 friction = vc->friction;
  262. b2Assert(pointCount == 1 || pointCount == 2);
  263. // Solve tangent constraints first because non-penetration is more important
  264. // than friction.
  265. for (int32 j = 0; j < pointCount; ++j)
  266. {
  267. b2VelocityConstraintPoint* vcp = vc->points + j;
  268. // Relative velocity at contact
  269. b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
  270. // Compute tangent force
  271. float32 vt = b2Dot(dv, tangent) - vc->tangentSpeed;
  272. float32 lambda = vcp->tangentMass * (-vt);
  273. // b2Clamp the accumulated force
  274. float32 maxFriction = friction * vcp->normalImpulse;
  275. float32 newImpulse = b2Clamp(vcp->tangentImpulse + lambda, -maxFriction, maxFriction);
  276. lambda = newImpulse - vcp->tangentImpulse;
  277. vcp->tangentImpulse = newImpulse;
  278. // Apply contact impulse
  279. b2Vec2 P = lambda * tangent;
  280. vA -= mA * P;
  281. wA -= iA * b2Cross(vcp->rA, P);
  282. vB += mB * P;
  283. wB += iB * b2Cross(vcp->rB, P);
  284. }
  285. // Solve normal constraints
  286. if (vc->pointCount == 1)
  287. {
  288. b2VelocityConstraintPoint* vcp = vc->points + 0;
  289. // Relative velocity at contact
  290. b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
  291. // Compute normal impulse
  292. float32 vn = b2Dot(dv, normal);
  293. float32 lambda = -vcp->normalMass * (vn - vcp->velocityBias);
  294. // b2Clamp the accumulated impulse
  295. float32 newImpulse = b2Max(vcp->normalImpulse + lambda, 0.0f);
  296. lambda = newImpulse - vcp->normalImpulse;
  297. vcp->normalImpulse = newImpulse;
  298. // Apply contact impulse
  299. b2Vec2 P = lambda * normal;
  300. vA -= mA * P;
  301. wA -= iA * b2Cross(vcp->rA, P);
  302. vB += mB * P;
  303. wB += iB * b2Cross(vcp->rB, P);
  304. }
  305. else
  306. {
  307. // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
  308. // Build the mini LCP for this contact patch
  309. //
  310. // vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
  311. //
  312. // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
  313. // b = vn0 - velocityBias
  314. //
  315. // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
  316. // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
  317. // vn1 = 0 and vn2 = 0, x1 = 0 and x2 = 0, x1 = 0 and vn2 = 0, x2 = 0 and vn1 = 0 need to be tested. The first valid
  318. // solution that satisfies the problem is chosen.
  319. //
  320. // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
  321. // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
  322. //
  323. // Substitute:
  324. //
  325. // x = a + d
  326. //
  327. // a := old total impulse
  328. // x := new total impulse
  329. // d := incremental impulse
  330. //
  331. // For the current iteration we extend the formula for the incremental impulse
  332. // to compute the new total impulse:
  333. //
  334. // vn = A * d + b
  335. // = A * (x - a) + b
  336. // = A * x + b - A * a
  337. // = A * x + b'
  338. // b' = b - A * a;
  339. b2VelocityConstraintPoint* cp1 = vc->points + 0;
  340. b2VelocityConstraintPoint* cp2 = vc->points + 1;
  341. b2Vec2 a(cp1->normalImpulse, cp2->normalImpulse);
  342. b2Assert(a.x >= 0.0f && a.y >= 0.0f);
  343. // Relative velocity at contact
  344. b2Vec2 dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  345. b2Vec2 dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  346. // Compute normal velocity
  347. float32 vn1 = b2Dot(dv1, normal);
  348. float32 vn2 = b2Dot(dv2, normal);
  349. b2Vec2 b;
  350. b.x = vn1 - cp1->velocityBias;
  351. b.y = vn2 - cp2->velocityBias;
  352. // Compute b'
  353. b -= b2Mul(vc->K, a);
  354. const float32 k_errorTol = 1e-3f;
  355. B2_NOT_USED(k_errorTol);
  356. for (;;)
  357. {
  358. //
  359. // Case 1: vn = 0
  360. //
  361. // 0 = A * x + b'
  362. //
  363. // Solve for x:
  364. //
  365. // x = - inv(A) * b'
  366. //
  367. b2Vec2 x = - b2Mul(vc->normalMass, b);
  368. if (x.x >= 0.0f && x.y >= 0.0f)
  369. {
  370. // Get the incremental impulse
  371. b2Vec2 d = x - a;
  372. // Apply incremental impulse
  373. b2Vec2 P1 = d.x * normal;
  374. b2Vec2 P2 = d.y * normal;
  375. vA -= mA * (P1 + P2);
  376. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  377. vB += mB * (P1 + P2);
  378. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  379. // Accumulate
  380. cp1->normalImpulse = x.x;
  381. cp2->normalImpulse = x.y;
  382. #if B2_DEBUG_SOLVER == 1
  383. // Postconditions
  384. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  385. dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  386. // Compute normal velocity
  387. vn1 = b2Dot(dv1, normal);
  388. vn2 = b2Dot(dv2, normal);
  389. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  390. b2Assert(b2Abs(vn2 - cp2->velocityBias) < k_errorTol);
  391. #endif
  392. break;
  393. }
  394. //
  395. // Case 2: vn1 = 0 and x2 = 0
  396. //
  397. // 0 = a11 * x1 + a12 * 0 + b1'
  398. // vn2 = a21 * x1 + a22 * 0 + b2'
  399. //
  400. x.x = - cp1->normalMass * b.x;
  401. x.y = 0.0f;
  402. vn1 = 0.0f;
  403. vn2 = vc->K.ex.y * x.x + b.y;
  404. if (x.x >= 0.0f && vn2 >= 0.0f)
  405. {
  406. // Get the incremental impulse
  407. b2Vec2 d = x - a;
  408. // Apply incremental impulse
  409. b2Vec2 P1 = d.x * normal;
  410. b2Vec2 P2 = d.y * normal;
  411. vA -= mA * (P1 + P2);
  412. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  413. vB += mB * (P1 + P2);
  414. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  415. // Accumulate
  416. cp1->normalImpulse = x.x;
  417. cp2->normalImpulse = x.y;
  418. #if B2_DEBUG_SOLVER == 1
  419. // Postconditions
  420. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  421. // Compute normal velocity
  422. vn1 = b2Dot(dv1, normal);
  423. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  424. #endif
  425. break;
  426. }
  427. //
  428. // Case 3: vn2 = 0 and x1 = 0
  429. //
  430. // vn1 = a11 * 0 + a12 * x2 + b1'
  431. // 0 = a21 * 0 + a22 * x2 + b2'
  432. //
  433. x.x = 0.0f;
  434. x.y = - cp2->normalMass * b.y;
  435. vn1 = vc->K.ey.x * x.y + b.x;
  436. vn2 = 0.0f;
  437. if (x.y >= 0.0f && vn1 >= 0.0f)
  438. {
  439. // Resubstitute for the incremental impulse
  440. b2Vec2 d = x - a;
  441. // Apply incremental impulse
  442. b2Vec2 P1 = d.x * normal;
  443. b2Vec2 P2 = d.y * normal;
  444. vA -= mA * (P1 + P2);
  445. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  446. vB += mB * (P1 + P2);
  447. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  448. // Accumulate
  449. cp1->normalImpulse = x.x;
  450. cp2->normalImpulse = x.y;
  451. #if B2_DEBUG_SOLVER == 1
  452. // Postconditions
  453. dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  454. // Compute normal velocity
  455. vn2 = b2Dot(dv2, normal);
  456. b2Assert(b2Abs(vn2 - cp2->velocityBias) < k_errorTol);
  457. #endif
  458. break;
  459. }
  460. //
  461. // Case 4: x1 = 0 and x2 = 0
  462. //
  463. // vn1 = b1
  464. // vn2 = b2;
  465. x.x = 0.0f;
  466. x.y = 0.0f;
  467. vn1 = b.x;
  468. vn2 = b.y;
  469. if (vn1 >= 0.0f && vn2 >= 0.0f )
  470. {
  471. // Resubstitute for the incremental impulse
  472. b2Vec2 d = x - a;
  473. // Apply incremental impulse
  474. b2Vec2 P1 = d.x * normal;
  475. b2Vec2 P2 = d.y * normal;
  476. vA -= mA * (P1 + P2);
  477. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  478. vB += mB * (P1 + P2);
  479. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  480. // Accumulate
  481. cp1->normalImpulse = x.x;
  482. cp2->normalImpulse = x.y;
  483. break;
  484. }
  485. // No solution, give up. This is hit sometimes, but it doesn't seem to matter.
  486. break;
  487. }
  488. }
  489. m_velocities[indexA].v = vA;
  490. m_velocities[indexA].w = wA;
  491. m_velocities[indexB].v = vB;
  492. m_velocities[indexB].w = wB;
  493. }
  494. }
  495. void b2ContactSolver::StoreImpulses()
  496. {
  497. for (int32 i = 0; i < m_count; ++i)
  498. {
  499. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  500. b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
  501. for (int32 j = 0; j < vc->pointCount; ++j)
  502. {
  503. manifold->points[j].normalImpulse = vc->points[j].normalImpulse;
  504. manifold->points[j].tangentImpulse = vc->points[j].tangentImpulse;
  505. }
  506. }
  507. }
  508. struct b2PositionSolverManifold
  509. {
  510. void Initialize(b2ContactPositionConstraint* pc, const b2Transform& xfA, const b2Transform& xfB, int32 index)
  511. {
  512. b2Assert(pc->pointCount > 0);
  513. switch (pc->type)
  514. {
  515. case b2Manifold::e_circles:
  516. {
  517. b2Vec2 pointA = b2Mul(xfA, pc->localPoint);
  518. b2Vec2 pointB = b2Mul(xfB, pc->localPoints[0]);
  519. normal = pointB - pointA;
  520. normal.Normalize();
  521. point = 0.5f * (pointA + pointB);
  522. separation = b2Dot(pointB - pointA, normal) - pc->radiusA - pc->radiusB;
  523. }
  524. break;
  525. case b2Manifold::e_faceA:
  526. {
  527. normal = b2Mul(xfA.q, pc->localNormal);
  528. b2Vec2 planePoint = b2Mul(xfA, pc->localPoint);
  529. b2Vec2 clipPoint = b2Mul(xfB, pc->localPoints[index]);
  530. separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
  531. point = clipPoint;
  532. }
  533. break;
  534. case b2Manifold::e_faceB:
  535. {
  536. normal = b2Mul(xfB.q, pc->localNormal);
  537. b2Vec2 planePoint = b2Mul(xfB, pc->localPoint);
  538. b2Vec2 clipPoint = b2Mul(xfA, pc->localPoints[index]);
  539. separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
  540. point = clipPoint;
  541. // Ensure normal points from A to B
  542. normal = -normal;
  543. }
  544. break;
  545. }
  546. }
  547. b2Vec2 normal;
  548. b2Vec2 point;
  549. float32 separation;
  550. };
  551. // Sequential solver.
  552. bool b2ContactSolver::SolvePositionConstraints()
  553. {
  554. float32 minSeparation = 0.0f;
  555. for (int32 i = 0; i < m_count; ++i)
  556. {
  557. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  558. int32 indexA = pc->indexA;
  559. int32 indexB = pc->indexB;
  560. b2Vec2 localCenterA = pc->localCenterA;
  561. float32 mA = pc->invMassA;
  562. float32 iA = pc->invIA;
  563. b2Vec2 localCenterB = pc->localCenterB;
  564. float32 mB = pc->invMassB;
  565. float32 iB = pc->invIB;
  566. int32 pointCount = pc->pointCount;
  567. b2Vec2 cA = m_positions[indexA].c;
  568. float32 aA = m_positions[indexA].a;
  569. b2Vec2 cB = m_positions[indexB].c;
  570. float32 aB = m_positions[indexB].a;
  571. // Solve normal constraints
  572. for (int32 j = 0; j < pointCount; ++j)
  573. {
  574. b2Transform xfA, xfB;
  575. xfA.q.Set(aA);
  576. xfB.q.Set(aB);
  577. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  578. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  579. b2PositionSolverManifold psm;
  580. psm.Initialize(pc, xfA, xfB, j);
  581. b2Vec2 normal = psm.normal;
  582. b2Vec2 point = psm.point;
  583. float32 separation = psm.separation;
  584. b2Vec2 rA = point - cA;
  585. b2Vec2 rB = point - cB;
  586. // Track max constraint error.
  587. minSeparation = b2Min(minSeparation, separation);
  588. // Prevent large corrections and allow slop.
  589. float32 C = b2Clamp(b2_baumgarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  590. // Compute the effective mass.
  591. float32 rnA = b2Cross(rA, normal);
  592. float32 rnB = b2Cross(rB, normal);
  593. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  594. // Compute normal impulse
  595. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  596. b2Vec2 P = impulse * normal;
  597. cA -= mA * P;
  598. aA -= iA * b2Cross(rA, P);
  599. cB += mB * P;
  600. aB += iB * b2Cross(rB, P);
  601. }
  602. m_positions[indexA].c = cA;
  603. m_positions[indexA].a = aA;
  604. m_positions[indexB].c = cB;
  605. m_positions[indexB].a = aB;
  606. }
  607. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  608. // push the separation above -b2_linearSlop.
  609. return minSeparation >= -3.0f * b2_linearSlop;
  610. }
  611. // Sequential position solver for position constraints.
  612. bool b2ContactSolver::SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB)
  613. {
  614. float32 minSeparation = 0.0f;
  615. for (int32 i = 0; i < m_count; ++i)
  616. {
  617. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  618. int32 indexA = pc->indexA;
  619. int32 indexB = pc->indexB;
  620. b2Vec2 localCenterA = pc->localCenterA;
  621. b2Vec2 localCenterB = pc->localCenterB;
  622. int32 pointCount = pc->pointCount;
  623. float32 mA = 0.0f;
  624. float32 iA = 0.0f;
  625. if (indexA == toiIndexA || indexA == toiIndexB)
  626. {
  627. mA = pc->invMassA;
  628. iA = pc->invIA;
  629. }
  630. float32 mB = 0.0f;
  631. float32 iB = 0.;
  632. if (indexB == toiIndexA || indexB == toiIndexB)
  633. {
  634. mB = pc->invMassB;
  635. iB = pc->invIB;
  636. }
  637. b2Vec2 cA = m_positions[indexA].c;
  638. float32 aA = m_positions[indexA].a;
  639. b2Vec2 cB = m_positions[indexB].c;
  640. float32 aB = m_positions[indexB].a;
  641. // Solve normal constraints
  642. for (int32 j = 0; j < pointCount; ++j)
  643. {
  644. b2Transform xfA, xfB;
  645. xfA.q.Set(aA);
  646. xfB.q.Set(aB);
  647. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  648. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  649. b2PositionSolverManifold psm;
  650. psm.Initialize(pc, xfA, xfB, j);
  651. b2Vec2 normal = psm.normal;
  652. b2Vec2 point = psm.point;
  653. float32 separation = psm.separation;
  654. b2Vec2 rA = point - cA;
  655. b2Vec2 rB = point - cB;
  656. // Track max constraint error.
  657. minSeparation = b2Min(minSeparation, separation);
  658. // Prevent large corrections and allow slop.
  659. float32 C = b2Clamp(b2_toiBaugarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  660. // Compute the effective mass.
  661. float32 rnA = b2Cross(rA, normal);
  662. float32 rnB = b2Cross(rB, normal);
  663. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  664. // Compute normal impulse
  665. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  666. b2Vec2 P = impulse * normal;
  667. cA -= mA * P;
  668. aA -= iA * b2Cross(rA, P);
  669. cB += mB * P;
  670. aB += iB * b2Cross(rB, P);
  671. }
  672. m_positions[indexA].c = cA;
  673. m_positions[indexA].a = aA;
  674. m_positions[indexB].c = cB;
  675. m_positions[indexB].a = aB;
  676. }
  677. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  678. // push the separation above -b2_linearSlop.
  679. return minSeparation >= -1.5f * b2_linearSlop;
  680. }