public override void Step(TestSettings settings) { Manifold manifold; Collision.CollidePolygons(out manifold, m_polygonA, m_transformA, m_polygonB, m_transformB); WorldManifold worldManifold = new WorldManifold(); worldManifold.Initialize(manifold, m_transformA, m_polygonA.m_radius, m_transformB, m_polygonB.m_radius); m_debugDraw.DrawString("point count = {0}", manifold.points.Count()); { Color color = Color.FromArgb(225, 225, 225); Vec2[] v = new Vec2[Settings._maxPolygonVertices]; for (int i = 0; i < m_polygonA.m_count; ++i) { v[i] = Utilities.Mul(m_transformA, m_polygonA.m_vertices[i]); } m_debugDraw.DrawPolygon(v, m_polygonA.m_count, color); for (int i = 0; i < m_polygonB.m_count; ++i) { v[i] = Utilities.Mul(m_transformB, m_polygonB.m_vertices[i]); } m_debugDraw.DrawPolygon(v, m_polygonB.m_count, color); } for (int i = 0; i < manifold.points.Count(); ++i) { m_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, Color.FromArgb(225, 75, 75)); } }
/// Get the world manifold. public void GetWorldManifold(out WorldManifold worldManifold){ throw new NotImplementedException(); //const Body* bodyA = m_fixtureA.GetBody(); //const Body* bodyB = m_fixtureB.GetBody(); //const Shape* shapeA = m_fixtureA.GetShape(); //const Shape* shapeB = m_fixtureB.GetShape(); //worldManifold.Initialize(&m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius); }
/// Get the world manifold. public void GetWorldManifold(out WorldManifold worldManifold) { throw new NotImplementedException(); //const Body* bodyA = m_fixtureA.GetBody(); //const Body* bodyB = m_fixtureB.GetBody(); //const Shape* shapeA = m_fixtureA.GetShape(); //const Shape* shapeB = m_fixtureB.GetShape(); //worldManifold.Initialize(&m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius); }
/// Get the world manifold. public void GetWorldManifold(out WorldManifold worldManifold) { Body bodyA = _fixtureA.GetBody(); Body bodyB = _fixtureB.GetBody(); Shape shapeA = _fixtureA.GetShape(); Shape shapeB = _fixtureB.GetShape(); Transform xfA, xfB; bodyA.GetTransform(out xfA); bodyB.GetTransform(out xfB); worldManifold = new WorldManifold(ref _manifold, ref xfA, shapeA._radius, ref xfB, shapeB._radius); }
public void Reset(Contact[] contacts, int contactCount, float impulseRatio) { _contacts = contacts; _constraintCount = contactCount; // grow the array if (_constraints == null || _constraints.Length < _constraintCount) { _constraints = new ContactConstraint[_constraintCount * 2]; } for (int i = 0; i < _constraintCount; ++i) { Contact contact = contacts[i]; Fixture fixtureA = contact._fixtureA; Fixture fixtureB = contact._fixtureB; Shape shapeA = fixtureA.GetShape(); Shape shapeB = fixtureB.GetShape(); float radiusA = shapeA._radius; float radiusB = shapeB._radius; Body bodyA = fixtureA.GetBody(); Body bodyB = fixtureB.GetBody(); Manifold manifold; contact.GetManifold(out manifold); float friction = Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction()); float restitution = Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution()); Vector2 vA = bodyA._linearVelocity; Vector2 vB = bodyB._linearVelocity; float wA = bodyA._angularVelocity; float wB = bodyB._angularVelocity; //Debug.Assert(manifold._pointCount > 0); WorldManifold worldManifold = new WorldManifold(ref manifold, ref bodyA._xf, radiusA, ref bodyB._xf, radiusB); ContactConstraint cc = _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 (int j = 0; j < cc.pointCount; ++j) { ManifoldPoint cp = manifold._points[j]; ContactConstraintPoint ccp = cc.points[j]; ccp.normalImpulse = impulseRatio * cp.NormalImpulse; ccp.tangentImpulse = impulseRatio * cp.TangentImpulse; ccp.localPoint = cp.LocalPoint; ccp.rA = worldManifold._points[j] - bodyA._sweep.c; ccp.rB = worldManifold._points[j] - bodyB._sweep.c; #if MATH_OVERLOADS float rnA = MathUtils.Cross(ccp.rA, cc.normal); float rnB = MathUtils.Cross(ccp.rB, cc.normal); #else float rnA = ccp.rA.x * cc.normal.y - ccp.rA.y * cc.normal.x; float rnB = ccp.rB.x * cc.normal.y - ccp.rB.y * cc.normal.x; #endif rnA *= rnA; rnB *= rnB; float kNormal = bodyA._invMass + bodyB._invMass + bodyA._invI * rnA + bodyB._invI * rnB; //Debug.Assert(kNormal > Settings.b2_epsilon); ccp.normalMass = 1.0f / kNormal; #if MATH_OVERLOADS Vector2 tangent = MathUtils.Cross(cc.normal, 1.0f); float rtA = MathUtils.Cross(ccp.rA, tangent); float rtB = MathUtils.Cross(ccp.rB, tangent); #else Vector2 tangent = new Vector2(cc.normal.y, -cc.normal.x); float rtA = ccp.rA.x * tangent.y - ccp.rA.y * tangent.x; float rtB = ccp.rB.x * tangent.y - ccp.rB.y * tangent.x; #endif rtA *= rtA; rtB *= rtB; float kTangent = bodyA._invMass + bodyB._invMass + bodyA._invI * rtA + bodyB._invI * rtB; //Debug.Assert(kTangent > Settings.b2_epsilon); ccp.tangentMass = 1.0f / kTangent; // Setup a velocity bias for restitution. ccp.velocityBias = 0.0f; float vRel = Vector2.Dot(cc.normal, vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA)); if (vRel < -Settings.b2_velocityThreshold) { ccp.velocityBias = -restitution * vRel; } cc.points[j] = ccp; } // If we have two points, then prepare the block solver. if (cc.pointCount == 2) { ContactConstraintPoint ccp1 = cc.points[0]; ContactConstraintPoint ccp2 = cc.points[1]; float invMassA = bodyA._invMass; float invIA = bodyA._invI; float invMassB = bodyB._invMass; float invIB = bodyB._invI; float rn1A = MathUtils.Cross(ccp1.rA, cc.normal); float rn1B = MathUtils.Cross(ccp1.rB, cc.normal); float rn2A = MathUtils.Cross(ccp2.rA, cc.normal); float rn2B = MathUtils.Cross(ccp2.rB, cc.normal); float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B; float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B; float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B; // Ensure a reasonable condition number. const float k_maxConditionNumber = 100.0f; if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12)) { // K is safe to invert. cc.K = new Mat22(new Vector2(k11, k12), new Vector2(k12, k22)); cc.normalMass = cc.K.GetInverse(); } else { // The constraints are redundant, just use one. // TODO_ERIN use deepest? cc.pointCount = 1; } } _constraints[i] = cc; } }
public void InitializeVelocityConstraints() { for (int i = 0; i < m_contacts.Count(); ++i) { ContactVelocityConstraint vc = m_velocityConstraints[i]; ContactPositionConstraint pc = m_positionConstraints[i]; float radiusA = pc.radiusA; float radiusB = pc.radiusB; Manifold 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; Vec2 localCenterA = pc.localCenterA; Vec2 localCenterB = pc.localCenterB; Vec2 cA = m_positions[indexA].c; float aA = m_positions[indexA].a; Vec2 vA = m_velocities[indexA].v; float wA = m_velocities[indexA].w; Vec2 cB = m_positions[indexB].c; float aB = m_positions[indexB].a; Vec2 vB = m_velocities[indexB].v; float wB = m_velocities[indexB].w; Utilities.Assert(manifold.points.Count() > 0); Transform xfA = new Transform(); Transform xfB = new Transform(); xfA.q.Set(aA); xfB.q.Set(aB); xfA.p = cA - Utilities.Mul(xfA.q, localCenterA); xfB.p = cB - Utilities.Mul(xfB.q, localCenterB); WorldManifold worldManifold = new WorldManifold(); worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB); vc.normal = worldManifold.normal; int pointCount = vc.points.Count; for (int j = 0; j < pointCount; ++j) { VelocityConstraintPoint vcp = vc.points[j]; vcp.rA = worldManifold.points[j] - cA; vcp.rB = worldManifold.points[j] - cB; float rnA = Utilities.Cross(vcp.rA, vc.normal); float rnB = Utilities.Cross(vcp.rB, vc.normal); float kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB; vcp.normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f; Vec2 tangent = Utilities.Cross(vc.normal, 1.0f); float rtA = Utilities.Cross(vcp.rA, tangent); float rtB = Utilities.Cross(vcp.rB, 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 = Utilities.Dot(vc.normal, vB + Utilities.Cross(wB, vcp.rB) - vA - Utilities.Cross(wA, vcp.rA)); if (vRel < -Settings._velocityThreshold) { vcp.velocityBias = -vc.restitution * vRel; } } // If we have two points, then prepare the block solver. if (vc.points.Count() == 2) { VelocityConstraintPoint vcp1 = vc.points[0]; VelocityConstraintPoint vcp2 = vc.points[1]; float rn1A = Utilities.Cross(vcp1.rA, vc.normal); float rn1B = Utilities.Cross(vcp1.rB, vc.normal); float rn2A = Utilities.Cross(vcp2.rA, vc.normal); float rn2B = Utilities.Cross(vcp2.rB, 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. const 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.points.Clear(); vc.points.Add(new VelocityConstraintPoint()); } } } }
public void InitializeVelocityConstraints() { for (int i = 0; i < m_contacts.Count(); ++i) { ContactVelocityConstraint vc = m_velocityConstraints[i]; ContactPositionConstraint pc = m_positionConstraints[i]; float radiusA = pc.radiusA; float radiusB = pc.radiusB; Manifold 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; Vec2 localCenterA = pc.localCenterA; Vec2 localCenterB = pc.localCenterB; Vec2 cA = m_positions[indexA].c; float aA = m_positions[indexA].a; Vec2 vA = m_velocities[indexA].v; float wA = m_velocities[indexA].w; Vec2 cB = m_positions[indexB].c; float aB = m_positions[indexB].a; Vec2 vB = m_velocities[indexB].v; float wB = m_velocities[indexB].w; Utilities.Assert(manifold.points.Count() > 0); Transform xfA = new Transform(); Transform xfB = new Transform(); xfA.q.Set(aA); xfB.q.Set(aB); xfA.p = cA - Utilities.Mul(xfA.q, localCenterA); xfB.p = cB - Utilities.Mul(xfB.q, localCenterB); WorldManifold worldManifold = new WorldManifold(); worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB); vc.normal = worldManifold.normal; int pointCount = vc.points.Count; for (int j = 0; j < pointCount; ++j) { VelocityConstraintPoint vcp = vc.points[j]; vcp.rA = worldManifold.points[j] - cA; vcp.rB = worldManifold.points[j] - cB; float rnA = Utilities.Cross(vcp.rA, vc.normal); float rnB = Utilities.Cross(vcp.rB, vc.normal); float kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB; vcp.normalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f; Vec2 tangent = Utilities.Cross(vc.normal, 1.0f); float rtA = Utilities.Cross(vcp.rA, tangent); float rtB = Utilities.Cross(vcp.rB, 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 = Utilities.Dot(vc.normal, vB + Utilities.Cross(wB, vcp.rB) - vA - Utilities.Cross(wA, vcp.rA)); if (vRel < -Settings._velocityThreshold) { vcp.velocityBias = -vc.restitution * vRel; } } // If we have two points, then prepare the block solver. if (vc.points.Count() == 2) { VelocityConstraintPoint vcp1 = vc.points[0]; VelocityConstraintPoint vcp2 = vc.points[1]; float rn1A = Utilities.Cross(vcp1.rA, vc.normal); float rn1B = Utilities.Cross(vcp1.rB, vc.normal); float rn2A = Utilities.Cross(vcp2.rA, vc.normal); float rn2B = Utilities.Cross(vcp2.rB, 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. const 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.points.Clear(); vc.points.Add(new VelocityConstraintPoint()); } } } }