b2ContactSolver.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. bool g_blockSolve = true;
  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 && g_blockSolve)
  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 (pointCount == 1 || g_blockSolve == false)
  288. {
  289. for (int32 i = 0; i < pointCount; ++i)
  290. {
  291. b2VelocityConstraintPoint* vcp = vc->points + i;
  292. // Relative velocity at contact
  293. b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
  294. // Compute normal impulse
  295. float32 vn = b2Dot(dv, normal);
  296. float32 lambda = -vcp->normalMass * (vn - vcp->velocityBias);
  297. // b2Clamp the accumulated impulse
  298. float32 newImpulse = b2Max(vcp->normalImpulse + lambda, 0.0f);
  299. lambda = newImpulse - vcp->normalImpulse;
  300. vcp->normalImpulse = newImpulse;
  301. // Apply contact impulse
  302. b2Vec2 P = lambda * normal;
  303. vA -= mA * P;
  304. wA -= iA * b2Cross(vcp->rA, P);
  305. vB += mB * P;
  306. wB += iB * b2Cross(vcp->rB, P);
  307. }
  308. }
  309. else
  310. {
  311. // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
  312. // Build the mini LCP for this contact patch
  313. //
  314. // vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
  315. //
  316. // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
  317. // b = vn0 - velocityBias
  318. //
  319. // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
  320. // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
  321. // 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
  322. // solution that satisfies the problem is chosen.
  323. //
  324. // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
  325. // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
  326. //
  327. // Substitute:
  328. //
  329. // x = a + d
  330. //
  331. // a := old total impulse
  332. // x := new total impulse
  333. // d := incremental impulse
  334. //
  335. // For the current iteration we extend the formula for the incremental impulse
  336. // to compute the new total impulse:
  337. //
  338. // vn = A * d + b
  339. // = A * (x - a) + b
  340. // = A * x + b - A * a
  341. // = A * x + b'
  342. // b' = b - A * a;
  343. b2VelocityConstraintPoint* cp1 = vc->points + 0;
  344. b2VelocityConstraintPoint* cp2 = vc->points + 1;
  345. b2Vec2 a(cp1->normalImpulse, cp2->normalImpulse);
  346. b2Assert(a.x >= 0.0f && a.y >= 0.0f);
  347. // Relative velocity at contact
  348. b2Vec2 dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  349. b2Vec2 dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  350. // Compute normal velocity
  351. float32 vn1 = b2Dot(dv1, normal);
  352. float32 vn2 = b2Dot(dv2, normal);
  353. b2Vec2 b;
  354. b.x = vn1 - cp1->velocityBias;
  355. b.y = vn2 - cp2->velocityBias;
  356. // Compute b'
  357. b -= b2Mul(vc->K, a);
  358. const float32 k_errorTol = 1e-3f;
  359. B2_NOT_USED(k_errorTol);
  360. for (;;)
  361. {
  362. //
  363. // Case 1: vn = 0
  364. //
  365. // 0 = A * x + b'
  366. //
  367. // Solve for x:
  368. //
  369. // x = - inv(A) * b'
  370. //
  371. b2Vec2 x = - b2Mul(vc->normalMass, b);
  372. if (x.x >= 0.0f && x.y >= 0.0f)
  373. {
  374. // Get the incremental impulse
  375. b2Vec2 d = x - a;
  376. // Apply incremental impulse
  377. b2Vec2 P1 = d.x * normal;
  378. b2Vec2 P2 = d.y * normal;
  379. vA -= mA * (P1 + P2);
  380. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  381. vB += mB * (P1 + P2);
  382. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  383. // Accumulate
  384. cp1->normalImpulse = x.x;
  385. cp2->normalImpulse = x.y;
  386. #if B2_DEBUG_SOLVER == 1
  387. // Postconditions
  388. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  389. dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  390. // Compute normal velocity
  391. vn1 = b2Dot(dv1, normal);
  392. vn2 = b2Dot(dv2, normal);
  393. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  394. b2Assert(b2Abs(vn2 - cp2->velocityBias) < k_errorTol);
  395. #endif
  396. break;
  397. }
  398. //
  399. // Case 2: vn1 = 0 and x2 = 0
  400. //
  401. // 0 = a11 * x1 + a12 * 0 + b1'
  402. // vn2 = a21 * x1 + a22 * 0 + b2'
  403. //
  404. x.x = - cp1->normalMass * b.x;
  405. x.y = 0.0f;
  406. vn1 = 0.0f;
  407. vn2 = vc->K.ex.y * x.x + b.y;
  408. if (x.x >= 0.0f && vn2 >= 0.0f)
  409. {
  410. // Get the incremental impulse
  411. b2Vec2 d = x - a;
  412. // Apply incremental impulse
  413. b2Vec2 P1 = d.x * normal;
  414. b2Vec2 P2 = d.y * normal;
  415. vA -= mA * (P1 + P2);
  416. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  417. vB += mB * (P1 + P2);
  418. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  419. // Accumulate
  420. cp1->normalImpulse = x.x;
  421. cp2->normalImpulse = x.y;
  422. #if B2_DEBUG_SOLVER == 1
  423. // Postconditions
  424. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  425. // Compute normal velocity
  426. vn1 = b2Dot(dv1, normal);
  427. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  428. #endif
  429. break;
  430. }
  431. //
  432. // Case 3: vn2 = 0 and x1 = 0
  433. //
  434. // vn1 = a11 * 0 + a12 * x2 + b1'
  435. // 0 = a21 * 0 + a22 * x2 + b2'
  436. //
  437. x.x = 0.0f;
  438. x.y = - cp2->normalMass * b.y;
  439. vn1 = vc->K.ey.x * x.y + b.x;
  440. vn2 = 0.0f;
  441. if (x.y >= 0.0f && vn1 >= 0.0f)
  442. {
  443. // Resubstitute for the incremental impulse
  444. b2Vec2 d = x - a;
  445. // Apply incremental impulse
  446. b2Vec2 P1 = d.x * normal;
  447. b2Vec2 P2 = d.y * normal;
  448. vA -= mA * (P1 + P2);
  449. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  450. vB += mB * (P1 + P2);
  451. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  452. // Accumulate
  453. cp1->normalImpulse = x.x;
  454. cp2->normalImpulse = x.y;
  455. #if B2_DEBUG_SOLVER == 1
  456. // Postconditions
  457. dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  458. // Compute normal velocity
  459. vn2 = b2Dot(dv2, normal);
  460. b2Assert(b2Abs(vn2 - cp2->velocityBias) < k_errorTol);
  461. #endif
  462. break;
  463. }
  464. //
  465. // Case 4: x1 = 0 and x2 = 0
  466. //
  467. // vn1 = b1
  468. // vn2 = b2;
  469. x.x = 0.0f;
  470. x.y = 0.0f;
  471. vn1 = b.x;
  472. vn2 = b.y;
  473. if (vn1 >= 0.0f && vn2 >= 0.0f )
  474. {
  475. // Resubstitute for the incremental impulse
  476. b2Vec2 d = x - a;
  477. // Apply incremental impulse
  478. b2Vec2 P1 = d.x * normal;
  479. b2Vec2 P2 = d.y * normal;
  480. vA -= mA * (P1 + P2);
  481. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  482. vB += mB * (P1 + P2);
  483. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  484. // Accumulate
  485. cp1->normalImpulse = x.x;
  486. cp2->normalImpulse = x.y;
  487. break;
  488. }
  489. // No solution, give up. This is hit sometimes, but it doesn't seem to matter.
  490. break;
  491. }
  492. }
  493. m_velocities[indexA].v = vA;
  494. m_velocities[indexA].w = wA;
  495. m_velocities[indexB].v = vB;
  496. m_velocities[indexB].w = wB;
  497. }
  498. }
  499. void b2ContactSolver::StoreImpulses()
  500. {
  501. for (int32 i = 0; i < m_count; ++i)
  502. {
  503. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  504. b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
  505. for (int32 j = 0; j < vc->pointCount; ++j)
  506. {
  507. manifold->points[j].normalImpulse = vc->points[j].normalImpulse;
  508. manifold->points[j].tangentImpulse = vc->points[j].tangentImpulse;
  509. }
  510. }
  511. }
  512. struct b2PositionSolverManifold
  513. {
  514. void Initialize(b2ContactPositionConstraint* pc, const b2Transform& xfA, const b2Transform& xfB, int32 index)
  515. {
  516. b2Assert(pc->pointCount > 0);
  517. switch (pc->type)
  518. {
  519. case b2Manifold::e_circles:
  520. {
  521. b2Vec2 pointA = b2Mul(xfA, pc->localPoint);
  522. b2Vec2 pointB = b2Mul(xfB, pc->localPoints[0]);
  523. normal = pointB - pointA;
  524. normal.Normalize();
  525. point = 0.5f * (pointA + pointB);
  526. separation = b2Dot(pointB - pointA, normal) - pc->radiusA - pc->radiusB;
  527. }
  528. break;
  529. case b2Manifold::e_faceA:
  530. {
  531. normal = b2Mul(xfA.q, pc->localNormal);
  532. b2Vec2 planePoint = b2Mul(xfA, pc->localPoint);
  533. b2Vec2 clipPoint = b2Mul(xfB, pc->localPoints[index]);
  534. separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
  535. point = clipPoint;
  536. }
  537. break;
  538. case b2Manifold::e_faceB:
  539. {
  540. normal = b2Mul(xfB.q, pc->localNormal);
  541. b2Vec2 planePoint = b2Mul(xfB, pc->localPoint);
  542. b2Vec2 clipPoint = b2Mul(xfA, pc->localPoints[index]);
  543. separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
  544. point = clipPoint;
  545. // Ensure normal points from A to B
  546. normal = -normal;
  547. }
  548. break;
  549. }
  550. }
  551. b2Vec2 normal;
  552. b2Vec2 point;
  553. float32 separation;
  554. };
  555. // Sequential solver.
  556. bool b2ContactSolver::SolvePositionConstraints()
  557. {
  558. float32 minSeparation = 0.0f;
  559. for (int32 i = 0; i < m_count; ++i)
  560. {
  561. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  562. int32 indexA = pc->indexA;
  563. int32 indexB = pc->indexB;
  564. b2Vec2 localCenterA = pc->localCenterA;
  565. float32 mA = pc->invMassA;
  566. float32 iA = pc->invIA;
  567. b2Vec2 localCenterB = pc->localCenterB;
  568. float32 mB = pc->invMassB;
  569. float32 iB = pc->invIB;
  570. int32 pointCount = pc->pointCount;
  571. b2Vec2 cA = m_positions[indexA].c;
  572. float32 aA = m_positions[indexA].a;
  573. b2Vec2 cB = m_positions[indexB].c;
  574. float32 aB = m_positions[indexB].a;
  575. // Solve normal constraints
  576. for (int32 j = 0; j < pointCount; ++j)
  577. {
  578. b2Transform xfA, xfB;
  579. xfA.q.Set(aA);
  580. xfB.q.Set(aB);
  581. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  582. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  583. b2PositionSolverManifold psm;
  584. psm.Initialize(pc, xfA, xfB, j);
  585. b2Vec2 normal = psm.normal;
  586. b2Vec2 point = psm.point;
  587. float32 separation = psm.separation;
  588. b2Vec2 rA = point - cA;
  589. b2Vec2 rB = point - cB;
  590. // Track max constraint error.
  591. minSeparation = b2Min(minSeparation, separation);
  592. // Prevent large corrections and allow slop.
  593. float32 C = b2Clamp(b2_baumgarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  594. // Compute the effective mass.
  595. float32 rnA = b2Cross(rA, normal);
  596. float32 rnB = b2Cross(rB, normal);
  597. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  598. // Compute normal impulse
  599. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  600. b2Vec2 P = impulse * normal;
  601. cA -= mA * P;
  602. aA -= iA * b2Cross(rA, P);
  603. cB += mB * P;
  604. aB += iB * b2Cross(rB, P);
  605. }
  606. m_positions[indexA].c = cA;
  607. m_positions[indexA].a = aA;
  608. m_positions[indexB].c = cB;
  609. m_positions[indexB].a = aB;
  610. }
  611. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  612. // push the separation above -b2_linearSlop.
  613. return minSeparation >= -3.0f * b2_linearSlop;
  614. }
  615. // Sequential position solver for position constraints.
  616. bool b2ContactSolver::SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB)
  617. {
  618. float32 minSeparation = 0.0f;
  619. for (int32 i = 0; i < m_count; ++i)
  620. {
  621. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  622. int32 indexA = pc->indexA;
  623. int32 indexB = pc->indexB;
  624. b2Vec2 localCenterA = pc->localCenterA;
  625. b2Vec2 localCenterB = pc->localCenterB;
  626. int32 pointCount = pc->pointCount;
  627. float32 mA = 0.0f;
  628. float32 iA = 0.0f;
  629. if (indexA == toiIndexA || indexA == toiIndexB)
  630. {
  631. mA = pc->invMassA;
  632. iA = pc->invIA;
  633. }
  634. float32 mB = 0.0f;
  635. float32 iB = 0.;
  636. if (indexB == toiIndexA || indexB == toiIndexB)
  637. {
  638. mB = pc->invMassB;
  639. iB = pc->invIB;
  640. }
  641. b2Vec2 cA = m_positions[indexA].c;
  642. float32 aA = m_positions[indexA].a;
  643. b2Vec2 cB = m_positions[indexB].c;
  644. float32 aB = m_positions[indexB].a;
  645. // Solve normal constraints
  646. for (int32 j = 0; j < pointCount; ++j)
  647. {
  648. b2Transform xfA, xfB;
  649. xfA.q.Set(aA);
  650. xfB.q.Set(aB);
  651. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  652. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  653. b2PositionSolverManifold psm;
  654. psm.Initialize(pc, xfA, xfB, j);
  655. b2Vec2 normal = psm.normal;
  656. b2Vec2 point = psm.point;
  657. float32 separation = psm.separation;
  658. b2Vec2 rA = point - cA;
  659. b2Vec2 rB = point - cB;
  660. // Track max constraint error.
  661. minSeparation = b2Min(minSeparation, separation);
  662. // Prevent large corrections and allow slop.
  663. float32 C = b2Clamp(b2_toiBaugarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  664. // Compute the effective mass.
  665. float32 rnA = b2Cross(rA, normal);
  666. float32 rnB = b2Cross(rB, normal);
  667. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  668. // Compute normal impulse
  669. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  670. b2Vec2 P = impulse * normal;
  671. cA -= mA * P;
  672. aA -= iA * b2Cross(rA, P);
  673. cB += mB * P;
  674. aB += iB * b2Cross(rB, P);
  675. }
  676. m_positions[indexA].c = cA;
  677. m_positions[indexA].a = aA;
  678. m_positions[indexB].c = cB;
  679. m_positions[indexB].a = aB;
  680. }
  681. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  682. // push the separation above -b2_linearSlop.
  683. return minSeparation >= -1.5f * b2_linearSlop;
  684. }