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