|
@@ -1,5 +1,5 @@
|
|
|
/*
|
|
|
-* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
|
|
|
+* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
|
|
*
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
@@ -17,6 +17,7 @@
|
|
|
*/
|
|
|
|
|
|
#include <Box2D/Dynamics/Contacts/b2ContactSolver.h>
|
|
|
+
|
|
|
#include <Box2D/Dynamics/Contacts/b2Contact.h>
|
|
|
#include <Box2D/Dynamics/b2Body.h>
|
|
|
#include <Box2D/Dynamics/b2Fixture.h>
|
|
@@ -25,17 +26,36 @@
|
|
|
|
|
|
#define B2_DEBUG_SOLVER 0
|
|
|
|
|
|
-b2ContactSolver::b2ContactSolver(b2Contact** contacts, int32 contactCount,
|
|
|
- b2StackAllocator* allocator, float32 impulseRatio)
|
|
|
+struct b2ContactPositionConstraint
|
|
|
{
|
|
|
- m_allocator = allocator;
|
|
|
-
|
|
|
- m_constraintCount = contactCount;
|
|
|
- m_constraints = (b2ContactConstraint*)m_allocator->Allocate(m_constraintCount * sizeof(b2ContactConstraint));
|
|
|
+ b2Vec2 localPoints[b2_maxManifoldPoints];
|
|
|
+ b2Vec2 localNormal;
|
|
|
+ b2Vec2 localPoint;
|
|
|
+ int32 indexA;
|
|
|
+ int32 indexB;
|
|
|
+ float32 invMassA, invMassB;
|
|
|
+ b2Vec2 localCenterA, localCenterB;
|
|
|
+ float32 invIA, invIB;
|
|
|
+ b2Manifold::Type type;
|
|
|
+ float32 radiusA, radiusB;
|
|
|
+ int32 pointCount;
|
|
|
+};
|
|
|
|
|
|
- for (int32 i = 0; i < m_constraintCount; ++i)
|
|
|
+b2ContactSolver::b2ContactSolver(b2ContactSolverDef* def)
|
|
|
+{
|
|
|
+ m_step = def->step;
|
|
|
+ m_allocator = def->allocator;
|
|
|
+ m_count = def->count;
|
|
|
+ m_positionConstraints = (b2ContactPositionConstraint*)m_allocator->Allocate(m_count * sizeof(b2ContactPositionConstraint));
|
|
|
+ m_velocityConstraints = (b2ContactVelocityConstraint*)m_allocator->Allocate(m_count * sizeof(b2ContactVelocityConstraint));
|
|
|
+ m_positions = def->positions;
|
|
|
+ m_velocities = def->velocities;
|
|
|
+ m_contacts = def->contacts;
|
|
|
+
|
|
|
+ // Initialize position independent portions of the constraints.
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
{
|
|
|
- b2Contact* contact = contacts[i];
|
|
|
+ b2Contact* contact = m_contacts[i];
|
|
|
|
|
|
b2Fixture* fixtureA = contact->m_fixtureA;
|
|
|
b2Fixture* fixtureB = contact->m_fixtureB;
|
|
@@ -47,222 +67,302 @@ b2ContactSolver::b2ContactSolver(b2Contact** contacts, int32 contactCount,
|
|
|
b2Body* bodyB = fixtureB->GetBody();
|
|
|
b2Manifold* manifold = contact->GetManifold();
|
|
|
|
|
|
- float32 friction = b2MixFriction(fixtureA->GetFriction(), fixtureB->GetFriction());
|
|
|
- float32 restitution = b2MixRestitution(fixtureA->GetRestitution(), fixtureB->GetRestitution());
|
|
|
+ int32 pointCount = manifold->pointCount;
|
|
|
+ b2Assert(pointCount > 0);
|
|
|
+
|
|
|
+ b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
|
|
|
+ vc->friction = contact->m_friction;
|
|
|
+ vc->restitution = contact->m_restitution;
|
|
|
+ vc->indexA = bodyA->m_islandIndex;
|
|
|
+ vc->indexB = bodyB->m_islandIndex;
|
|
|
+ vc->invMassA = bodyA->m_invMass;
|
|
|
+ vc->invMassB = bodyB->m_invMass;
|
|
|
+ vc->invIA = bodyA->m_invI;
|
|
|
+ vc->invIB = bodyB->m_invI;
|
|
|
+ vc->contactIndex = i;
|
|
|
+ vc->pointCount = pointCount;
|
|
|
+ vc->K.SetZero();
|
|
|
+ vc->normalMass.SetZero();
|
|
|
+
|
|
|
+ b2ContactPositionConstraint* pc = m_positionConstraints + i;
|
|
|
+ pc->indexA = bodyA->m_islandIndex;
|
|
|
+ pc->indexB = bodyB->m_islandIndex;
|
|
|
+ pc->invMassA = bodyA->m_invMass;
|
|
|
+ pc->invMassB = bodyB->m_invMass;
|
|
|
+ pc->localCenterA = bodyA->m_sweep.localCenter;
|
|
|
+ pc->localCenterB = bodyB->m_sweep.localCenter;
|
|
|
+ pc->invIA = bodyA->m_invI;
|
|
|
+ pc->invIB = bodyB->m_invI;
|
|
|
+ pc->localNormal = manifold->localNormal;
|
|
|
+ pc->localPoint = manifold->localPoint;
|
|
|
+ pc->pointCount = pointCount;
|
|
|
+ pc->radiusA = radiusA;
|
|
|
+ pc->radiusB = radiusB;
|
|
|
+ pc->type = manifold->type;
|
|
|
+
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
+ {
|
|
|
+ b2ManifoldPoint* cp = manifold->points + j;
|
|
|
+ b2VelocityConstraintPoint* vcp = vc->points + j;
|
|
|
+
|
|
|
+ if (m_step.warmStarting)
|
|
|
+ {
|
|
|
+ vcp->normalImpulse = m_step.dtRatio * cp->normalImpulse;
|
|
|
+ vcp->tangentImpulse = m_step.dtRatio * cp->tangentImpulse;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ vcp->normalImpulse = 0.0f;
|
|
|
+ vcp->tangentImpulse = 0.0f;
|
|
|
+ }
|
|
|
+
|
|
|
+ vcp->rA.SetZero();
|
|
|
+ vcp->rB.SetZero();
|
|
|
+ vcp->normalMass = 0.0f;
|
|
|
+ vcp->tangentMass = 0.0f;
|
|
|
+ vcp->velocityBias = 0.0f;
|
|
|
+
|
|
|
+ pc->localPoints[j] = cp->localPoint;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+b2ContactSolver::~b2ContactSolver()
|
|
|
+{
|
|
|
+ m_allocator->Free(m_velocityConstraints);
|
|
|
+ m_allocator->Free(m_positionConstraints);
|
|
|
+}
|
|
|
+
|
|
|
+// Initialize position dependent portions of the velocity constraints.
|
|
|
+void b2ContactSolver::InitializeVelocityConstraints()
|
|
|
+{
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
+ {
|
|
|
+ b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
|
|
|
+ b2ContactPositionConstraint* pc = m_positionConstraints + i;
|
|
|
+
|
|
|
+ float32 radiusA = pc->radiusA;
|
|
|
+ float32 radiusB = pc->radiusB;
|
|
|
+ b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
|
|
|
+
|
|
|
+ int32 indexA = vc->indexA;
|
|
|
+ int32 indexB = vc->indexB;
|
|
|
+
|
|
|
+ float32 mA = vc->invMassA;
|
|
|
+ float32 mB = vc->invMassB;
|
|
|
+ float32 iA = vc->invIA;
|
|
|
+ float32 iB = vc->invIB;
|
|
|
+ b2Vec2 localCenterA = pc->localCenterA;
|
|
|
+ b2Vec2 localCenterB = pc->localCenterB;
|
|
|
|
|
|
- b2Vec2 vA = bodyA->m_linearVelocity;
|
|
|
- b2Vec2 vB = bodyB->m_linearVelocity;
|
|
|
- float32 wA = bodyA->m_angularVelocity;
|
|
|
- float32 wB = bodyB->m_angularVelocity;
|
|
|
+ b2Vec2 cA = m_positions[indexA].c;
|
|
|
+ float32 aA = m_positions[indexA].a;
|
|
|
+ b2Vec2 vA = m_velocities[indexA].v;
|
|
|
+ float32 wA = m_velocities[indexA].w;
|
|
|
+
|
|
|
+ b2Vec2 cB = m_positions[indexB].c;
|
|
|
+ float32 aB = m_positions[indexB].a;
|
|
|
+ b2Vec2 vB = m_velocities[indexB].v;
|
|
|
+ float32 wB = m_velocities[indexB].w;
|
|
|
|
|
|
b2Assert(manifold->pointCount > 0);
|
|
|
|
|
|
+ b2Transform xfA, xfB;
|
|
|
+ xfA.q.Set(aA);
|
|
|
+ xfB.q.Set(aB);
|
|
|
+ xfA.p = cA - b2Mul(xfA.q, localCenterA);
|
|
|
+ xfB.p = cB - b2Mul(xfB.q, localCenterB);
|
|
|
+
|
|
|
b2WorldManifold worldManifold;
|
|
|
- worldManifold.Initialize(manifold, bodyA->m_xf, radiusA, bodyB->m_xf, radiusB);
|
|
|
-
|
|
|
- b2ContactConstraint* cc = m_constraints + i;
|
|
|
- cc->bodyA = bodyA;
|
|
|
- cc->bodyB = bodyB;
|
|
|
- cc->manifold = manifold;
|
|
|
- cc->normal = worldManifold.normal;
|
|
|
- cc->pointCount = manifold->pointCount;
|
|
|
- cc->friction = friction;
|
|
|
-
|
|
|
- cc->localNormal = manifold->localNormal;
|
|
|
- cc->localPoint = manifold->localPoint;
|
|
|
- cc->radius = radiusA + radiusB;
|
|
|
- cc->type = manifold->type;
|
|
|
-
|
|
|
- for (int32 j = 0; j < cc->pointCount; ++j)
|
|
|
- {
|
|
|
- b2ManifoldPoint* cp = manifold->points + j;
|
|
|
- b2ContactConstraintPoint* ccp = cc->points + j;
|
|
|
+ worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB);
|
|
|
|
|
|
- ccp->normalImpulse = impulseRatio * cp->normalImpulse;
|
|
|
- ccp->tangentImpulse = impulseRatio * cp->tangentImpulse;
|
|
|
+ vc->normal = worldManifold.normal;
|
|
|
|
|
|
- ccp->localPoint = cp->localPoint;
|
|
|
+ int32 pointCount = vc->pointCount;
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
+ {
|
|
|
+ b2VelocityConstraintPoint* vcp = vc->points + j;
|
|
|
|
|
|
- ccp->rA = worldManifold.points[j] - bodyA->m_sweep.c;
|
|
|
- ccp->rB = worldManifold.points[j] - bodyB->m_sweep.c;
|
|
|
+ vcp->rA = worldManifold.points[j] - cA;
|
|
|
+ vcp->rB = worldManifold.points[j] - cB;
|
|
|
|
|
|
- float32 rnA = b2Cross(ccp->rA, cc->normal);
|
|
|
- float32 rnB = b2Cross(ccp->rB, cc->normal);
|
|
|
- rnA *= rnA;
|
|
|
- rnB *= rnB;
|
|
|
+ float32 rnA = b2Cross(vcp->rA, vc->normal);
|
|
|
+ float32 rnB = b2Cross(vcp->rB, vc->normal);
|
|
|
|
|
|
- float32 kNormal = bodyA->m_invMass + bodyB->m_invMass + bodyA->m_invI * rnA + bodyB->m_invI * rnB;
|
|
|
+ float32 kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
|
|
|
|
|
|
- b2Assert(kNormal > b2_epsilon);
|
|
|
- ccp->normalMass = 1.0f / kNormal;
|
|
|
+ vcp->normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f;
|
|
|
|
|
|
- b2Vec2 tangent = b2Cross(cc->normal, 1.0f);
|
|
|
+ b2Vec2 tangent = b2Cross(vc->normal, 1.0f);
|
|
|
|
|
|
- float32 rtA = b2Cross(ccp->rA, tangent);
|
|
|
- float32 rtB = b2Cross(ccp->rB, tangent);
|
|
|
- rtA *= rtA;
|
|
|
- rtB *= rtB;
|
|
|
+ float32 rtA = b2Cross(vcp->rA, tangent);
|
|
|
+ float32 rtB = b2Cross(vcp->rB, tangent);
|
|
|
|
|
|
- float32 kTangent = bodyA->m_invMass + bodyB->m_invMass + bodyA->m_invI * rtA + bodyB->m_invI * rtB;
|
|
|
+ float32 kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB;
|
|
|
|
|
|
- b2Assert(kTangent > b2_epsilon);
|
|
|
- ccp->tangentMass = 1.0f / kTangent;
|
|
|
+ vcp->tangentMass = kTangent > 0.0f ? 1.0f / kTangent : 0.0f;
|
|
|
|
|
|
// Setup a velocity bias for restitution.
|
|
|
- ccp->velocityBias = 0.0f;
|
|
|
- float32 vRel = b2Dot(cc->normal, vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA));
|
|
|
+ vcp->velocityBias = 0.0f;
|
|
|
+ float32 vRel = b2Dot(vc->normal, vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA));
|
|
|
if (vRel < -b2_velocityThreshold)
|
|
|
{
|
|
|
- ccp->velocityBias = -restitution * vRel;
|
|
|
+ vcp->velocityBias = -vc->restitution * vRel;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// If we have two points, then prepare the block solver.
|
|
|
- if (cc->pointCount == 2)
|
|
|
+ if (vc->pointCount == 2)
|
|
|
{
|
|
|
- b2ContactConstraintPoint* ccp1 = cc->points + 0;
|
|
|
- b2ContactConstraintPoint* ccp2 = cc->points + 1;
|
|
|
-
|
|
|
- float32 invMassA = bodyA->m_invMass;
|
|
|
- float32 invIA = bodyA->m_invI;
|
|
|
- float32 invMassB = bodyB->m_invMass;
|
|
|
- float32 invIB = bodyB->m_invI;
|
|
|
-
|
|
|
- float32 rn1A = b2Cross(ccp1->rA, cc->normal);
|
|
|
- float32 rn1B = b2Cross(ccp1->rB, cc->normal);
|
|
|
- float32 rn2A = b2Cross(ccp2->rA, cc->normal);
|
|
|
- float32 rn2B = b2Cross(ccp2->rB, cc->normal);
|
|
|
-
|
|
|
- float32 k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
|
|
|
- float32 k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
|
|
|
- float32 k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;
|
|
|
+ b2VelocityConstraintPoint* vcp1 = vc->points + 0;
|
|
|
+ b2VelocityConstraintPoint* vcp2 = vc->points + 1;
|
|
|
+
|
|
|
+ float32 rn1A = b2Cross(vcp1->rA, vc->normal);
|
|
|
+ float32 rn1B = b2Cross(vcp1->rB, vc->normal);
|
|
|
+ float32 rn2A = b2Cross(vcp2->rA, vc->normal);
|
|
|
+ float32 rn2B = b2Cross(vcp2->rB, vc->normal);
|
|
|
+
|
|
|
+ float32 k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
|
|
|
+ float32 k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
|
|
|
+ float32 k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B;
|
|
|
|
|
|
// Ensure a reasonable condition number.
|
|
|
- const float32 k_maxConditionNumber = 100.0f;
|
|
|
+ const float32 k_maxConditionNumber = 1000.0f;
|
|
|
if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
|
|
|
{
|
|
|
// K is safe to invert.
|
|
|
- cc->K.col1.Set(k11, k12);
|
|
|
- cc->K.col2.Set(k12, k22);
|
|
|
- cc->normalMass = cc->K.GetInverse();
|
|
|
+ vc->K.ex.Set(k11, k12);
|
|
|
+ vc->K.ey.Set(k12, k22);
|
|
|
+ vc->normalMass = vc->K.GetInverse();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// The constraints are redundant, just use one.
|
|
|
// TODO_ERIN use deepest?
|
|
|
- cc->pointCount = 1;
|
|
|
+ vc->pointCount = 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-b2ContactSolver::~b2ContactSolver()
|
|
|
-{
|
|
|
- m_allocator->Free(m_constraints);
|
|
|
-}
|
|
|
-
|
|
|
void b2ContactSolver::WarmStart()
|
|
|
{
|
|
|
// Warm start.
|
|
|
- for (int32 i = 0; i < m_constraintCount; ++i)
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
{
|
|
|
- b2ContactConstraint* c = m_constraints + i;
|
|
|
-
|
|
|
- b2Body* bodyA = c->bodyA;
|
|
|
- b2Body* bodyB = c->bodyB;
|
|
|
- float32 invMassA = bodyA->m_invMass;
|
|
|
- float32 invIA = bodyA->m_invI;
|
|
|
- float32 invMassB = bodyB->m_invMass;
|
|
|
- float32 invIB = bodyB->m_invI;
|
|
|
- b2Vec2 normal = c->normal;
|
|
|
+ b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
|
|
|
+
|
|
|
+ int32 indexA = vc->indexA;
|
|
|
+ int32 indexB = vc->indexB;
|
|
|
+ float32 mA = vc->invMassA;
|
|
|
+ float32 iA = vc->invIA;
|
|
|
+ float32 mB = vc->invMassB;
|
|
|
+ float32 iB = vc->invIB;
|
|
|
+ int32 pointCount = vc->pointCount;
|
|
|
+
|
|
|
+ b2Vec2 vA = m_velocities[indexA].v;
|
|
|
+ float32 wA = m_velocities[indexA].w;
|
|
|
+ b2Vec2 vB = m_velocities[indexB].v;
|
|
|
+ float32 wB = m_velocities[indexB].w;
|
|
|
+
|
|
|
+ b2Vec2 normal = vc->normal;
|
|
|
b2Vec2 tangent = b2Cross(normal, 1.0f);
|
|
|
|
|
|
- for (int32 j = 0; j < c->pointCount; ++j)
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
{
|
|
|
- b2ContactConstraintPoint* ccp = c->points + j;
|
|
|
- b2Vec2 P = ccp->normalImpulse * normal + ccp->tangentImpulse * tangent;
|
|
|
- bodyA->m_angularVelocity -= invIA * b2Cross(ccp->rA, P);
|
|
|
- bodyA->m_linearVelocity -= invMassA * P;
|
|
|
- bodyB->m_angularVelocity += invIB * b2Cross(ccp->rB, P);
|
|
|
- bodyB->m_linearVelocity += invMassB * P;
|
|
|
+ b2VelocityConstraintPoint* vcp = vc->points + j;
|
|
|
+ b2Vec2 P = vcp->normalImpulse * normal + vcp->tangentImpulse * tangent;
|
|
|
+ wA -= iA * b2Cross(vcp->rA, P);
|
|
|
+ vA -= mA * P;
|
|
|
+ wB += iB * b2Cross(vcp->rB, P);
|
|
|
+ vB += mB * P;
|
|
|
}
|
|
|
+
|
|
|
+ m_velocities[indexA].v = vA;
|
|
|
+ m_velocities[indexA].w = wA;
|
|
|
+ m_velocities[indexB].v = vB;
|
|
|
+ m_velocities[indexB].w = wB;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void b2ContactSolver::SolveVelocityConstraints()
|
|
|
{
|
|
|
- for (int32 i = 0; i < m_constraintCount; ++i)
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
{
|
|
|
- b2ContactConstraint* c = m_constraints + i;
|
|
|
- b2Body* bodyA = c->bodyA;
|
|
|
- b2Body* bodyB = c->bodyB;
|
|
|
- float32 wA = bodyA->m_angularVelocity;
|
|
|
- float32 wB = bodyB->m_angularVelocity;
|
|
|
- b2Vec2 vA = bodyA->m_linearVelocity;
|
|
|
- b2Vec2 vB = bodyB->m_linearVelocity;
|
|
|
- float32 invMassA = bodyA->m_invMass;
|
|
|
- float32 invIA = bodyA->m_invI;
|
|
|
- float32 invMassB = bodyB->m_invMass;
|
|
|
- float32 invIB = bodyB->m_invI;
|
|
|
- b2Vec2 normal = c->normal;
|
|
|
+ b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
|
|
|
+
|
|
|
+ int32 indexA = vc->indexA;
|
|
|
+ int32 indexB = vc->indexB;
|
|
|
+ float32 mA = vc->invMassA;
|
|
|
+ float32 iA = vc->invIA;
|
|
|
+ float32 mB = vc->invMassB;
|
|
|
+ float32 iB = vc->invIB;
|
|
|
+ int32 pointCount = vc->pointCount;
|
|
|
+
|
|
|
+ b2Vec2 vA = m_velocities[indexA].v;
|
|
|
+ float32 wA = m_velocities[indexA].w;
|
|
|
+ b2Vec2 vB = m_velocities[indexB].v;
|
|
|
+ float32 wB = m_velocities[indexB].w;
|
|
|
+
|
|
|
+ b2Vec2 normal = vc->normal;
|
|
|
b2Vec2 tangent = b2Cross(normal, 1.0f);
|
|
|
- float32 friction = c->friction;
|
|
|
+ float32 friction = vc->friction;
|
|
|
|
|
|
- b2Assert(c->pointCount == 1 || c->pointCount == 2);
|
|
|
+ b2Assert(pointCount == 1 || pointCount == 2);
|
|
|
|
|
|
- // Solve tangent constraints
|
|
|
- for (int32 j = 0; j < c->pointCount; ++j)
|
|
|
+ // Solve tangent constraints first because non-penetration is more important
|
|
|
+ // than friction.
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
{
|
|
|
- b2ContactConstraintPoint* ccp = c->points + j;
|
|
|
+ b2VelocityConstraintPoint* vcp = vc->points + j;
|
|
|
|
|
|
// Relative velocity at contact
|
|
|
- b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
|
|
|
+ b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
|
|
|
|
|
|
// Compute tangent force
|
|
|
float32 vt = b2Dot(dv, tangent);
|
|
|
- float32 lambda = ccp->tangentMass * (-vt);
|
|
|
+ float32 lambda = vcp->tangentMass * (-vt);
|
|
|
|
|
|
// b2Clamp the accumulated force
|
|
|
- float32 maxFriction = friction * ccp->normalImpulse;
|
|
|
- float32 newImpulse = b2Clamp(ccp->tangentImpulse + lambda, -maxFriction, maxFriction);
|
|
|
- lambda = newImpulse - ccp->tangentImpulse;
|
|
|
+ float32 maxFriction = friction * vcp->normalImpulse;
|
|
|
+ float32 newImpulse = b2Clamp(vcp->tangentImpulse + lambda, -maxFriction, maxFriction);
|
|
|
+ lambda = newImpulse - vcp->tangentImpulse;
|
|
|
+ vcp->tangentImpulse = newImpulse;
|
|
|
|
|
|
// Apply contact impulse
|
|
|
b2Vec2 P = lambda * tangent;
|
|
|
|
|
|
- vA -= invMassA * P;
|
|
|
- wA -= invIA * b2Cross(ccp->rA, P);
|
|
|
-
|
|
|
- vB += invMassB * P;
|
|
|
- wB += invIB * b2Cross(ccp->rB, P);
|
|
|
+ vA -= mA * P;
|
|
|
+ wA -= iA * b2Cross(vcp->rA, P);
|
|
|
|
|
|
- ccp->tangentImpulse = newImpulse;
|
|
|
+ vB += mB * P;
|
|
|
+ wB += iB * b2Cross(vcp->rB, P);
|
|
|
}
|
|
|
|
|
|
// Solve normal constraints
|
|
|
- if (c->pointCount == 1)
|
|
|
+ if (vc->pointCount == 1)
|
|
|
{
|
|
|
- b2ContactConstraintPoint* ccp = c->points + 0;
|
|
|
+ b2VelocityConstraintPoint* vcp = vc->points + 0;
|
|
|
|
|
|
// Relative velocity at contact
|
|
|
- b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);
|
|
|
+ b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA);
|
|
|
|
|
|
// Compute normal impulse
|
|
|
float32 vn = b2Dot(dv, normal);
|
|
|
- float32 lambda = -ccp->normalMass * (vn - ccp->velocityBias);
|
|
|
+ float32 lambda = -vcp->normalMass * (vn - vcp->velocityBias);
|
|
|
|
|
|
// b2Clamp the accumulated impulse
|
|
|
- float32 newImpulse = b2Max(ccp->normalImpulse + lambda, 0.0f);
|
|
|
- lambda = newImpulse - ccp->normalImpulse;
|
|
|
+ float32 newImpulse = b2Max(vcp->normalImpulse + lambda, 0.0f);
|
|
|
+ lambda = newImpulse - vcp->normalImpulse;
|
|
|
+ vcp->normalImpulse = newImpulse;
|
|
|
|
|
|
// Apply contact impulse
|
|
|
b2Vec2 P = lambda * normal;
|
|
|
- vA -= invMassA * P;
|
|
|
- wA -= invIA * b2Cross(ccp->rA, P);
|
|
|
+ vA -= mA * P;
|
|
|
+ wA -= iA * b2Cross(vcp->rA, P);
|
|
|
|
|
|
- vB += invMassB * P;
|
|
|
- wB += invIB * b2Cross(ccp->rB, P);
|
|
|
- ccp->normalImpulse = newImpulse;
|
|
|
+ vB += mB * P;
|
|
|
+ wB += iB * b2Cross(vcp->rB, P);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -272,7 +372,7 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
// vn = A * x + b, vn >= 0, , vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
|
|
|
//
|
|
|
// A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
|
|
|
- // b = vn_0 - velocityBias
|
|
|
+ // b = vn0 - velocityBias
|
|
|
//
|
|
|
// The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
|
|
|
// implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
|
|
@@ -284,18 +384,23 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
//
|
|
|
// Substitute:
|
|
|
//
|
|
|
- // x = x' - a
|
|
|
+ // x = a + d
|
|
|
//
|
|
|
- // Plug into above equation:
|
|
|
+ // a := old total impulse
|
|
|
+ // x := new total impulse
|
|
|
+ // d := incremental impulse
|
|
|
//
|
|
|
- // vn = A * x + b
|
|
|
- // = A * (x' - a) + b
|
|
|
- // = A * x' + b - A * a
|
|
|
- // = A * x' + b'
|
|
|
+ // For the current iteration we extend the formula for the incremental impulse
|
|
|
+ // to compute the new total impulse:
|
|
|
+ //
|
|
|
+ // vn = A * d + b
|
|
|
+ // = A * (x - a) + b
|
|
|
+ // = A * x + b - A * a
|
|
|
+ // = A * x + b'
|
|
|
// b' = b - A * a;
|
|
|
|
|
|
- b2ContactConstraintPoint* cp1 = c->points + 0;
|
|
|
- b2ContactConstraintPoint* cp2 = c->points + 1;
|
|
|
+ b2VelocityConstraintPoint* cp1 = vc->points + 0;
|
|
|
+ b2VelocityConstraintPoint* cp2 = vc->points + 1;
|
|
|
|
|
|
b2Vec2 a(cp1->normalImpulse, cp2->normalImpulse);
|
|
|
b2Assert(a.x >= 0.0f && a.y >= 0.0f);
|
|
@@ -311,7 +416,9 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
b2Vec2 b;
|
|
|
b.x = vn1 - cp1->velocityBias;
|
|
|
b.y = vn2 - cp2->velocityBias;
|
|
|
- b -= b2Mul(c->K, a);
|
|
|
+
|
|
|
+ // Compute b'
|
|
|
+ b -= b2Mul(vc->K, a);
|
|
|
|
|
|
const float32 k_errorTol = 1e-3f;
|
|
|
B2_NOT_USED(k_errorTol);
|
|
@@ -321,27 +428,27 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
//
|
|
|
// Case 1: vn = 0
|
|
|
//
|
|
|
- // 0 = A * x' + b'
|
|
|
+ // 0 = A * x + b'
|
|
|
//
|
|
|
- // Solve for x':
|
|
|
+ // Solve for x:
|
|
|
//
|
|
|
- // x' = - inv(A) * b'
|
|
|
+ // x = - inv(A) * b'
|
|
|
//
|
|
|
- b2Vec2 x = - b2Mul(c->normalMass, b);
|
|
|
+ b2Vec2 x = - b2Mul(vc->normalMass, b);
|
|
|
|
|
|
if (x.x >= 0.0f && x.y >= 0.0f)
|
|
|
{
|
|
|
- // Resubstitute for the incremental impulse
|
|
|
+ // Get the incremental impulse
|
|
|
b2Vec2 d = x - a;
|
|
|
|
|
|
// Apply incremental impulse
|
|
|
b2Vec2 P1 = d.x * normal;
|
|
|
b2Vec2 P2 = d.y * normal;
|
|
|
- vA -= invMassA * (P1 + P2);
|
|
|
- wA -= invIA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
+ vA -= mA * (P1 + P2);
|
|
|
+ wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
|
|
|
- vB += invMassB * (P1 + P2);
|
|
|
- wB += invIB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
+ vB += mB * (P1 + P2);
|
|
|
+ wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
|
|
|
// Accumulate
|
|
|
cp1->normalImpulse = x.x;
|
|
@@ -365,27 +472,27 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
//
|
|
|
// Case 2: vn1 = 0 and x2 = 0
|
|
|
//
|
|
|
- // 0 = a11 * x1' + a12 * 0 + b1'
|
|
|
- // vn2 = a21 * x1' + a22 * 0 + b2'
|
|
|
+ // 0 = a11 * x1 + a12 * 0 + b1'
|
|
|
+ // vn2 = a21 * x1 + a22 * 0 + b2'
|
|
|
//
|
|
|
x.x = - cp1->normalMass * b.x;
|
|
|
x.y = 0.0f;
|
|
|
vn1 = 0.0f;
|
|
|
- vn2 = c->K.col1.y * x.x + b.y;
|
|
|
+ vn2 = vc->K.ex.y * x.x + b.y;
|
|
|
|
|
|
if (x.x >= 0.0f && vn2 >= 0.0f)
|
|
|
{
|
|
|
- // Resubstitute for the incremental impulse
|
|
|
+ // Get the incremental impulse
|
|
|
b2Vec2 d = x - a;
|
|
|
|
|
|
// Apply incremental impulse
|
|
|
b2Vec2 P1 = d.x * normal;
|
|
|
b2Vec2 P2 = d.y * normal;
|
|
|
- vA -= invMassA * (P1 + P2);
|
|
|
- wA -= invIA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
+ vA -= mA * (P1 + P2);
|
|
|
+ wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
|
|
|
- vB += invMassB * (P1 + P2);
|
|
|
- wB += invIB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
+ vB += mB * (P1 + P2);
|
|
|
+ wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
|
|
|
// Accumulate
|
|
|
cp1->normalImpulse = x.x;
|
|
@@ -407,12 +514,12 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
//
|
|
|
// Case 3: vn2 = 0 and x1 = 0
|
|
|
//
|
|
|
- // vn1 = a11 * 0 + a12 * x2' + b1'
|
|
|
- // 0 = a21 * 0 + a22 * x2' + b2'
|
|
|
+ // vn1 = a11 * 0 + a12 * x2 + b1'
|
|
|
+ // 0 = a21 * 0 + a22 * x2 + b2'
|
|
|
//
|
|
|
x.x = 0.0f;
|
|
|
x.y = - cp2->normalMass * b.y;
|
|
|
- vn1 = c->K.col2.x * x.y + b.x;
|
|
|
+ vn1 = vc->K.ey.x * x.y + b.x;
|
|
|
vn2 = 0.0f;
|
|
|
|
|
|
if (x.y >= 0.0f && vn1 >= 0.0f)
|
|
@@ -423,11 +530,11 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
// Apply incremental impulse
|
|
|
b2Vec2 P1 = d.x * normal;
|
|
|
b2Vec2 P2 = d.y * normal;
|
|
|
- vA -= invMassA * (P1 + P2);
|
|
|
- wA -= invIA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
+ vA -= mA * (P1 + P2);
|
|
|
+ wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
|
|
|
- vB += invMassB * (P1 + P2);
|
|
|
- wB += invIB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
+ vB += mB * (P1 + P2);
|
|
|
+ wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
|
|
|
// Accumulate
|
|
|
cp1->normalImpulse = x.x;
|
|
@@ -463,11 +570,11 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
// Apply incremental impulse
|
|
|
b2Vec2 P1 = d.x * normal;
|
|
|
b2Vec2 P2 = d.y * normal;
|
|
|
- vA -= invMassA * (P1 + P2);
|
|
|
- wA -= invIA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
+ vA -= mA * (P1 + P2);
|
|
|
+ wA -= iA * (b2Cross(cp1->rA, P1) + b2Cross(cp2->rA, P2));
|
|
|
|
|
|
- vB += invMassB * (P1 + P2);
|
|
|
- wB += invIB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
+ vB += mB * (P1 + P2);
|
|
|
+ wB += iB * (b2Cross(cp1->rB, P1) + b2Cross(cp2->rB, P2));
|
|
|
|
|
|
// Accumulate
|
|
|
cp1->normalImpulse = x.x;
|
|
@@ -481,73 +588,65 @@ void b2ContactSolver::SolveVelocityConstraints()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- bodyA->m_linearVelocity = vA;
|
|
|
- bodyA->m_angularVelocity = wA;
|
|
|
- bodyB->m_linearVelocity = vB;
|
|
|
- bodyB->m_angularVelocity = wB;
|
|
|
+ m_velocities[indexA].v = vA;
|
|
|
+ m_velocities[indexA].w = wA;
|
|
|
+ m_velocities[indexB].v = vB;
|
|
|
+ m_velocities[indexB].w = wB;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void b2ContactSolver::StoreImpulses()
|
|
|
{
|
|
|
- for (int32 i = 0; i < m_constraintCount; ++i)
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
{
|
|
|
- b2ContactConstraint* c = m_constraints + i;
|
|
|
- b2Manifold* m = c->manifold;
|
|
|
+ b2ContactVelocityConstraint* vc = m_velocityConstraints + i;
|
|
|
+ b2Manifold* manifold = m_contacts[vc->contactIndex]->GetManifold();
|
|
|
|
|
|
- for (int32 j = 0; j < c->pointCount; ++j)
|
|
|
+ for (int32 j = 0; j < vc->pointCount; ++j)
|
|
|
{
|
|
|
- m->points[j].normalImpulse = c->points[j].normalImpulse;
|
|
|
- m->points[j].tangentImpulse = c->points[j].tangentImpulse;
|
|
|
+ manifold->points[j].normalImpulse = vc->points[j].normalImpulse;
|
|
|
+ manifold->points[j].tangentImpulse = vc->points[j].tangentImpulse;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
struct b2PositionSolverManifold
|
|
|
{
|
|
|
- void Initialize(b2ContactConstraint* cc, int32 index)
|
|
|
+ void Initialize(b2ContactPositionConstraint* pc, const b2Transform& xfA, const b2Transform& xfB, int32 index)
|
|
|
{
|
|
|
- b2Assert(cc->pointCount > 0);
|
|
|
+ b2Assert(pc->pointCount > 0);
|
|
|
|
|
|
- switch (cc->type)
|
|
|
+ switch (pc->type)
|
|
|
{
|
|
|
case b2Manifold::e_circles:
|
|
|
{
|
|
|
- b2Vec2 pointA = cc->bodyA->GetWorldPoint(cc->localPoint);
|
|
|
- b2Vec2 pointB = cc->bodyB->GetWorldPoint(cc->points[0].localPoint);
|
|
|
- if (b2DistanceSquared(pointA, pointB) > b2_epsilon * b2_epsilon)
|
|
|
- {
|
|
|
- normal = pointB - pointA;
|
|
|
- normal.Normalize();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- normal.Set(1.0f, 0.0f);
|
|
|
- }
|
|
|
-
|
|
|
+ b2Vec2 pointA = b2Mul(xfA, pc->localPoint);
|
|
|
+ b2Vec2 pointB = b2Mul(xfB, pc->localPoints[0]);
|
|
|
+ normal = pointB - pointA;
|
|
|
+ normal.Normalize();
|
|
|
point = 0.5f * (pointA + pointB);
|
|
|
- separation = b2Dot(pointB - pointA, normal) - cc->radius;
|
|
|
+ separation = b2Dot(pointB - pointA, normal) - pc->radiusA - pc->radiusB;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case b2Manifold::e_faceA:
|
|
|
{
|
|
|
- normal = cc->bodyA->GetWorldVector(cc->localNormal);
|
|
|
- b2Vec2 planePoint = cc->bodyA->GetWorldPoint(cc->localPoint);
|
|
|
+ normal = b2Mul(xfA.q, pc->localNormal);
|
|
|
+ b2Vec2 planePoint = b2Mul(xfA, pc->localPoint);
|
|
|
|
|
|
- b2Vec2 clipPoint = cc->bodyB->GetWorldPoint(cc->points[index].localPoint);
|
|
|
- separation = b2Dot(clipPoint - planePoint, normal) - cc->radius;
|
|
|
+ b2Vec2 clipPoint = b2Mul(xfB, pc->localPoints[index]);
|
|
|
+ separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
|
|
|
point = clipPoint;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case b2Manifold::e_faceB:
|
|
|
{
|
|
|
- normal = cc->bodyB->GetWorldVector(cc->localNormal);
|
|
|
- b2Vec2 planePoint = cc->bodyB->GetWorldPoint(cc->localPoint);
|
|
|
+ normal = b2Mul(xfB.q, pc->localNormal);
|
|
|
+ b2Vec2 planePoint = b2Mul(xfB, pc->localPoint);
|
|
|
|
|
|
- b2Vec2 clipPoint = cc->bodyA->GetWorldPoint(cc->points[index].localPoint);
|
|
|
- separation = b2Dot(clipPoint - planePoint, normal) - cc->radius;
|
|
|
+ b2Vec2 clipPoint = b2Mul(xfA, pc->localPoints[index]);
|
|
|
+ separation = b2Dot(clipPoint - planePoint, normal) - pc->radiusA - pc->radiusB;
|
|
|
point = clipPoint;
|
|
|
|
|
|
// Ensure normal points from A to B
|
|
@@ -563,58 +662,168 @@ struct b2PositionSolverManifold
|
|
|
};
|
|
|
|
|
|
// Sequential solver.
|
|
|
-bool b2ContactSolver::SolvePositionConstraints(float32 baumgarte)
|
|
|
+bool b2ContactSolver::SolvePositionConstraints()
|
|
|
{
|
|
|
float32 minSeparation = 0.0f;
|
|
|
|
|
|
- for (int32 i = 0; i < m_constraintCount; ++i)
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
{
|
|
|
- b2ContactConstraint* c = m_constraints + i;
|
|
|
- b2Body* bodyA = c->bodyA;
|
|
|
- b2Body* bodyB = c->bodyB;
|
|
|
+ b2ContactPositionConstraint* pc = m_positionConstraints + i;
|
|
|
|
|
|
- float32 invMassA = bodyA->m_mass * bodyA->m_invMass;
|
|
|
- float32 invIA = bodyA->m_mass * bodyA->m_invI;
|
|
|
- float32 invMassB = bodyB->m_mass * bodyB->m_invMass;
|
|
|
- float32 invIB = bodyB->m_mass * bodyB->m_invI;
|
|
|
+ int32 indexA = pc->indexA;
|
|
|
+ int32 indexB = pc->indexB;
|
|
|
+ b2Vec2 localCenterA = pc->localCenterA;
|
|
|
+ float32 mA = pc->invMassA;
|
|
|
+ float32 iA = pc->invIA;
|
|
|
+ b2Vec2 localCenterB = pc->localCenterB;
|
|
|
+ float32 mB = pc->invMassB;
|
|
|
+ float32 iB = pc->invIB;
|
|
|
+ int32 pointCount = pc->pointCount;
|
|
|
+
|
|
|
+ b2Vec2 cA = m_positions[indexA].c;
|
|
|
+ float32 aA = m_positions[indexA].a;
|
|
|
+
|
|
|
+ b2Vec2 cB = m_positions[indexB].c;
|
|
|
+ float32 aB = m_positions[indexB].a;
|
|
|
|
|
|
// Solve normal constraints
|
|
|
- for (int32 j = 0; j < c->pointCount; ++j)
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
{
|
|
|
+ b2Transform xfA, xfB;
|
|
|
+ xfA.q.Set(aA);
|
|
|
+ xfB.q.Set(aB);
|
|
|
+ xfA.p = cA - b2Mul(xfA.q, localCenterA);
|
|
|
+ xfB.p = cB - b2Mul(xfB.q, localCenterB);
|
|
|
+
|
|
|
b2PositionSolverManifold psm;
|
|
|
- psm.Initialize(c, j);
|
|
|
+ psm.Initialize(pc, xfA, xfB, j);
|
|
|
b2Vec2 normal = psm.normal;
|
|
|
|
|
|
b2Vec2 point = psm.point;
|
|
|
float32 separation = psm.separation;
|
|
|
|
|
|
- b2Vec2 rA = point - bodyA->m_sweep.c;
|
|
|
- b2Vec2 rB = point - bodyB->m_sweep.c;
|
|
|
+ b2Vec2 rA = point - cA;
|
|
|
+ b2Vec2 rB = point - cB;
|
|
|
|
|
|
// Track max constraint error.
|
|
|
minSeparation = b2Min(minSeparation, separation);
|
|
|
|
|
|
// Prevent large corrections and allow slop.
|
|
|
- float32 C = b2Clamp(baumgarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
|
|
|
+ float32 C = b2Clamp(b2_baumgarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
|
|
|
|
|
|
// Compute the effective mass.
|
|
|
float32 rnA = b2Cross(rA, normal);
|
|
|
float32 rnB = b2Cross(rB, normal);
|
|
|
- float32 K = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;
|
|
|
+ float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
|
|
|
|
|
|
// Compute normal impulse
|
|
|
float32 impulse = K > 0.0f ? - C / K : 0.0f;
|
|
|
|
|
|
b2Vec2 P = impulse * normal;
|
|
|
|
|
|
- bodyA->m_sweep.c -= invMassA * P;
|
|
|
- bodyA->m_sweep.a -= invIA * b2Cross(rA, P);
|
|
|
- bodyA->SynchronizeTransform();
|
|
|
+ cA -= mA * P;
|
|
|
+ aA -= iA * b2Cross(rA, P);
|
|
|
|
|
|
- bodyB->m_sweep.c += invMassB * P;
|
|
|
- bodyB->m_sweep.a += invIB * b2Cross(rB, P);
|
|
|
- bodyB->SynchronizeTransform();
|
|
|
+ cB += mB * P;
|
|
|
+ aB += iB * b2Cross(rB, P);
|
|
|
}
|
|
|
+
|
|
|
+ m_positions[indexA].c = cA;
|
|
|
+ m_positions[indexA].a = aA;
|
|
|
+
|
|
|
+ m_positions[indexB].c = cB;
|
|
|
+ m_positions[indexB].a = aB;
|
|
|
+ }
|
|
|
+
|
|
|
+ // We can't expect minSpeparation >= -b2_linearSlop because we don't
|
|
|
+ // push the separation above -b2_linearSlop.
|
|
|
+ return minSeparation >= -3.0f * b2_linearSlop;
|
|
|
+}
|
|
|
+
|
|
|
+// Sequential position solver for position constraints.
|
|
|
+bool b2ContactSolver::SolveTOIPositionConstraints(int32 toiIndexA, int32 toiIndexB)
|
|
|
+{
|
|
|
+ float32 minSeparation = 0.0f;
|
|
|
+
|
|
|
+ for (int32 i = 0; i < m_count; ++i)
|
|
|
+ {
|
|
|
+ b2ContactPositionConstraint* pc = m_positionConstraints + i;
|
|
|
+
|
|
|
+ int32 indexA = pc->indexA;
|
|
|
+ int32 indexB = pc->indexB;
|
|
|
+ b2Vec2 localCenterA = pc->localCenterA;
|
|
|
+ b2Vec2 localCenterB = pc->localCenterB;
|
|
|
+ int32 pointCount = pc->pointCount;
|
|
|
+
|
|
|
+ float32 mA = 0.0f;
|
|
|
+ float32 iA = 0.0f;
|
|
|
+ if (indexA == toiIndexA || indexA == toiIndexB)
|
|
|
+ {
|
|
|
+ mA = pc->invMassA;
|
|
|
+ iA = pc->invIA;
|
|
|
+ }
|
|
|
+
|
|
|
+ float32 mB = pc->invMassB;
|
|
|
+ float32 iB = pc->invIB;
|
|
|
+ if (indexB == toiIndexA || indexB == toiIndexB)
|
|
|
+ {
|
|
|
+ mB = pc->invMassB;
|
|
|
+ iB = pc->invIB;
|
|
|
+ }
|
|
|
+
|
|
|
+ b2Vec2 cA = m_positions[indexA].c;
|
|
|
+ float32 aA = m_positions[indexA].a;
|
|
|
+
|
|
|
+ b2Vec2 cB = m_positions[indexB].c;
|
|
|
+ float32 aB = m_positions[indexB].a;
|
|
|
+
|
|
|
+ // Solve normal constraints
|
|
|
+ for (int32 j = 0; j < pointCount; ++j)
|
|
|
+ {
|
|
|
+ b2Transform xfA, xfB;
|
|
|
+ xfA.q.Set(aA);
|
|
|
+ xfB.q.Set(aB);
|
|
|
+ xfA.p = cA - b2Mul(xfA.q, localCenterA);
|
|
|
+ xfB.p = cB - b2Mul(xfB.q, localCenterB);
|
|
|
+
|
|
|
+ b2PositionSolverManifold psm;
|
|
|
+ psm.Initialize(pc, xfA, xfB, j);
|
|
|
+ b2Vec2 normal = psm.normal;
|
|
|
+
|
|
|
+ b2Vec2 point = psm.point;
|
|
|
+ float32 separation = psm.separation;
|
|
|
+
|
|
|
+ b2Vec2 rA = point - cA;
|
|
|
+ b2Vec2 rB = point - cB;
|
|
|
+
|
|
|
+ // Track max constraint error.
|
|
|
+ minSeparation = b2Min(minSeparation, separation);
|
|
|
+
|
|
|
+ // Prevent large corrections and allow slop.
|
|
|
+ float32 C = b2Clamp(b2_toiBaugarte * (separation + b2_linearSlop), -b2_maxLinearCorrection, 0.0f);
|
|
|
+
|
|
|
+ // Compute the effective mass.
|
|
|
+ float32 rnA = b2Cross(rA, normal);
|
|
|
+ float32 rnB = b2Cross(rB, normal);
|
|
|
+ float32 K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
|
|
|
+
|
|
|
+ // Compute normal impulse
|
|
|
+ float32 impulse = K > 0.0f ? - C / K : 0.0f;
|
|
|
+
|
|
|
+ b2Vec2 P = impulse * normal;
|
|
|
+
|
|
|
+ cA -= mA * P;
|
|
|
+ aA -= iA * b2Cross(rA, P);
|
|
|
+
|
|
|
+ cB += mB * P;
|
|
|
+ aB += iB * b2Cross(rB, P);
|
|
|
+ }
|
|
|
+
|
|
|
+ m_positions[indexA].c = cA;
|
|
|
+ m_positions[indexA].a = aA;
|
|
|
+
|
|
|
+ m_positions[indexB].c = cB;
|
|
|
+ m_positions[indexB].a = aB;
|
|
|
}
|
|
|
|
|
|
// We can't expect minSpeparation >= -b2_linearSlop because we don't
|