The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

833 lines
23KB

  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 "b2ContactSolver.h"
  19. #include "b2Contact.h"
  20. #include "../b2Body.h"
  21. #include "../b2Fixture.h"
  22. #include "../b2World.h"
  23. #include "../../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->indexA = bodyA->m_islandIndex;
  68. vc->indexB = bodyB->m_islandIndex;
  69. vc->invMassA = bodyA->m_invMass;
  70. vc->invMassB = bodyB->m_invMass;
  71. vc->invIA = bodyA->m_invI;
  72. vc->invIB = bodyB->m_invI;
  73. vc->contactIndex = i;
  74. vc->pointCount = pointCount;
  75. vc->K.SetZero();
  76. vc->normalMass.SetZero();
  77. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  78. pc->indexA = bodyA->m_islandIndex;
  79. pc->indexB = bodyB->m_islandIndex;
  80. pc->invMassA = bodyA->m_invMass;
  81. pc->invMassB = bodyB->m_invMass;
  82. pc->localCenterA = bodyA->m_sweep.localCenter;
  83. pc->localCenterB = bodyB->m_sweep.localCenter;
  84. pc->invIA = bodyA->m_invI;
  85. pc->invIB = bodyB->m_invI;
  86. pc->localNormal = manifold->localNormal;
  87. pc->localPoint = manifold->localPoint;
  88. pc->pointCount = pointCount;
  89. pc->radiusA = radiusA;
  90. pc->radiusB = radiusB;
  91. pc->type = manifold->type;
  92. for (int32 j = 0; j < pointCount; ++j)
  93. {
  94. b2ManifoldPoint* cp = manifold->points + j;
  95. b2VelocityConstraintPoint* vcp = vc->points + j;
  96. if (m_step.warmStarting)
  97. {
  98. vcp->normalImpulse = m_step.dtRatio * cp->normalImpulse;
  99. vcp->tangentImpulse = m_step.dtRatio * cp->tangentImpulse;
  100. }
  101. else
  102. {
  103. vcp->normalImpulse = 0.0f;
  104. vcp->tangentImpulse = 0.0f;
  105. }
  106. vcp->rA.SetZero();
  107. vcp->rB.SetZero();
  108. vcp->normalMass = 0.0f;
  109. vcp->tangentMass = 0.0f;
  110. vcp->velocityBias = 0.0f;
  111. pc->localPoints[j] = cp->localPoint;
  112. }
  113. }
  114. }
  115. b2ContactSolver::~b2ContactSolver()
  116. {
  117. m_allocator->Free(m_velocityConstraints);
  118. m_allocator->Free(m_positionConstraints);
  119. }
  120. // Initialize position dependent portions of the velocity constraints.
  121. void b2ContactSolver::InitializeVelocityConstraints()
  122. {
  123. for (int32 i = 0; i < m_count; ++i)
  124. {
  125. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  126. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  127. float32 radiusA = pc->radiusA;
  128. float32 radiusB = pc->radiusB;
  129. b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
  130. int32 indexA = vc->indexA;
  131. int32 indexB = vc->indexB;
  132. float32 mA = vc->invMassA;
  133. float32 mB = vc->invMassB;
  134. float32 iA = vc->invIA;
  135. float32 iB = vc->invIB;
  136. b2Vec2 localCenterA = pc->localCenterA;
  137. b2Vec2 localCenterB = pc->localCenterB;
  138. b2Vec2 cA = m_positions[indexA].c;
  139. float32 aA = m_positions[indexA].a;
  140. b2Vec2 vA = m_velocities[indexA].v;
  141. float32 wA = m_velocities[indexA].w;
  142. b2Vec2 cB = m_positions[indexB].c;
  143. float32 aB = m_positions[indexB].a;
  144. b2Vec2 vB = m_velocities[indexB].v;
  145. float32 wB = m_velocities[indexB].w;
  146. b2Assert(manifold->pointCount > 0);
  147. b2Transform xfA, xfB;
  148. xfA.q.Set(aA);
  149. xfB.q.Set(aB);
  150. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  151. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  152. b2WorldManifold worldManifold;
  153. worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB);
  154. vc->normal = worldManifold.normal;
  155. int32 pointCount = vc->pointCount;
  156. for (int32 j = 0; j < pointCount; ++j)
  157. {
  158. b2VelocityConstraintPoint* vcp = vc->points + j;
  159. vcp->rA = worldManifold.points[j] - cA;
  160. vcp->rB = worldManifold.points[j] - cB;
  161. float32 rnA = b2Cross(vcp->rA, vc->normal);
  162. float32 rnB = b2Cross(vcp->rB, vc->normal);
  163. float32 kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  164. vcp->normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f;
  165. b2Vec2 tangent = b2Cross(vc->normal, 1.0f);
  166. float32 rtA = b2Cross(vcp->rA, tangent);
  167. float32 rtB = b2Cross(vcp->rB, tangent);
  168. float32 kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB;
  169. vcp->tangentMass = kTangent > 0.0f ? 1.0f / kTangent : 0.0f;
  170. // Setup a velocity bias for restitution.
  171. vcp->velocityBias = 0.0f;
  172. float32 vRel = b2Dot(vc->normal, vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA));
  173. if (vRel < -b2_velocityThreshold)
  174. {
  175. vcp->velocityBias = -vc->restitution * vRel;
  176. }
  177. }
  178. // If we have two points, then prepare the block solver.
  179. if (vc->pointCount == 2)
  180. {
  181. b2VelocityConstraintPoint* vcp1 = vc->points + 0;
  182. b2VelocityConstraintPoint* vcp2 = vc->points + 1;
  183. float32 rn1A = b2Cross(vcp1->rA, vc->normal);
  184. float32 rn1B = b2Cross(vcp1->rB, vc->normal);
  185. float32 rn2A = b2Cross(vcp2->rA, vc->normal);
  186. float32 rn2B = b2Cross(vcp2->rB, vc->normal);
  187. float32 k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
  188. float32 k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
  189. float32 k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B;
  190. // Ensure a reasonable condition number.
  191. const float32 k_maxConditionNumber = 1000.0f;
  192. if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
  193. {
  194. // K is safe to invert.
  195. vc->K.ex.Set(k11, k12);
  196. vc->K.ey.Set(k12, k22);
  197. vc->normalMass = vc->K.GetInverse();
  198. }
  199. else
  200. {
  201. // The constraints are redundant, just use one.
  202. // TODO_ERIN use deepest?
  203. vc->pointCount = 1;
  204. }
  205. }
  206. }
  207. }
  208. void b2ContactSolver::WarmStart()
  209. {
  210. // Warm start.
  211. for (int32 i = 0; i < m_count; ++i)
  212. {
  213. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  214. int32 indexA = vc->indexA;
  215. int32 indexB = vc->indexB;
  216. float32 mA = vc->invMassA;
  217. float32 iA = vc->invIA;
  218. float32 mB = vc->invMassB;
  219. float32 iB = vc->invIB;
  220. int32 pointCount = vc->pointCount;
  221. b2Vec2 vA = m_velocities[indexA].v;
  222. float32 wA = m_velocities[indexA].w;
  223. b2Vec2 vB = m_velocities[indexB].v;
  224. float32 wB = m_velocities[indexB].w;
  225. b2Vec2 normal = vc->normal;
  226. b2Vec2 tangent = b2Cross(normal, 1.0f);
  227. for (int32 j = 0; j < pointCount; ++j)
  228. {
  229. b2VelocityConstraintPoint* vcp = vc->points + j;
  230. b2Vec2 P = vcp->normalImpulse * normal + vcp->tangentImpulse * tangent;
  231. wA -= iA * b2Cross(vcp->rA, P);
  232. vA -= mA * P;
  233. wB += iB * b2Cross(vcp->rB, P);
  234. vB += mB * P;
  235. }
  236. m_velocities[indexA].v = vA;
  237. m_velocities[indexA].w = wA;
  238. m_velocities[indexB].v = vB;
  239. m_velocities[indexB].w = wB;
  240. }
  241. }
  242. void b2ContactSolver::SolveVelocityConstraints()
  243. {
  244. for (int32 i = 0; i < m_count; ++i)
  245. {
  246. b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
  247. int32 indexA = vc->indexA;
  248. int32 indexB = vc->indexB;
  249. float32 mA = vc->invMassA;
  250. float32 iA = vc->invIA;
  251. float32 mB = vc->invMassB;
  252. float32 iB = vc->invIB;
  253. int32 pointCount = vc->pointCount;
  254. b2Vec2 vA = m_velocities[indexA].v;
  255. float32 wA = m_velocities[indexA].w;
  256. b2Vec2 vB = m_velocities[indexB].v;
  257. float32 wB = m_velocities[indexB].w;
  258. b2Vec2 normal = vc->normal;
  259. b2Vec2 tangent = b2Cross(normal, 1.0f);
  260. float32 friction = vc->friction;
  261. b2Assert(pointCount == 1 || pointCount == 2);
  262. // Solve tangent constraints first because non-penetration is more important
  263. // than friction.
  264. for (int32 j = 0; j < pointCount; ++j)
  265. {
  266. b2VelocityConstraintPoint* vcp = vc->points + j;
  267. // Relative velocity at contact
  268. b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
  269. // Compute tangent force
  270. float32 vt = b2Dot(dv, tangent);
  271. float32 lambda = vcp->tangentMass * (-vt);
  272. // b2Clamp the accumulated force
  273. float32 maxFriction = friction * vcp->normalImpulse;
  274. float32 newImpulse = b2Clamp(vcp->tangentImpulse + lambda, -maxFriction, maxFriction);
  275. lambda = newImpulse - vcp->tangentImpulse;
  276. vcp->tangentImpulse = newImpulse;
  277. // Apply contact impulse
  278. b2Vec2 P = lambda * tangent;
  279. vA -= mA * P;
  280. wA -= iA * b2Cross(vcp->rA, P);
  281. vB += mB * P;
  282. wB += iB * b2Cross(vcp->rB, P);
  283. }
  284. // Solve normal constraints
  285. if (vc->pointCount == 1)
  286. {
  287. b2VelocityConstraintPoint* vcp = vc->points + 0;
  288. // Relative velocity at contact
  289. b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
  290. // Compute normal impulse
  291. float32 vn = b2Dot(dv, normal);
  292. float32 lambda = -vcp->normalMass * (vn - vcp->velocityBias);
  293. // b2Clamp the accumulated impulse
  294. float32 newImpulse = b2Max(vcp->normalImpulse + lambda, 0.0f);
  295. lambda = newImpulse - vcp->normalImpulse;
  296. vcp->normalImpulse = newImpulse;
  297. // Apply contact impulse
  298. b2Vec2 P = lambda * normal;
  299. vA -= mA * P;
  300. wA -= iA * b2Cross(vcp->rA, P);
  301. vB += mB * P;
  302. wB += iB * b2Cross(vcp->rB, P);
  303. }
  304. else
  305. {
  306. // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
  307. // Build the mini LCP for this contact patch
  308. //
  309. // vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
  310. //
  311. // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
  312. // b = vn0 - velocityBias
  313. //
  314. // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
  315. // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
  316. // 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
  317. // solution that satisfies the problem is chosen.
  318. //
  319. // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
  320. // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
  321. //
  322. // Substitute:
  323. //
  324. // x = a + d
  325. //
  326. // a := old total impulse
  327. // x := new total impulse
  328. // d := incremental impulse
  329. //
  330. // For the current iteration we extend the formula for the incremental impulse
  331. // to compute the new total impulse:
  332. //
  333. // vn = A * d + b
  334. // = A * (x - a) + b
  335. // = A * x + b - A * a
  336. // = A * x + b'
  337. // b' = b - A * a;
  338. b2VelocityConstraintPoint* cp1 = vc->points + 0;
  339. b2VelocityConstraintPoint* cp2 = vc->points + 1;
  340. b2Vec2 a(cp1->normalImpulse, cp2->normalImpulse);
  341. b2Assert(a.x >= 0.0f && a.y >= 0.0f);
  342. // Relative velocity at contact
  343. b2Vec2 dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  344. b2Vec2 dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  345. // Compute normal velocity
  346. float32 vn1 = b2Dot(dv1, normal);
  347. float32 vn2 = b2Dot(dv2, normal);
  348. b2Vec2 b;
  349. b.x = vn1 - cp1->velocityBias;
  350. b.y = vn2 - cp2->velocityBias;
  351. // Compute b'
  352. b -= b2Mul(vc->K, a);
  353. const float32 k_errorTol = 1e-3f;
  354. B2_NOT_USED(k_errorTol);
  355. for (;;)
  356. {
  357. //
  358. // Case 1: vn = 0
  359. //
  360. // 0 = A * x + b'
  361. //
  362. // Solve for x:
  363. //
  364. // x = - inv(A) * b'
  365. //
  366. b2Vec2 x = - b2Mul(vc->normalMass, b);
  367. if (x.x >= 0.0f && x.y >= 0.0f)
  368. {
  369. // Get the incremental impulse
  370. b2Vec2 d = x - a;
  371. // Apply incremental impulse
  372. b2Vec2 P1 = d.x * normal;
  373. b2Vec2 P2 = d.y * normal;
  374. vA -= mA * (P1 + P2);
  375. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  376. vB += mB * (P1 + P2);
  377. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  378. // Accumulate
  379. cp1->normalImpulse = x.x;
  380. cp2->normalImpulse = x.y;
  381. #if B2_DEBUG_SOLVER == 1
  382. // Postconditions
  383. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  384. dv2 = vB + b2Cross(wB, cp2->rB) - vA - b2Cross(wA, cp2->rA);
  385. // Compute normal velocity
  386. vn1 = b2Dot(dv1, normal);
  387. vn2 = b2Dot(dv2, normal);
  388. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  389. b2Assert(b2Abs(vn2 - cp2->velocityBias) < k_errorTol);
  390. #endif
  391. break;
  392. }
  393. //
  394. // Case 2: vn1 = 0 and x2 = 0
  395. //
  396. // 0 = a11 * x1 + a12 * 0 + b1'
  397. // vn2 = a21 * x1 + a22 * 0 + b2'
  398. //
  399. x.x = - cp1->normalMass * b.x;
  400. x.y = 0.0f;
  401. //vn1 = 0.0f;
  402. vn2 = vc->K.ex.y * x.x + b.y;
  403. if (x.x >= 0.0f && vn2 >= 0.0f)
  404. {
  405. // Get the incremental impulse
  406. b2Vec2 d = x - a;
  407. // Apply incremental impulse
  408. b2Vec2 P1 = d.x * normal;
  409. b2Vec2 P2 = d.y * normal;
  410. vA -= mA * (P1 + P2);
  411. wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
  412. vB += mB * (P1 + P2);
  413. wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
  414. // Accumulate
  415. cp1->normalImpulse = x.x;
  416. cp2->normalImpulse = x.y;
  417. #if B2_DEBUG_SOLVER == 1
  418. // Postconditions
  419. dv1 = vB + b2Cross(wB, cp1->rB) - vA - b2Cross(wA, cp1->rA);
  420. // Compute normal velocity
  421. vn1 = b2Dot(dv1, normal);
  422. b2Assert(b2Abs(vn1 - cp1->velocityBias) < k_errorTol);
  423. #endif
  424. break;
  425. }
  426. //
  427. // Case 3: vn2 = 0 and x1 = 0
  428. //
  429. // vn1 = a11 * 0 + a12 * x2 + b1'
  430. // 0 = a21 * 0 + a22 * x2 + b2'
  431. //
  432. x.x = 0.0f;
  433. x.y = - cp2->normalMass * b.y;
  434. vn1 = vc->K.ey.x * x.y + b.x;
  435. //vn2 = 0.0f;
  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. }
  545. }
  546. b2Vec2 normal;
  547. b2Vec2 point;
  548. float32 separation;
  549. };
  550. // Sequential solver.
  551. bool b2ContactSolver::SolvePositionConstraints()
  552. {
  553. float32 minSeparation = 0.0f;
  554. for (int32 i = 0; i < m_count; ++i)
  555. {
  556. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  557. int32 indexA = pc->indexA;
  558. int32 indexB = pc->indexB;
  559. b2Vec2 localCenterA = pc->localCenterA;
  560. float32 mA = pc->invMassA;
  561. float32 iA = pc->invIA;
  562. b2Vec2 localCenterB = pc->localCenterB;
  563. float32 mB = pc->invMassB;
  564. float32 iB = pc->invIB;
  565. int32 pointCount = pc->pointCount;
  566. b2Vec2 cA = m_positions[indexA].c;
  567. float32 aA = m_positions[indexA].a;
  568. b2Vec2 cB = m_positions[indexB].c;
  569. float32 aB = m_positions[indexB].a;
  570. // Solve normal constraints
  571. for (int32 j = 0; j < pointCount; ++j)
  572. {
  573. b2Transform xfA, xfB;
  574. xfA.q.Set(aA);
  575. xfB.q.Set(aB);
  576. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  577. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  578. b2PositionSolverManifold psm;
  579. psm.Initialize(pc, xfA, xfB, j);
  580. b2Vec2 normal = psm.normal;
  581. b2Vec2 point = psm.point;
  582. float32 separation = psm.separation;
  583. b2Vec2 rA = point - cA;
  584. b2Vec2 rB = point - cB;
  585. // Track max constraint error.
  586. minSeparation = b2Min(minSeparation, separation);
  587. // Prevent large corrections and allow slop.
  588. float32 C = b2Clamp(b2_baumgarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  589. // Compute the effective mass.
  590. float32 rnA = b2Cross(rA, normal);
  591. float32 rnB = b2Cross(rB, normal);
  592. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  593. // Compute normal impulse
  594. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  595. b2Vec2 P = impulse * normal;
  596. cA -= mA * P;
  597. aA -= iA * b2Cross(rA, P);
  598. cB += mB * P;
  599. aB += iB * b2Cross(rB, P);
  600. }
  601. m_positions[indexA].c = cA;
  602. m_positions[indexA].a = aA;
  603. m_positions[indexB].c = cB;
  604. m_positions[indexB].a = aB;
  605. }
  606. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  607. // push the separation above -b2_linearSlop.
  608. return minSeparation >= -3.0f * b2_linearSlop;
  609. }
  610. // Sequential position solver for position constraints.
  611. bool b2ContactSolver::SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB)
  612. {
  613. float32 minSeparation = 0.0f;
  614. for (int32 i = 0; i < m_count; ++i)
  615. {
  616. b2ContactPositionConstraint* pc = m_positionConstraints + i;
  617. int32 indexA = pc->indexA;
  618. int32 indexB = pc->indexB;
  619. b2Vec2 localCenterA = pc->localCenterA;
  620. b2Vec2 localCenterB = pc->localCenterB;
  621. int32 pointCount = pc->pointCount;
  622. float32 mA = 0.0f;
  623. float32 iA = 0.0f;
  624. if (indexA == toiIndexA || indexA == toiIndexB)
  625. {
  626. mA = pc->invMassA;
  627. iA = pc->invIA;
  628. }
  629. float32 mB = pc->invMassB;
  630. float32 iB = pc->invIB;
  631. if (indexB == toiIndexA || indexB == toiIndexB)
  632. {
  633. mB = pc->invMassB;
  634. iB = pc->invIB;
  635. }
  636. b2Vec2 cA = m_positions[indexA].c;
  637. float32 aA = m_positions[indexA].a;
  638. b2Vec2 cB = m_positions[indexB].c;
  639. float32 aB = m_positions[indexB].a;
  640. // Solve normal constraints
  641. for (int32 j = 0; j < pointCount; ++j)
  642. {
  643. b2Transform xfA, xfB;
  644. xfA.q.Set(aA);
  645. xfB.q.Set(aB);
  646. xfA.p = cA - b2Mul(xfA.q, localCenterA);
  647. xfB.p = cB - b2Mul(xfB.q, localCenterB);
  648. b2PositionSolverManifold psm;
  649. psm.Initialize(pc, xfA, xfB, j);
  650. b2Vec2 normal = psm.normal;
  651. b2Vec2 point = psm.point;
  652. float32 separation = psm.separation;
  653. b2Vec2 rA = point - cA;
  654. b2Vec2 rB = point - cB;
  655. // Track max constraint error.
  656. minSeparation = b2Min(minSeparation, separation);
  657. // Prevent large corrections and allow slop.
  658. float32 C = b2Clamp(b2_toiBaugarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
  659. // Compute the effective mass.
  660. float32 rnA = b2Cross(rA, normal);
  661. float32 rnB = b2Cross(rB, normal);
  662. float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
  663. // Compute normal impulse
  664. float32 impulse = K > 0.0f ? - C / K : 0.0f;
  665. b2Vec2 P = impulse * normal;
  666. cA -= mA * P;
  667. aA -= iA * b2Cross(rA, P);
  668. cB += mB * P;
  669. aB += iB * b2Cross(rB, P);
  670. }
  671. m_positions[indexA].c = cA;
  672. m_positions[indexA].a = aA;
  673. m_positions[indexB].c = cB;
  674. m_positions[indexB].a = aB;
  675. }
  676. // We can't expect minSpeparation >= -b2_linearSlop because we don't
  677. // push the separation above -b2_linearSlop.
  678. return minSeparation >= -1.5f * b2_linearSlop;
  679. }