コード例 #1
0
        public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB)
        {
            FP fP = 0f;

            for (int i = 0; i < this._count; i++)
            {
                ContactPositionConstraint contactPositionConstraint = this._positionConstraints[i];
                int       indexA       = contactPositionConstraint.indexA;
                int       indexB       = contactPositionConstraint.indexB;
                TSVector2 localCenterA = contactPositionConstraint.localCenterA;
                TSVector2 localCenterB = contactPositionConstraint.localCenterB;
                int       pointCount   = contactPositionConstraint.pointCount;
                FP        fP2          = 0f;
                FP        x            = 0f;
                bool      flag         = indexA == toiIndexA || indexA == toiIndexB;
                if (flag)
                {
                    fP2 = contactPositionConstraint.invMassA;
                    x   = contactPositionConstraint.invIA;
                }
                FP   fP3   = 0f;
                FP   x2    = 0f;
                bool flag2 = indexB == toiIndexA || indexB == toiIndexB;
                if (flag2)
                {
                    fP3 = contactPositionConstraint.invMassB;
                    x2  = contactPositionConstraint.invIB;
                }
                TSVector2 tSVector  = this._positions[indexA].c;
                FP        fP4       = this._positions[indexA].a;
                TSVector2 tSVector2 = this._positions[indexB].c;
                FP        fP5       = this._positions[indexB].a;
                for (int j = 0; j < pointCount; j++)
                {
                    Transform transform  = default(Transform);
                    Transform transform2 = default(Transform);
                    transform.q.Set(fP4);
                    transform2.q.Set(fP5);
                    transform.p  = tSVector - MathUtils.Mul(transform.q, localCenterA);
                    transform2.p = tSVector2 - MathUtils.Mul(transform2.q, localCenterB);
                    TSVector2 tSVector3;
                    TSVector2 value;
                    FP        fP6;
                    ContactSolver.PositionSolverManifold.Initialize(contactPositionConstraint, transform, transform2, j, out tSVector3, out value, out fP6);
                    TSVector2 a  = value - tSVector;
                    TSVector2 a2 = value - tSVector2;
                    fP = TSMath.Min(fP, fP6);
                    FP        x3          = MathUtils.Clamp(Settings.Baumgarte * (fP6 + Settings.LinearSlop), -Settings.MaxLinearCorrection, 0f);
                    FP        y           = MathUtils.Cross(a, tSVector3);
                    FP        y2          = MathUtils.Cross(a2, tSVector3);
                    FP        fP7         = fP2 + fP3 + x * y * y + x2 * y2 * y2;
                    FP        scaleFactor = (fP7 > 0f) ? (-x3 / fP7) : 0f;
                    TSVector2 tSVector4   = scaleFactor * tSVector3;
                    tSVector  -= fP2 * tSVector4;
                    fP4       -= x * MathUtils.Cross(a, tSVector4);
                    tSVector2 += fP3 * tSVector4;
                    fP5       += x2 * MathUtils.Cross(a2, tSVector4);
                }
                this._positions[indexA].c = tSVector;
                this._positions[indexA].a = fP4;
                this._positions[indexB].c = tSVector2;
                this._positions[indexB].a = fP5;
            }
            return(fP >= -1.5f * Settings.LinearSlop);
        }
コード例 #2
0
 public void InitializeVelocityConstraints()
 {
     for (int i = 0; i < this._count; i++)
     {
         ContactVelocityConstraint contactVelocityConstraint = this._velocityConstraints[i];
         ContactPositionConstraint contactPositionConstraint = this._positionConstraints[i];
         FP        radiusA      = contactPositionConstraint.radiusA;
         FP        radiusB      = contactPositionConstraint.radiusB;
         Manifold  manifold     = this._contacts[contactVelocityConstraint.contactIndex].Manifold;
         int       indexA       = contactVelocityConstraint.indexA;
         int       indexB       = contactVelocityConstraint.indexB;
         FP        invMassA     = contactVelocityConstraint.invMassA;
         FP        invMassB     = contactVelocityConstraint.invMassB;
         FP        invIA        = contactVelocityConstraint.invIA;
         FP        invIB        = contactVelocityConstraint.invIB;
         TSVector2 localCenterA = contactPositionConstraint.localCenterA;
         TSVector2 localCenterB = contactPositionConstraint.localCenterB;
         TSVector2 c            = this._positions[indexA].c;
         FP        a            = this._positions[indexA].a;
         TSVector2 v            = this._velocities[indexA].v;
         FP        w            = this._velocities[indexA].w;
         TSVector2 c2           = this._positions[indexB].c;
         FP        a2           = this._positions[indexB].a;
         TSVector2 v2           = this._velocities[indexB].v;
         FP        w2           = this._velocities[indexB].w;
         Debug.Assert(manifold.PointCount > 0);
         Transform transform  = default(Transform);
         Transform transform2 = default(Transform);
         transform.q.Set(a);
         transform2.q.Set(a2);
         transform.p  = c - MathUtils.Mul(transform.q, localCenterA);
         transform2.p = c2 - MathUtils.Mul(transform2.q, localCenterB);
         TSVector2 normal;
         FixedArray2 <TSVector2> fixedArray;
         ContactSolver.WorldManifold.Initialize(ref manifold, ref transform, radiusA, ref transform2, radiusB, out normal, out fixedArray);
         contactVelocityConstraint.normal = normal;
         int pointCount = contactVelocityConstraint.pointCount;
         for (int j = 0; j < pointCount; j++)
         {
             VelocityConstraintPoint velocityConstraintPoint = contactVelocityConstraint.points[j];
             velocityConstraintPoint.rA = fixedArray[j] - c;
             velocityConstraintPoint.rB = fixedArray[j] - c2;
             FP y  = MathUtils.Cross(velocityConstraintPoint.rA, contactVelocityConstraint.normal);
             FP y2 = MathUtils.Cross(velocityConstraintPoint.rB, contactVelocityConstraint.normal);
             FP fP = invMassA + invMassB + invIA * y * y + invIB * y2 * y2;
             velocityConstraintPoint.normalMass = ((fP > 0f) ? (1f / fP) : 0f);
             TSVector2 b   = MathUtils.Cross(contactVelocityConstraint.normal, 1f);
             FP        y3  = MathUtils.Cross(velocityConstraintPoint.rA, b);
             FP        y4  = MathUtils.Cross(velocityConstraintPoint.rB, b);
             FP        fP2 = invMassA + invMassB + invIA * y3 * y3 + invIB * y4 * y4;
             velocityConstraintPoint.tangentMass  = ((fP2 > 0f) ? (1f / fP2) : 0f);
             velocityConstraintPoint.velocityBias = 0f;
             FP   fP3  = TSVector2.Dot(contactVelocityConstraint.normal, v2 + MathUtils.Cross(w2, velocityConstraintPoint.rB) - v - MathUtils.Cross(w, velocityConstraintPoint.rA));
             bool flag = fP3 < -Settings.VelocityThreshold;
             if (flag)
             {
                 velocityConstraintPoint.velocityBias = -contactVelocityConstraint.restitution * fP3;
             }
         }
         bool flag2 = contactVelocityConstraint.pointCount == 2;
         if (flag2)
         {
             VelocityConstraintPoint velocityConstraintPoint2 = contactVelocityConstraint.points[0];
             VelocityConstraintPoint velocityConstraintPoint3 = contactVelocityConstraint.points[1];
             FP   y5    = MathUtils.Cross(velocityConstraintPoint2.rA, contactVelocityConstraint.normal);
             FP   y6    = MathUtils.Cross(velocityConstraintPoint2.rB, contactVelocityConstraint.normal);
             FP   y7    = MathUtils.Cross(velocityConstraintPoint3.rA, contactVelocityConstraint.normal);
             FP   y8    = MathUtils.Cross(velocityConstraintPoint3.rB, contactVelocityConstraint.normal);
             FP   fP4   = invMassA + invMassB + invIA * y5 * y5 + invIB * y6 * y6;
             FP   y9    = invMassA + invMassB + invIA * y7 * y7 + invIB * y8 * y8;
             FP   fP5   = invMassA + invMassB + invIA * y5 * y7 + invIB * y6 * y8;
             FP   x     = 1000f;
             bool flag3 = fP4 * fP4 < x * (fP4 * y9 - fP5 * fP5);
             if (flag3)
             {
                 contactVelocityConstraint.K.ex       = new TSVector2(fP4, fP5);
                 contactVelocityConstraint.K.ey       = new TSVector2(fP5, y9);
                 contactVelocityConstraint.normalMass = contactVelocityConstraint.K.Inverse;
             }
             else
             {
                 contactVelocityConstraint.pointCount = 1;
             }
         }
     }
 }
コード例 #3
0
        public void Reset(TimeStep step, int count, Contact[] contacts, Position[] positions, Velocity[] velocities, bool warmstarting = true)
        {
            this._step       = step;
            this._count      = count;
            this._positions  = positions;
            this._velocities = velocities;
            this._contacts   = contacts;
            bool flag = this._velocityConstraints == null || this._velocityConstraints.Length < count;

            if (flag)
            {
                this._velocityConstraints = new ContactVelocityConstraint[count * 2];
                this._positionConstraints = new ContactPositionConstraint[count * 2];
                for (int i = 0; i < this._velocityConstraints.Length; i++)
                {
                    this._velocityConstraints[i] = new ContactVelocityConstraint();
                }
                for (int j = 0; j < this._positionConstraints.Length; j++)
                {
                    this._positionConstraints[j] = new ContactPositionConstraint();
                }
            }
            for (int k = 0; k < this._count; k++)
            {
                Contact  contact    = contacts[k];
                Fixture  fixtureA   = contact.FixtureA;
                Fixture  fixtureB   = contact.FixtureB;
                Shape    shape      = fixtureA.Shape;
                Shape    shape2     = fixtureB.Shape;
                FP       radius     = shape.Radius;
                FP       radius2    = shape2.Radius;
                Body     body       = fixtureA.Body;
                Body     body2      = fixtureB.Body;
                Manifold manifold   = contact.Manifold;
                int      pointCount = manifold.PointCount;
                Debug.Assert(pointCount > 0);
                ContactVelocityConstraint contactVelocityConstraint = this._velocityConstraints[k];
                contactVelocityConstraint.friction     = contact.Friction;
                contactVelocityConstraint.restitution  = contact.Restitution;
                contactVelocityConstraint.tangentSpeed = contact.TangentSpeed;
                contactVelocityConstraint.indexA       = body.IslandIndex;
                contactVelocityConstraint.indexB       = body2.IslandIndex;
                contactVelocityConstraint.invMassA     = body._invMass;
                contactVelocityConstraint.invMassB     = body2._invMass;
                contactVelocityConstraint.invIA        = body._invI;
                contactVelocityConstraint.invIB        = body2._invI;
                contactVelocityConstraint.contactIndex = k;
                contactVelocityConstraint.pointCount   = pointCount;
                contactVelocityConstraint.K.SetZero();
                contactVelocityConstraint.normalMass.SetZero();
                ContactPositionConstraint contactPositionConstraint = this._positionConstraints[k];
                contactPositionConstraint.indexA       = body.IslandIndex;
                contactPositionConstraint.indexB       = body2.IslandIndex;
                contactPositionConstraint.invMassA     = body._invMass;
                contactPositionConstraint.invMassB     = body2._invMass;
                contactPositionConstraint.localCenterA = body._sweep.LocalCenter;
                contactPositionConstraint.localCenterB = body2._sweep.LocalCenter;
                contactPositionConstraint.invIA        = body._invI;
                contactPositionConstraint.invIB        = body2._invI;
                contactPositionConstraint.localNormal  = manifold.LocalNormal;
                contactPositionConstraint.localPoint   = manifold.LocalPoint;
                contactPositionConstraint.pointCount   = pointCount;
                contactPositionConstraint.radiusA      = radius;
                contactPositionConstraint.radiusB      = radius2;
                contactPositionConstraint.type         = manifold.Type;
                for (int l = 0; l < pointCount; l++)
                {
                    ManifoldPoint           manifoldPoint           = manifold.Points[l];
                    VelocityConstraintPoint velocityConstraintPoint = contactVelocityConstraint.points[l];
                    velocityConstraintPoint.normalImpulse    = this._step.dtRatio * manifoldPoint.NormalImpulse;
                    velocityConstraintPoint.tangentImpulse   = this._step.dtRatio * manifoldPoint.TangentImpulse;
                    velocityConstraintPoint.rA               = TSVector2.zero;
                    velocityConstraintPoint.rB               = TSVector2.zero;
                    velocityConstraintPoint.normalMass       = 0f;
                    velocityConstraintPoint.tangentMass      = 0f;
                    velocityConstraintPoint.velocityBias     = 0f;
                    contactPositionConstraint.localPoints[l] = manifoldPoint.LocalPoint;
                }
            }
        }
コード例 #4
0
        public void Reset(TimeStep step, int count, Contact[] contacts, Position[] positions, Velocity[] velocities, bool warmstarting = Settings.EnableWarmstarting)
        {
            _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;
                FP       radiusA  = shapeA.Radius;
                FP       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           = TSVector2.zero;
                    vcp.rB           = TSVector2.zero;
                    vcp.normalMass   = 0.0f;
                    vcp.tangentMass  = 0.0f;
                    vcp.velocityBias = 0.0f;

                    pc.localPoints[j] = cp.LocalPoint;
                }
            }
        }
コード例 #5
0
        // Sequential position solver for position constraints.
        public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB)
        {
            FP minSeparation = 0.0f;

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

                int       indexA       = pc.indexA;
                int       indexB       = pc.indexB;
                TSVector2 localCenterA = pc.localCenterA;
                TSVector2 localCenterB = pc.localCenterB;
                int       pointCount   = pc.pointCount;

                FP mA = 0.0f;
                FP iA = 0.0f;
                if (indexA == toiIndexA || indexA == toiIndexB)
                {
                    mA = pc.invMassA;
                    iA = pc.invIA;
                }

                FP mB = 0.0f;
                FP iB = 0.0f;
                if (indexB == toiIndexA || indexB == toiIndexB)
                {
                    mB = pc.invMassB;
                    iB = pc.invIB;
                }

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

                TSVector2 cB = _positions[indexB].c;
                FP        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);

                    TSVector2 normal;
                    TSVector2 point;
                    FP        separation;

                    PositionSolverManifold.Initialize(pc, xfA, xfB, j, out normal, out point, out separation);

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

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

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

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

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

                    TSVector2 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);
        }
コード例 #6
0
        public void InitializeVelocityConstraints()
        {
            for (int i = 0; i < _count; ++i)
            {
                ContactVelocityConstraint vc = _velocityConstraints[i];
                ContactPositionConstraint pc = _positionConstraints[i];

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

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

                FP        mA           = vc.invMassA;
                FP        mB           = vc.invMassB;
                FP        iA           = vc.invIA;
                FP        iB           = vc.invIB;
                TSVector2 localCenterA = pc.localCenterA;
                TSVector2 localCenterB = pc.localCenterB;

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

                TSVector2 cB = _positions[indexB].c;
                FP        aB = _positions[indexB].a;
                TSVector2 vB = _velocities[indexB].v;
                FP        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);

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

                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;

                    FP rnA = MathUtils.Cross(vcp.rA, vc.normal);
                    FP rnB = MathUtils.Cross(vcp.rB, vc.normal);

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

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

                    TSVector2 tangent = MathUtils.Cross(vc.normal, 1.0f);

                    FP rtA = MathUtils.Cross(vcp.rA, tangent);
                    FP rtB = MathUtils.Cross(vcp.rB, tangent);

                    FP 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;
                    FP vRel = TSVector2.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)
                {
                    VelocityConstraintPoint vcp1 = vc.points[0];
                    VelocityConstraintPoint vcp2 = vc.points[1];

                    FP rn1A = MathUtils.Cross(vcp1.rA, vc.normal);
                    FP rn1B = MathUtils.Cross(vcp1.rB, vc.normal);
                    FP rn2A = MathUtils.Cross(vcp2.rA, vc.normal);
                    FP rn2B = MathUtils.Cross(vcp2.rB, vc.normal);

                    FP k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
                    FP k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
                    FP k12 = mA + mB + iA * rn1A * rn2A + iB * rn1B * rn2B;

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