static public b2ContactPositionConstraint Create() { var cvc = new b2ContactPositionConstraint(); cvc.Defaults(); return(cvc); }
public b2PositionSolverManifold(b2ContactPositionConstraint pc, ref b2Transform xfA, ref b2Transform xfB, int index) { Debug.Assert(pc.pointCount > 0); switch (pc.type) { case b2ManifoldType.e_circles: { b2Vec2 pointA = b2Math.b2Mul(xfA, pc.localPoint); b2Vec2 pointB = b2Math.b2Mul(xfB, pc.localPoints[0]); normal = pointB - pointA; normal.Normalize(); point = 0.5f * (pointA + pointB); separation = b2Math.b2Dot(pointB - pointA, normal) - pc.radiusA - pc.radiusB; } break; case b2ManifoldType.e_faceA: { normal = b2Math.b2Mul(xfA.q, pc.localNormal); b2Vec2 planePoint = b2Math.b2Mul(xfA, pc.localPoint); b2Vec2 clipPoint = b2Math.b2Mul(xfB, pc.localPoints[index]); b2Vec2 rCP = clipPoint - planePoint; separation = b2Math.b2Dot(ref rCP, ref normal) - pc.radiusA - pc.radiusB; point = clipPoint; } break; case b2ManifoldType.e_faceB: { normal = b2Math.b2Mul(xfB.q, pc.localNormal); b2Vec2 planePoint = b2Math.b2Mul(xfB, pc.localPoint); b2Vec2 clipPoint = b2Math.b2Mul(xfA, pc.localPoints[index]); b2Vec2 rCP = clipPoint - planePoint; separation = b2Math.b2Dot(ref rCP, ref normal) - pc.radiusA - pc.radiusB; point = clipPoint; // Ensure normal points from A to B normal = -normal; } break; } }
public b2PositionSolverManifold(ref b2ContactPositionConstraint pc, ref b2Transform xfA, ref b2Transform xfB, int index) { Debug.Assert(pc.pointCount > 0); switch (pc.type) { case b2ManifoldType.e_circles: { b2Vec2 pointA = b2Math.b2Mul(xfA, pc.localPoint); b2Vec2 pointB = b2Math.b2Mul(xfB, pc.localPoints[0]); normal = pointB - pointA; normal.Normalize(); point = 0.5f * (pointA + pointB); separation = b2Math.b2Dot(pointB - pointA, normal) - pc.radiusA - pc.radiusB; } break; case b2ManifoldType.e_faceA: { normal = b2Math.b2Mul(xfA.q, pc.localNormal); b2Vec2 planePoint = b2Math.b2Mul(xfA, pc.localPoint); b2Vec2 clipPoint = b2Math.b2Mul(xfB, pc.localPoints[index]); separation = b2Math.b2Dot(clipPoint - planePoint, normal) - pc.radiusA - pc.radiusB; point = clipPoint; } break; case b2ManifoldType.e_faceB: { normal = b2Math.b2Mul(xfB.q, pc.localNormal); b2Vec2 planePoint = b2Math.b2Mul(xfB, pc.localPoint); b2Vec2 clipPoint = b2Math.b2Mul(xfA, pc.localPoints[index]); separation = b2Math.b2Dot(clipPoint - planePoint, normal) - pc.radiusA - pc.radiusB; point = clipPoint; // Ensure normal points from A to B normal = -normal; } break; } }
public static b2ContactPositionConstraint Create() { var cvc = new b2ContactPositionConstraint(); cvc.Defaults(); return cvc; }
// Sequential position solver for position constraints. public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB) { float minSeparation = 0.0f; for (int i = 0; i < m_count; ++i) { b2ContactPositionConstraint pc = m_positionConstraints[i]; int indexA = pc.indexA; int indexB = pc.indexB; b2Vec2 localCenterA = pc.localCenterA; b2Vec2 localCenterB = pc.localCenterB; int pointCount = pc.pointCount; float mA = 0.0f; float iA = 0.0f; if (indexA == toiIndexA || indexA == toiIndexB) { mA = pc.invMassA; iA = pc.invIA; } float mB = pc.invMassB; float iB = pc.invIB; if (indexB == toiIndexA || indexB == toiIndexB) { mB = pc.invMassB; iB = pc.invIB; } b2Vec2 cA = m_positions[indexA].c; float aA = m_positions[indexA].a; b2Vec2 cB = m_positions[indexB].c; float aB = m_positions[indexB].a; // Solve normal constraints for (int j = 0; j < pointCount; ++j) { b2Transform xfA = b2Transform.Identity, xfB = b2Transform.Identity; xfA.q.Set(aA); xfB.q.Set(aB); xfA.p = cA - b2Math.b2Mul(xfA.q, localCenterA); xfB.p = cB - b2Math.b2Mul(xfB.q, localCenterB); b2PositionSolverManifold psm = new b2PositionSolverManifold(pc, ref xfA, ref xfB, j); b2Vec2 normal = psm.normal; b2Vec2 point = psm.point; float separation = psm.separation; b2Vec2 rA = point - cA; b2Vec2 rB = point - cB; // Track max constraint error. minSeparation = Math.Min(minSeparation, separation); // Prevent large corrections and allow slop. float C = b2Math.b2Clamp(b2Settings.b2_toiBaugarte * (separation + b2Settings.b2_linearSlop), -b2Settings.b2_maxLinearCorrection, 0.0f); // Compute the effective mass. float rnA = b2Math.b2Cross(ref rA, ref normal); float rnB = b2Math.b2Cross(ref rB, ref normal); float K = mA + mB + iA * rnA * rnA + iB * rnB * rnB; // Compute normal impulse float impulse = K > 0.0f ? -C / K : 0.0f; b2Vec2 P = impulse * normal; cA -= mA * P; aA -= iA * b2Math.b2Cross(rA, P); cB += mB * P; aB += iB * b2Math.b2Cross(rB, P); } m_positions[indexA].c = cA; m_positions[indexA].a = aA; m_positions[indexB].c = cB; m_positions[indexB].a = aB; //m_positionConstraints[i] = pc; } // We can't expect minSpeparation >= -b2_linearSlop because we don't // push the separation above -b2_linearSlop. return(minSeparation >= -1.5f * b2Settings.b2_linearSlop); }
// Initialize position dependent portions of the velocity constraints. public virtual void InitializeVelocityConstraints() { for (int i = 0; i < m_count; ++i) { b2ContactVelocityConstraint vc = m_velocityConstraints[i]; b2ContactPositionConstraint pc = m_positionConstraints[i]; float radiusA = pc.radiusA; float radiusB = pc.radiusB; b2Manifold manifold = m_contacts[vc.contactIndex].GetManifold(); int indexA = vc.indexA; int indexB = vc.indexB; float mA = vc.invMassA; float mB = vc.invMassB; float iA = vc.invIA; float iB = vc.invIB; b2Vec2 localCenterA = pc.localCenterA; b2Vec2 localCenterB = pc.localCenterB; b2Vec2 cA = m_positions[indexA].c; float aA = m_positions[indexA].a; b2Vec2 vA = m_velocities[indexA].v; float wA = m_velocities[indexA].w; b2Vec2 cB = m_positions[indexB].c; float aB = m_positions[indexB].a; b2Vec2 vB = m_velocities[indexB].v; float wB = m_velocities[indexB].w; Debug.Assert(manifold.pointCount > 0); b2Transform xfA = b2Transform.Identity, xfB = b2Transform.Identity; xfA.q.Set(aA); xfB.q.Set(aB); xfA.p = cA - b2Math.b2Mul(ref xfA.q, ref localCenterA); xfB.p = cB - b2Math.b2Mul(ref xfB.q, ref localCenterB); b2WorldManifold worldManifold = new b2WorldManifold(); worldManifold.Initialize(ref manifold, xfA, radiusA, xfB, radiusB); vc.normal = worldManifold.normal; int pointCount = vc.pointCount; for (int j = 0; j < pointCount; ++j) { b2VelocityConstraintPoint vcp = vc.points[j]; vcp.rA = worldManifold.points[j] - cA; vcp.rB = worldManifold.points[j] - cB; float rnA = b2Math.b2Cross(ref vcp.rA, ref vc.normal); float rnB = b2Math.b2Cross(ref vcp.rB, ref vc.normal); float kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB; vcp.normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f; b2Vec2 tangent = vc.normal.UnitCross(); // b2Math.b2Cross(vc.normal, 1.0f); float rtA = b2Math.b2Cross(ref vcp.rA, ref tangent); float rtB = b2Math.b2Cross(ref vcp.rB, ref tangent); float kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB; vcp.tangentMass = kTangent > 0.0f ? 1.0f / kTangent : 0.0f; // Setup a velocity bias for restitution. vcp.velocityBias = 0.0f; float vRel = b2Math.b2Dot(vc.normal, vB + b2Math.b2Cross(wB, ref vcp.rB) - vA - b2Math.b2Cross(wA, ref vcp.rA)); if (vRel < -b2Settings.b2_velocityThreshold) { vcp.velocityBias = -vc.restitution * vRel; } //vc.points[j] = vcp; } // If we have two points, then prepare the block solver. if (vc.pointCount == 2) { b2VelocityConstraintPoint vcp1 = vc.points[0]; b2VelocityConstraintPoint vcp2 = vc.points[1]; float rn1A = b2Math.b2Cross(ref vcp1.rA, ref vc.normal); float rn1B = b2Math.b2Cross(ref vcp1.rB, ref vc.normal); float rn2A = b2Math.b2Cross(ref vcp2.rA, ref vc.normal); float rn2B = b2Math.b2Cross(ref vcp2.rB, ref vc.normal); float k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B; float k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B; float k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B; // Ensure a reasonable condition number. float k_maxConditionNumber = 1000.0f; if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12)) { // K is safe to invert. 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? vc.pointCount = 1; } } //m_positionConstraints[i] = pc; //m_velocityConstraints[i] = vc; } }
public b2ContactSolver(b2ContactSolverDef def) { m_step = def.step; m_count = def.count; m_positionConstraints = new b2ContactPositionConstraint[m_count]; for (int pc = 0; pc < m_count; pc++) { m_positionConstraints[pc] = b2ContactPositionConstraint.Create(); } m_velocityConstraints = new b2ContactVelocityConstraint[m_count]; for (int vc = 0; vc < m_count; vc++) { m_velocityConstraints[vc] = b2ContactVelocityConstraint.Create(); } m_positions = def.positions; m_velocities = def.velocities; m_contacts = def.contacts; // Initialize position independent portions of the constraints. for (int i = 0; i < m_count; ++i) { b2Contact contact = m_contacts[i]; b2Fixture fixtureA = contact.FixtureA; b2Fixture fixtureB = contact.FixtureB; b2Shape shapeA = fixtureA.Shape; b2Shape shapeB = fixtureB.Shape; float radiusA = shapeA.Radius; float radiusB = shapeB.Radius; b2Body bodyA = fixtureA.Body; b2Body bodyB = fixtureB.Body; b2Manifold manifold = contact.GetManifold(); int pointCount = manifold.pointCount; Debug.Assert(pointCount > 0); b2ContactVelocityConstraint vc = m_velocityConstraints[i]; vc.friction = contact.Friction; vc.restitution = contact.Restitution; vc.indexA = bodyA.IslandIndex; vc.indexB = bodyB.IslandIndex; vc.invMassA = bodyA.InvertedMass; vc.invMassB = bodyB.InvertedMass; vc.invIA = bodyA.InvertedI; vc.invIB = bodyB.InvertedI; vc.contactIndex = i; vc.pointCount = pointCount; vc.K.SetZero(); vc.normalMass.SetZero(); b2ContactPositionConstraint pc = m_positionConstraints[i]; pc.indexA = bodyA.IslandIndex; pc.indexB = bodyB.IslandIndex; pc.invMassA = bodyA.InvertedMass; pc.invMassB = bodyB.InvertedMass; pc.localCenterA = bodyA.Sweep.localCenter; pc.localCenterB = bodyB.Sweep.localCenter; pc.invIA = bodyA.InvertedI; pc.invIB = bodyB.InvertedI; pc.localNormal = manifold.localNormal; pc.localPoint = manifold.localPoint; pc.pointCount = pointCount; pc.radiusA = radiusA; pc.radiusB = radiusB; pc.type = manifold.type; for (int 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; //vc.points[j] = vcp; } //Put back the struct data since struct data is copied by value //m_positionConstraints[i] = pc; //m_velocityConstraints[i] = vc; } }