public b2Vec2 GetSearchDirection() { switch (m_count) { case 1: return(-m_vertices[0].w); case 2: { b2Vec2 e12 = m_vertices[1].w - m_vertices[0].w; float sgn = b2Math.b2Cross(e12, -m_vertices[0].w); if (sgn > 0.0f) { // Origin is left of e12. return(e12.NegUnitCross()); // b2Math.b2Cross(1.0f, e12); } else { // Origin is right of e12. return(e12.UnitCross()); // b2Math.b2Cross(e12, 1.0f); } } default: Debug.Assert(false); return(b2Vec2.Zero); } }
public virtual void WarmStart() { // Warm start. for (int i = 0; i < m_count; ++i) { b2ContactVelocityConstraint vc = m_velocityConstraints[i]; int indexA = vc.indexA; int indexB = vc.indexB; float mA = vc.invMassA; float iA = vc.invIA; float mB = vc.invMassB; float iB = vc.invIB; int pointCount = vc.pointCount; b2Vec2 vA = m_velocities[indexA].v; float wA = m_velocities[indexA].w; b2Vec2 vB = m_velocities[indexB].v; float wB = m_velocities[indexB].w; b2Vec2 normal = vc.normal; b2Vec2 tangent = normal.UnitCross(); // b2Math.b2Cross(normal, 1.0f); for (int j = 0; j < pointCount; ++j) { b2VelocityConstraintPoint vcp = vc.points[j]; b2Vec2 P = vcp.normalImpulse * normal + vcp.tangentImpulse * tangent; wA -= iA * b2Math.b2Cross(ref vcp.rA, ref P); vA -= mA * P; wB += iB * b2Math.b2Cross(ref vcp.rB, ref P); vB += mB * P; } m_velocities[indexA].v = vA; m_velocities[indexA].w = wA; m_velocities[indexB].v = vB; m_velocities[indexB].w = wB; } }
public virtual void SolveVelocityConstraints() { for (int i = 0; i < m_count; ++i) { b2ContactVelocityConstraint vc = m_velocityConstraints[i]; int indexA = vc.indexA; int indexB = vc.indexB; float mA = vc.invMassA; float iA = vc.invIA; float mB = vc.invMassB; float iB = vc.invIB; int pointCount = vc.pointCount; b2Vec2 vA = m_velocities[indexA].v; float wA = m_velocities[indexA].w; b2Vec2 vB = m_velocities[indexB].v; float wB = m_velocities[indexB].w; b2Vec2 normal = vc.normal; b2Vec2 tangent = normal.UnitCross(); // b2Math.b2Cross(normal, 1.0f); float friction = vc.friction; Debug.Assert(pointCount == 1 || pointCount == 2); // Solve tangent constraints first because non-penetration is more important // than friction. for (int j = 0; j < pointCount; ++j) { b2VelocityConstraintPoint vcp = vc.points[j]; // Relative velocity at contact /* * b.m_x = -s * a.m_y; * b.m_y = s * a.m_x; */ // b2Vec2 dv = vB + b2Math.b2Cross(wB, ref vcp.rB) - vA - b2Math.b2Cross(wA, ref vcp.rA); b2Vec2 dv; dv.x = vB.x + (-wB * vcp.rB.y) - vA.x - (-wA * vcp.rA.y); dv.y = vB.y + (wB * vcp.rB.x) - vA.y - (wA * vcp.rA.x); // Compute tangent force float vt = dv.x * tangent.x + dv.y * tangent.y; // b2Math.b2Dot(dv, tangent); float lambda = vcp.tangentMass * (-vt); // b2Math.b2Clamp the accumulated force float maxFriction = friction * vcp.normalImpulse; float newImpulse = b2Math.b2Clamp(vcp.tangentImpulse + lambda, -maxFriction, maxFriction); lambda = newImpulse - vcp.tangentImpulse; vcp.tangentImpulse = newImpulse; // Apply contact impulse // P = lambda * tangent; b2Vec2 P; P.x = lambda * tangent.x; P.y = lambda * tangent.y; // vA -= mA * P; vA.x -= mA * P.x; vA.y -= mA * P.y; // wA -= iA * b2Math.b2Cross(vcp.rA, P); wA -= iA * (vcp.rA.x * P.y - vcp.rA.y * P.x); // vB += mB * P; vB.x += mB * P.x; vB.y += mB * P.y; // wB += iB * b2Math.b2Cross(vcp.rB, P); wB += iB * (vcp.rB.x * P.y - vcp.rB.y * P.x); //vc.points[j] = vcp; } // Solve normal constraints if (vc.pointCount == 1) { b2VelocityConstraintPoint vcp = vc.points[0]; // Relative velocity at contact // b2Vec2 dv = vB + b2Math.b2Cross(wB, ref vcp.rB) - vA - b2Math.b2Cross(wA, ref vcp.rA); b2Vec2 dv; dv.x = vB.x + (-wB * vcp.rB.y) - vA.x - (-wA * vcp.rA.y); dv.y = vB.y + (wB * vcp.rB.x) - vA.y - (wA * vcp.rA.x); // Compute normal impulse float vn = dv.x * normal.x + dv.y * normal.y; //b2Math.b2Dot(ref dv, ref normal); float lambda = -vcp.normalMass * (vn - vcp.velocityBias); // b2Math.b2Clamp the accumulated impulse float newImpulse = Math.Max(vcp.normalImpulse + lambda, 0.0f); lambda = newImpulse - vcp.normalImpulse; vcp.normalImpulse = newImpulse; // Apply contact impulse //b2Vec2 P = lambda * normal; b2Vec2 P; P.x = lambda * normal.x; P.y = lambda * normal.y; // vA -= mA * P; vA.x -= mA * P.x; vA.y -= mA * P.y; // wA -= iA * b2Math.b2Cross(vcp.rA, P); wA -= iA * (vcp.rA.x * P.y - vcp.rA.y * P.x); // vB += mB * P; vB.x += mB * P.x; vB.y += mB * P.y; // wB += iB * b2Math.b2Cross(vcp.rB, P); wB += iB * (vcp.rB.x * P.y - vcp.rB.y * P.x); //vc.points[0] = vcp; } else { // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite). // Build the mini LCP for this contact patch // // 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 = 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 // 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 // solution that satisfies the problem is chosen. // // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i). // // Substitute: // // x = a + d // // a := old total impulse // x := new total impulse // d := incremental impulse // // 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; b2VelocityConstraintPoint cp1 = vc.points[0]; b2VelocityConstraintPoint cp2 = vc.points[1]; b2Vec2 a = new b2Vec2(cp1.normalImpulse, cp2.normalImpulse); Debug.Assert(a.x >= 0.0f && a.y >= 0.0f); // Relative velocity at contact // vB + b2Math.b2Cross(wB, ref cp1.rB) - vA - b2Math.b2Cross(wA, ref cp1.rA); b2Vec2 dv1; dv1.x = vB.x + (-wB * cp1.rB.y) - vA.x - (-wA * cp1.rA.y); dv1.y = vB.y + (wB * cp1.rB.x) - vA.y - (wA * cp1.rA.x); // vB + b2Math.b2Cross(wB, ref cp2.rB) - vA - b2Math.b2Cross(wA, ref cp2.rA); b2Vec2 dv2; dv2.x = vB.x + (-wB * cp2.rB.y) - vA.x - (-wA * cp2.rA.y); dv2.y = vB.y + (wB * cp2.rB.x) - vA.y - (wA * cp2.rA.x); // Compute normal velocity float vn1 = dv1.x * normal.x + dv1.y * normal.y; // b2Math.b2Dot(ref dv1, ref normal); float vn2 = dv2.x * normal.x + dv2.y * normal.y; // b2Math.b2Dot(ref dv2, ref normal); b2Vec2 b; b.x = vn1 - cp1.velocityBias; b.y = vn2 - cp2.velocityBias; // Compute b' // (A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y) b.x -= (vc.K.ex.x * a.x + vc.K.ey.x * a.y); b.y -= (vc.K.ex.y * a.x + vc.K.ey.y * a.y); // b -= b2Math.b2Mul(vc.K, a); // float k_errorTol = 1e-3f; #region Iteration while (true) { // // Case 1: vn = 0 // // 0 = A * x + b' // // Solve for x: // // x = - inv(A) * b' // b2Vec2 x = -b2Math.b2Mul(ref vc.normalMass, ref b); if (x.x >= 0.0f && x.y >= 0.0f) { // Get the incremental impulse b2Vec2 d = x - a; // Apply incremental impulse b2Vec2 P1 = d.x * normal; b2Vec2 P2 = d.y * normal; vA -= mA * (P1 + P2); wA -= iA * (b2Math.b2Cross(ref cp1.rA, ref P1) + b2Math.b2Cross(ref cp2.rA, ref P2)); vB += mB * (P1 + P2); wB += iB * (b2Math.b2Cross(ref cp1.rB, ref P1) + b2Math.b2Cross(ref cp2.rB, ref P2)); // Accumulate cp1.normalImpulse = x.x; cp2.normalImpulse = x.y; #if B2_DEBUG_SOLVER // Postconditions dv1 = vB + b2Math.b2Cross(wB, cp1.rB) - vA - b2Math.b2Cross(wA, cp1.rA); dv2 = vB + b2Math.b2Cross(wB, cp2.rB) - vA - b2Math.b2Cross(wA, cp2.rA); // Compute normal velocity vn1 = b2Math.b2Dot(dv1, normal); vn2 = b2Math.b2Dot(dv2, normal); Debug.Assert(b2Abs(vn1 - cp1.velocityBias) < k_errorTol); Debug.Assert(b2Abs(vn2 - cp2.velocityBias) < k_errorTol); #endif break; } // // Case 2: vn1 = 0 and x2 = 0 // // 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 = vc.K.ex.y * x.x + b.y; if (x.x >= 0.0f && vn2 >= 0.0f) { // Get the incremental impulse b2Vec2 d = x - a; // Apply incremental impulse b2Vec2 P1 = d.x * normal; b2Vec2 P2 = d.y * normal; vA -= mA * (P1 + P2); wA -= iA * (b2Math.b2Cross(ref cp1.rA, ref P1) + b2Math.b2Cross(ref cp2.rA, ref P2)); vB += mB * (P1 + P2); wB += iB * (b2Math.b2Cross(ref cp1.rB, ref P1) + b2Math.b2Cross(ref cp2.rB, ref P2)); // Accumulate cp1.normalImpulse = x.x; cp2.normalImpulse = x.y; #if B2_DEBUG_SOLVER // Postconditions dv1 = vB + b2Math.b2Cross(wB, cp1.rB) - vA - b2Math.b2Cross(wA, cp1.rA); // Compute normal velocity vn1 = b2Math.b2Dot(dv1, normal); Debug.Assert(b2Abs(vn1 - cp1.velocityBias) < k_errorTol); #endif break; } // // Case 3: vn2 = 0 and x1 = 0 // // vn1 = a11 * 0 + a12 * x2 + b1' // 0 = a21 * 0 + a22 * x2 + b2' // x.x = 0.0f; x.y = -cp2.normalMass * b.y; vn1 = vc.K.ey.x * x.y + b.x; vn2 = 0.0f; if (x.y >= 0.0f && vn1 >= 0.0f) { // Resubstitute for the incremental impulse b2Vec2 d = x - a; // Apply incremental impulse b2Vec2 P1 = d.x * normal; b2Vec2 P2 = d.y * normal; vA -= mA * (P1 + P2); wA -= iA * (b2Math.b2Cross(ref cp1.rA, ref P1) + b2Math.b2Cross(ref cp2.rA, ref P2)); vB += mB * (P1 + P2); wB += iB * (b2Math.b2Cross(ref cp1.rB, ref P1) + b2Math.b2Cross(ref cp2.rB, ref P2)); // Accumulate cp1.normalImpulse = x.x; cp2.normalImpulse = x.y; #if B2_DEBUG_SOLVER // Postconditions dv2 = vB + b2Math.b2Cross(wB, cp2.rB) - vA - b2Math.b2Cross(wA, cp2.rA); // Compute normal velocity vn2 = b2Math.b2Dot(dv2, normal); Debug.Assert(b2Abs(vn2 - cp2.velocityBias) < k_errorTol); #endif break; } // // Case 4: x1 = 0 and x2 = 0 // // vn1 = b1 // vn2 = b2; x.x = 0.0f; x.y = 0.0f; vn1 = b.x; vn2 = b.y; if (vn1 >= 0.0f && vn2 >= 0.0f) { // Resubstitute for the incremental impulse b2Vec2 d = x - a; // Apply incremental impulse b2Vec2 P1 = d.x * normal; b2Vec2 P2 = d.y * normal; vA -= mA * (P1 + P2); wA -= iA * (b2Math.b2Cross(ref cp1.rA, ref P1) + b2Math.b2Cross(ref cp2.rA, ref P2)); vB += mB * (P1 + P2); wB += iB * (b2Math.b2Cross(ref cp1.rB, ref P1) + b2Math.b2Cross(ref cp2.rB, ref P2)); // Accumulate cp1.normalImpulse = x.x; cp2.normalImpulse = x.y; break; } // No solution, give up. This is hit sometimes, but it doesn't seem to matter. break; } #endregion //vc.points[0] = cp1; //vc.points[1] = cp2; } m_velocities[indexA].v = vA; m_velocities[indexA].w = wA; m_velocities[indexB].v = vB; m_velocities[indexB].w = wB; //m_velocityConstraints[i] = vc; } }
// TODO_ERIN might not need to return the separation public float Initialize(ref b2SimplexCache cache, b2DistanceProxy proxyA, ref b2Sweep sweepA, b2DistanceProxy proxyB, ref b2Sweep sweepB, float t1) { m_proxyA = proxyA; m_proxyB = proxyB; int count = cache.count; Debug.Assert(0 < count && count < 3); m_sweepA = sweepA; m_sweepB = sweepB; b2Transform xfA, xfB; m_sweepA.GetTransform(out xfA, t1); m_sweepB.GetTransform(out xfB, t1); if (count == 1) { m_type = SeparationType.e_points; b2Vec2 localPointA = m_proxyA.GetVertex((int)cache.indexA[0]); b2Vec2 localPointB = m_proxyB.GetVertex((int)cache.indexB[0]); b2Vec2 pointA = b2Math.b2Mul(xfA, localPointA); b2Vec2 pointB = b2Math.b2Mul(xfB, localPointB); m_axis = pointB - pointA; float s = m_axis.Normalize(); return(s); } else if (cache.indexA[0] == cache.indexA[1]) { // Two points on B and one on A. m_type = SeparationType.e_faceB; b2Vec2 localPointB1 = proxyB.GetVertex((int)cache.indexB[0]); b2Vec2 localPointB2 = proxyB.GetVertex((int)cache.indexB[1]); float b21x = localPointB2.x - localPointB1.x; float b21y = localPointB2.y - localPointB1.y; m_axis.x = -b21y; m_axis.y = b21x; // m_axis = b2Math.b2Cross(localPointB2 - localPointB1, 1.0f); m_axis.Normalize(); b2Vec2 normal = b2Math.b2Mul(xfB.q, m_axis); m_localPoint = 0.5f * (localPointB1 + localPointB2); b2Vec2 pointB = b2Math.b2Mul(xfB, m_localPoint); b2Vec2 localPointA = proxyA.GetVertex((int)cache.indexA[0]); b2Vec2 pointA = b2Math.b2Mul(xfA, localPointA); b2Vec2 aminusb = pointA - pointB; float s = b2Math.b2Dot(ref aminusb, ref normal); if (s < 0.0f) { m_axis = -m_axis; s = -s; } return(s); } else { // Two points on A and one or two points on B. m_type = SeparationType.e_faceA; b2Vec2 localPointA1 = m_proxyA.GetVertex(cache.indexA[0]); b2Vec2 localPointA2 = m_proxyA.GetVertex(cache.indexA[1]); b2Vec2 a2minusa1 = localPointA2 - localPointA1; m_axis = a2minusa1.UnitCross();// b2Math.b2Cross(localPointA2 - localPointA1, 1.0f); m_axis.Normalize(); b2Vec2 normal = b2Math.b2Mul(xfA.q, m_axis); m_localPoint = 0.5f * (localPointA1 + localPointA2); b2Vec2 pointA = b2Math.b2Mul(xfA, m_localPoint); b2Vec2 localPointB = m_proxyB.GetVertex(cache.indexB[0]); b2Vec2 pointB = b2Math.b2Mul(xfB, localPointB); b2Vec2 bminusa = pointB - pointA; float s = b2Math.b2Dot(ref bminusa, ref normal); if (s < 0.0f) { m_axis = -m_axis; s = -s; } return(s); } }