public void Reset(TimeStep step, int count, Contact[] contacts, Position[] positions, Velocity[] velocities)
        {
            _step       = step;
            _count      = count;
            _positions  = positions;
            _velocities = velocities;
            _contacts   = contacts;

            // grow the array
            if (VelocityConstraints == null || VelocityConstraints.Length < count)
            {
                VelocityConstraints  = new ContactVelocityConstraint[count * 2];
                _positionConstraints = new ContactPositionConstraint[count * 2];

                for (int i = 0; i < VelocityConstraints.Length; i++)
                {
                    VelocityConstraints[i] = new ContactVelocityConstraint();
                }

                for (int i = 0; i < _positionConstraints.Length; i++)
                {
                    _positionConstraints[i] = new ContactPositionConstraint();
                }
            }

            // Initialize position independent portions of the constraints.
            for (int i = 0; i < _count; ++i)
            {
                Contact contact = contacts[i];

                Fixture          fixtureA = contact.FixtureA;
                Fixture          fixtureB = contact.FixtureB;
                Shape            shapeA   = fixtureA.Shape;
                Shape            shapeB   = fixtureB.Shape;
                GGame.Math.Fix64 radiusA  = shapeA.Radius;
                GGame.Math.Fix64 radiusB  = shapeB.Radius;
                Body             bodyA    = fixtureA.Body;
                Body             bodyB    = fixtureB.Body;
                Manifold         manifold = contact.Manifold;

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

                ContactVelocityConstraint vc = VelocityConstraints[i];
                vc.Friction     = contact.Friction;
                vc.Restitution  = contact.Restitution;
                vc.TangentSpeed = contact.TangentSpeed;
                vc.IndexA       = bodyA.IslandIndex;
                vc.IndexB       = bodyB.IslandIndex;
                vc.InvMassA     = bodyA._invMass;
                vc.InvMassB     = bodyB._invMass;
                vc.InvIA        = bodyA._invI;
                vc.InvIB        = bodyB._invI;
                vc.ContactIndex = i;
                vc.PointCount   = pointCount;
                vc.K.SetZero();
                vc.NormalMass.SetZero();

                ContactPositionConstraint pc = _positionConstraints[i];
                pc.IndexA       = bodyA.IslandIndex;
                pc.IndexB       = bodyB.IslandIndex;
                pc.InvMassA     = bodyA._invMass;
                pc.InvMassB     = bodyB._invMass;
                pc.LocalCenterA = bodyA._sweep.LocalCenter;
                pc.LocalCenterB = bodyB._sweep.LocalCenter;
                pc.InvIA        = bodyA._invI;
                pc.InvIB        = bodyB._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];
                    VelocityConstraintPoint vcp = vc.Points[j];

                    if (Settings.EnableWarmstarting)
                    {
                        vcp.NormalImpulse  = _step.dtRatio * cp.NormalImpulse;
                        vcp.TangentImpulse = _step.dtRatio * cp.TangentImpulse;
                    }
                    else
                    {
                        vcp.NormalImpulse  = 0.0f;
                        vcp.TangentImpulse = 0.0f;
                    }

                    vcp.rA           = Vector2.Zero;
                    vcp.rB           = Vector2.Zero;
                    vcp.NormalMass   = 0.0f;
                    vcp.TangentMass  = 0.0f;
                    vcp.VelocityBias = 0.0f;

                    pc.LocalPoints[j] = cp.LocalPoint;
                }
            }
        }
        // Sequential position solver for position constraints.
        public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB)
        {
            GGame.Math.Fix64 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;

                GGame.Math.Fix64 mA = 0.0f;
                GGame.Math.Fix64 iA = 0.0f;
                if (indexA == toiIndexA || indexA == toiIndexB)
                {
                    mA = pc.InvMassA;
                    iA = pc.InvIA;
                }

                GGame.Math.Fix64 mB = 0.0f;
                GGame.Math.Fix64 iB = 0.0f;
                if (indexB == toiIndexA || indexB == toiIndexB)
                {
                    mB = pc.InvMassB;
                    iB = pc.InvIB;
                }

                Vector2          cA = _positions[indexA].C;
                GGame.Math.Fix64 aA = _positions[indexA].A;

                Vector2          cB = _positions[indexB].C;
                GGame.Math.Fix64 aB = _positions[indexB].A;

                // Solve normal constraints
                for (int j = 0; j < pointCount; ++j)
                {
                    Transform xfA = new Transform();
                    Transform xfB = new Transform();
                    xfA.q.Set(aA);
                    xfB.q.Set(aB);
                    xfA.p = cA - MathUtils.Mul(xfA.q, localCenterA);
                    xfB.p = cB - MathUtils.Mul(xfB.q, localCenterB);

                    PositionSolverManifold.Initialize(pc, xfA, xfB, j, out Vector2 normal, out Vector2 point, out GGame.Math.Fix64 separation);

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

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

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

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

                    // Compute normal impulse
                    GGame.Math.Fix64 impulse = K > 0.0f ? -C / K : 0.0f;

                    Vector2 P = impulse * normal;

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

                    cB += mB * P;
                    aB += iB * MathUtils.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);
        }
        /// <summary>
        /// Initialize position dependent portions of the velocity constraints.
        /// </summary>
        public void InitializeVelocityConstraints()
        {
            for (int i = 0; i < _count; ++i)
            {
                ContactVelocityConstraint vc = VelocityConstraints[i];
                ContactPositionConstraint pc = _positionConstraints[i];

                GGame.Math.Fix64 radiusA  = pc.RadiusA;
                GGame.Math.Fix64 radiusB  = pc.RadiusB;
                Manifold         manifold = _contacts[vc.ContactIndex].Manifold;

                int indexA = vc.IndexA;
                int indexB = vc.IndexB;

                GGame.Math.Fix64 mA           = vc.InvMassA;
                GGame.Math.Fix64 mB           = vc.InvMassB;
                GGame.Math.Fix64 iA           = vc.InvIA;
                GGame.Math.Fix64 iB           = vc.InvIB;
                Vector2          localCenterA = pc.LocalCenterA;
                Vector2          localCenterB = pc.LocalCenterB;

                Vector2          cA = _positions[indexA].C;
                GGame.Math.Fix64 aA = _positions[indexA].A;
                Vector2          vA = _velocities[indexA].V;
                GGame.Math.Fix64 wA = _velocities[indexA].W;

                Vector2          cB = _positions[indexB].C;
                GGame.Math.Fix64 aB = _positions[indexB].A;
                Vector2          vB = _velocities[indexB].V;
                GGame.Math.Fix64 wB = _velocities[indexB].W;

                Debug.Assert(manifold.PointCount > 0);

                Transform xfA = new Transform();
                Transform xfB = new Transform();
                xfA.q.Set(aA);
                xfB.q.Set(aB);
                xfA.p = cA - MathUtils.Mul(xfA.q, localCenterA);
                xfB.p = cB - MathUtils.Mul(xfB.q, localCenterB);

                WorldManifold.Initialize(ref manifold, ref xfA, radiusA, ref xfB, radiusB, out Vector2 normal, out FixedArray2 <Vector2> points, out _);

                vc.Normal = normal;

                int pointCount = vc.PointCount;
                for (int j = 0; j < pointCount; ++j)
                {
                    VelocityConstraintPoint vcp = vc.Points[j];

                    vcp.rA = points[j] - cA;
                    vcp.rB = points[j] - cB;

                    GGame.Math.Fix64 rnA = MathUtils.Cross(vcp.rA, vc.Normal);
                    GGame.Math.Fix64 rnB = MathUtils.Cross(vcp.rB, vc.Normal);

                    GGame.Math.Fix64 kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;

                    vcp.NormalMass = kNormal > 0.0f ? 1.0f / kNormal : 0.0f;

                    Vector2 tangent = MathUtils.Cross(vc.Normal, 1.0f);

                    GGame.Math.Fix64 rtA = MathUtils.Cross(vcp.rA, tangent);
                    GGame.Math.Fix64 rtB = MathUtils.Cross(vcp.rB, tangent);

                    GGame.Math.Fix64 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;
                    GGame.Math.Fix64 vRel = Vector2.Dot(vc.Normal, vB + MathUtils.Cross(wB, vcp.rB) - vA - MathUtils.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];

                    GGame.Math.Fix64 rn1A = MathUtils.Cross(vcp1.rA, vc.Normal);
                    GGame.Math.Fix64 rn1B = MathUtils.Cross(vcp1.rB, vc.Normal);
                    GGame.Math.Fix64 rn2A = MathUtils.Cross(vcp2.rA, vc.Normal);
                    GGame.Math.Fix64 rn2B = MathUtils.Cross(vcp2.rB, vc.Normal);

                    GGame.Math.Fix64 k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
                    GGame.Math.Fix64 k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
                    GGame.Math.Fix64 k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    GGame.Math.Fix64 k_maxConditionNumber = 1000.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        vc.K.ex       = new Vector2(k11, k12);
                        vc.K.ey       = new Vector2(k12, k22);
                        vc.NormalMass = vc.K.Inverse;
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        vc.PointCount = 1;
                    }
                }
            }
        }