b2ContactSolver.cpp 22 KB

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