public ContactSolver(ContactSolverDef def)
        {
            _step  = def.step;
            _count = def.count;
            _positionConstraints = new ContactPositionConstraint[_count];
            _velocityConstraints = new ContactVelocityConstraint[_count];
            _positions           = def.positions;
            _velocities          = def.velocities;
            _contacts            = def.contacts;

            for (int i = 0; i < _count; ++i)
            {
                Contact contact = _contacts[i];

                Fixture  fixtureA = contact.m_fixtureA;
                Fixture  fixtureB = contact.m_fixtureB;
                Shape    shapeA   = fixtureA.Shape;
                Shape    shapeB   = fixtureB.Shape;
                float    radiusA  = shapeA.m_radius;
                float    radiusB  = shapeB.m_radius;
                Body     bodyA    = fixtureA.Body;
                Body     bodyB    = fixtureB.Body;
                Manifold manifold = contact.Manifold;

                int pointCount = manifold.pointCount;
                //Debug.Assert(pointCount > 0);

                _velocityConstraints[i] = new ContactVelocityConstraint();
                ContactVelocityConstraint vc = _velocityConstraints[i];
                vc.friction     = contact.m_friction;
                vc.restitution  = contact.m_restitution;
                vc.tangentSpeed = contact.m_tangentSpeed;
                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            = new Matrix3x2(); // .SetZero();
                vc.normalMass   = new Matrix3x2(); // .SetZero();

                _positionConstraints[i] = new ContactPositionConstraint();
                ContactPositionConstraint pc = _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 (int j = 0; j < pointCount; ++j)
                {
                    ManifoldPoint cp = manifold.points[j];
                    vc.points[j] = new VelocityConstraintPoint();
                    VelocityConstraintPoint vcp = vc.points[j];

                    if (_step.warmStarting)
                    {
                        vcp.normalImpulse  = _step.dtRatio * cp.normalImpulse;
                        vcp.tangentImpulse = _step.dtRatio * cp.tangentImpulse;
                    }
                    else
                    {
                        vcp.normalImpulse  = 0f;
                        vcp.tangentImpulse = 0f;
                    }

                    vcp.rA           = Vector2.Zero;
                    vcp.rB           = Vector2.Zero;
                    vcp.normalMass   = 0f;
                    vcp.tangentMass  = 0f;
                    vcp.velocityBias = 0f;

                    pc.localPoints[j] = cp.localPoint;
                }
            }
        }
        public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB)
        {
            float minSeparation = 0.0f;

            for (int i = 0; i < _count; ++i)
            {
                ContactPositionConstraint pc = _positionConstraints[i];

                int     indexA       = pc.indexA;
                int     indexB       = pc.indexB;
                Vector2 localCenterA = pc.localCenterA;
                Vector2 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 = 0.0f;
                float iB = 0.0f;
                if (indexB == toiIndexA || indexB == toiIndexB)
                {
                    mB = pc.invMassB;
                    iB = pc.invIB;
                }

                Vector2 cA = _positions[indexA].c;
                float   aA = _positions[indexA].a;

                Vector2 cB = _positions[indexB].c;
                float   aB = _positions[indexB].a;

                // Solve normal constraints
                for (int j = 0; j < pointCount; ++j)
                {
                    Transform xfA = new Transform();
                    Transform xfB = new Transform();
                    xfA.q = Matrex.CreateRotation(aA);                   // Actually about twice as fast to use our own function
                    xfB.q = Matrex.CreateRotation(aB);                   // Actually about twice as fast to use our own function
                    xfA.p = cA - Vector2.Transform(localCenterA, xfA.q); // Common.Math.Mul(xfA.q, localCenterA);
                    xfB.p = cB - Vector2.Transform(localCenterB, xfB.q); // Common.Math.Mul(xfB.q, localCenterB);

                    PositionSolverManifold psm = new PositionSolverManifold();
                    psm.Initialize(pc, xfA, xfB, j);
                    Vector2 normal = psm.normal;

                    Vector2 point      = psm.point;
                    float   separation = psm.separation;

                    Vector2 rA = point - cA;
                    Vector2 rB = point - cB;

                    // Track max constraint error.
                    minSeparation = MathF.Min(minSeparation, separation);

                    // Prevent large corrections and allow slop.
                    float C = Math.Clamp(Settings.TOIBaumgarte * (separation + Settings.LinearSlop),
                                         -Settings.MaxLinearCorrection, 0.0f);

                    // Compute the effective mass.
                    float rnA = Vectex.Cross(rA, normal);
                    float rnB = Vectex.Cross(rB, normal);
                    float K   = mA + mB + iA * rnA * rnA + iB * rnB * rnB;

                    // Compute normal impulse
                    float impulse = K > 0.0f ? -C / K : 0.0f;

                    Vector2 P = impulse * normal;

                    cA -= mA * P;
                    aA -= iA * Vectex.Cross(rA, P);

                    cB += mB * P;
                    aB += iB * Vectex.Cross(rB, P);
                }

                _positions[indexA].c = cA;
                _positions[indexA].a = aA;

                _positions[indexB].c = cB;
                _positions[indexB].a = aB;
            }

            // We can't expect minSpeparation >= -b2_linearSlop because we don't
            // push the separation above -b2_linearSlop.
            return(minSeparation >= -1.5f * Settings.LinearSlop);
        }
        public void InitializeVelocityConstraints()
        {
            for (int i = 0; i < _count; ++i)
            {
                ContactVelocityConstraint vc = _velocityConstraints[i];
                ContactPositionConstraint pc = _positionConstraints[i];

                float    radiusA  = pc.radiusA;
                float    radiusB  = pc.radiusB;
                Manifold manifold = _contacts[vc.contactIndex].Manifold;

                int indexA = vc.indexA;
                int indexB = vc.indexB;

                float   mA           = vc.invMassA;
                float   mB           = vc.invMassB;
                float   iA           = vc.invIA;
                float   iB           = vc.invIB;
                Vector2 localCenterA = pc.localCenterA;
                Vector2 localCenterB = pc.localCenterB;

                Vector2 cA = _positions[indexA].c;
                float   aA = _positions[indexA].a;
                Vector2 vA = _velocities[indexA].v;
                float   wA = _velocities[indexA].w;

                Vector2 cB = _positions[indexB].c;
                float   aB = _positions[indexB].a;
                Vector2 vB = _velocities[indexB].v;
                float   wB = _velocities[indexB].w;

                //Debug.Assert(manifold.pointCount > 0);

                Transform xfA = new Transform();
                Transform xfB = new Transform();

                xfA.q = Matrex.CreateRotation(aA);                   // Actually about twice as fast to use our own function
                xfB.q = Matrex.CreateRotation(aB);                   // Actually about twice as fast to use our own function
                xfA.p = cA - Vector2.Transform(localCenterA, xfA.q); // Common.Math.Mul(xfA.q, localCenterA);
                xfB.p = cB - Vector2.Transform(localCenterB, xfB.q); // Common.Math.Mul(xfB.q, localCenterB);

                WorldManifold worldManifold = new WorldManifold();
                worldManifold.Initialize(manifold, xfA, radiusA, xfB, radiusB);

                vc.normal = worldManifold.normal;

                int pointCount = vc.pointCount;
                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 = Vectex.Cross(vcp.rA, vc.normal);
                    float rnB = Vectex.Cross(vcp.rB, vc.normal);

                    float kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;

                    vcp.normalMass = kNormal > 0f ? 1f / kNormal : 0f;

                    Vector2 tangent = Vectex.Cross(vc.normal, 1f);

                    float rtA = Vectex.Cross(vcp.rA, tangent);
                    float rtB = Vectex.Cross(vcp.rB, tangent);

                    float kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB;

                    vcp.tangentMass = kTangent > 0f ? 1f / kTangent : 0f;

                    vcp.velocityBias = 0f;
                    float vRel = Vector2.Dot(vc.normal, vB + Vectex.Cross(wB, vcp.rB) - vA - Vectex.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.pointCount == 2 && Settings.BlockSolve)
                {
                    VelocityConstraintPoint vcp1 = vc.points[0];
                    VelocityConstraintPoint vcp2 = vc.points[1];

                    float rn1A = Vectex.Cross(vcp1.rA, vc.normal);
                    float rn1B = Vectex.Cross(vcp1.rB, vc.normal);
                    float rn2A = Vectex.Cross(vcp2.rA, vc.normal);
                    float rn2B = Vectex.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 = new Matrix3x2(k11, k12, k12, k22, 0, 0);

                        // vc.K.ex       = new Vector2(k11, k12);
                        // vc.K.ey       = new Vector2(k12, k22);
                        /*Matrix3x2*/
                        Matrex.Invert(vc.K, out Matrix3x2 KT);
                        vc.normalMass = KT;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        vc.pointCount = 1;
                    }
                }
            }
        }