Esempio n. 1
0
        /// <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];

                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.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;

                    float rnA = MathUtils.Cross(vcp.RA, vc.Normal);
                    float rnB = MathUtils.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;

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

                    float rtA = MathUtils.Cross(vcp.RA, tangent);
                    float rtB = MathUtils.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 = MathUtils.Dot(vc.Normal,
                                               vB + MathUtils.Cross(wB, vcp.RB) - vA - MathUtils.Cross(wA, vcp.RA));
                    if (vRel < -vc.Threshold)
                    {
                        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 = MathUtils.Cross(vcp1.RA, vc.Normal);
                    float rn1B = MathUtils.Cross(vcp1.RB, vc.Normal);
                    float rn2A = MathUtils.Cross(vcp2.RA, vc.Normal);
                    float rn2B = MathUtils.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 kMaxConditionNumber = 1000.0f;
                    if (k11 * k11 < kMaxConditionNumber * (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;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Solves the velocity constraints
        /// </summary>
        public void SolveVelocityConstraints()
        {
            for (int i = 0; i < count; ++i)
            {
                ContactVelocityConstraint vc = VelocityConstraints[i];

                int   indexA     = vc.IndexA;
                int   indexB     = vc.IndexB;
                float mA         = vc.InvMassA;
                float iA         = vc.InvIa;
                float mB         = vc.InvMassB;
                float iB         = vc.InvIb;
                int   pointCount = vc.PointCount;

                Vector2 vA = velocities[indexA].V;
                float   wA = velocities[indexA].W;
                Vector2 vB = velocities[indexB].V;
                float   wB = velocities[indexB].W;

                Vector2 normal   = vc.Normal;
                Vector2 tangent  = MathUtils.Cross(normal, 1.0f);
                float   friction = vc.Friction;

                Debug.Assert(pointCount == 1 || pointCount == 2);

                // Solve tangent constraints first because non-penetration is more important
                // than friction.
                for (int j = 0; j < pointCount; ++j)
                {
                    VelocityConstraintPoint vcp = vc.Points[j];

                    // Relative velocity at contact
                    Vector2 dv = vB + MathUtils.Cross(wB, vcp.RB) - vA - MathUtils.Cross(wA, vcp.RA);

                    // Compute tangent force
                    float vt     = Vector2.Dot(dv, tangent) - vc.TangentSpeed;
                    float lambda = vcp.TangentMass * -vt;

                    // b2Clamp the accumulated force
                    float maxFriction = friction * vcp.NormalImpulse;
                    float newImpulse  = MathUtils.Clamp(vcp.TangentImpulse + lambda, -maxFriction, maxFriction);
                    lambda             = newImpulse - vcp.TangentImpulse;
                    vcp.TangentImpulse = newImpulse;

                    // Apply contact impulse
                    Vector2 p = lambda * tangent;

                    vA -= mA * p;
                    wA -= iA * MathUtils.Cross(vcp.RA, p);

                    vB += mB * p;
                    wB += iB * MathUtils.Cross(vcp.RB, p);
                }

                // Solve normal constraints
                if (pointCount == 1 || !Settings.BlockSolve)
                {
                    for (int j = 0; j < pointCount; ++j)
                    {
                        VelocityConstraintPoint vcp = vc.Points[j];

                        // Relative velocity at contact
                        Vector2 dv = vB + MathUtils.Cross(wB, vcp.RB) - vA - MathUtils.Cross(wA, vcp.RA);

                        // Compute normal impulse
                        float vn     = Vector2.Dot(dv, normal);
                        float lambda = -vcp.NormalMass * (vn - vcp.VelocityBias);

                        // b2Clamp the accumulated impulse
                        float newImpulse = Math.Max(vcp.NormalImpulse + lambda, 0.0f);
                        lambda            = newImpulse - vcp.NormalImpulse;
                        vcp.NormalImpulse = newImpulse;

                        // Apply contact impulse
                        Vector2 p = lambda * normal;
                        vA -= mA * p;
                        wA -= iA * MathUtils.Cross(vcp.RA, p);

                        vB += mB * p;
                        wB += iB * MathUtils.Cross(vcp.RB, p);
                    }
                }
                else
                {
                    // Block solver developed in collaboration with Dirk Gregorius (back in 01/07 on Box2D_Lite).
                    // Build the mini LCP for this contact patch
                    //
                    // vn = A * x + b, vn >= 0, x >= 0 and vn_i * x_i = 0 with i = 1..2
                    //
                    // A = J * W * JT and J = ( -n, -r1 x n, n, r2 x n )
                    // b = vn0 - velocityBias
                    //
                    // The system is solved using the "Total enumeration method" (s. Murty). The complementary constraint vn_i * x_i
                    // implies that we must have in any solution either vn_i = 0 or x_i = 0. So for the 2D contact problem the cases
                    // vn1 = 0 and vn2 = 0, x1 = 0 and x2 = 0, x1 = 0 and vn2 = 0, x2 = 0 and vn1 = 0 need to be tested. The first valid
                    // solution that satisfies the problem is chosen.
                    //
                    // In order to account of the accumulated impulse 'a' (because of the iterative nature of the solver which only requires
                    // that the accumulated impulse is clamped and not the incremental impulse) we change the impulse variable (x_i).
                    //
                    // Substitute:
                    //
                    // x = a + d
                    //
                    // a := old total impulse
                    // x := new total impulse
                    // d := incremental impulse
                    //
                    // For the current iteration we extend the formula for the incremental impulse
                    // to compute the new total impulse:
                    //
                    // vn = A * d + b
                    //    = A * (x - a) + b
                    //    = A * x + b - A * a
                    //    = A * x + b'
                    // b' = b - A * a;

                    VelocityConstraintPoint cp1 = vc.Points[0];
                    VelocityConstraintPoint cp2 = vc.Points[1];

                    Vector2 a = new Vector2(cp1.NormalImpulse, cp2.NormalImpulse);
                    Debug.Assert(a.X >= 0.0f && a.Y >= 0.0f);

                    // Relative velocity at contact
                    Vector2 dv1 = vB + MathUtils.Cross(wB, cp1.RB) - vA - MathUtils.Cross(wA, cp1.RA);
                    Vector2 dv2 = vB + MathUtils.Cross(wB, cp2.RB) - vA - MathUtils.Cross(wA, cp2.RA);

                    // Compute normal velocity
                    float vn1 = Vector2.Dot(dv1, normal);
                    float vn2 = Vector2.Dot(dv2, normal);

                    Vector2 b = Vector2.Zero;
                    b.X = vn1 - cp1.VelocityBias;
                    b.Y = vn2 - cp2.VelocityBias;

                    // Compute b'
                    b -= MathUtils.Mul(ref vc.K, a);

                    for (;;)
                    {
                        //
                        // Case 1: vn = 0
                        //
                        // 0 = A * x + b'
                        //
                        // Solve for x:
                        //
                        // x = - inv(A) * b'
                        //
                        Vector2 x = -MathUtils.Mul(ref vc.NormalMass, b);

                        if (x.X >= 0.0f && x.Y >= 0.0f)
                        {
                            // Get the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 p1 = d.X * normal;
                            Vector2 p2 = d.Y * normal;
                            vA -= mA * (p1 + p2);
                            wA -= iA * (MathUtils.Cross(cp1.RA, p1) + MathUtils.Cross(cp2.RA, p2));

                            vB += mB * (p1 + p2);
                            wB += iB * (MathUtils.Cross(cp1.RB, p1) + MathUtils.Cross(cp2.RB, p2));

                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            // Postconditions
                            dv1 = vB + MathUtils.Cross(wB, cp1.rB) - vA - MathUtils.Cross(wA, cp1.rA);
                            dv2 = vB + MathUtils.Cross(wB, cp2.rB) - vA - MathUtils.Cross(wA, cp2.rA);

                            // Compute normal velocity
                            vn1 = Vector2.Dot(dv1, normal);
                            vn2 = Vector2.Dot(dv2, normal);

                            Debug.Assert(Math.Abs(vn1 - cp1.VelocityBias) < k_errorTol);
                            Debug.Assert(Math.Abs(vn2 - cp2.VelocityBias) < k_errorTol);
#endif
                            break;
                        }

                        //
                        // Case 2: vn1 = 0 and x2 = 0
                        //
                        //   0 = a11 * x1 + a12 * 0 + b1'
                        // vn2 = a21 * x1 + a22 * 0 + b2'
                        //
                        x.X = -cp1.NormalMass * b.X;
                        x.Y = 0.0f;
                        vn1 = 0.0f;
                        vn2 = vc.K.Ex.Y * x.X + b.Y;

                        if (x.X >= 0.0f && vn2 >= 0.0f)
                        {
                            // Get the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 p1 = d.X * normal;
                            Vector2 p2 = d.Y * normal;
                            vA -= mA * (p1 + p2);
                            wA -= iA * (MathUtils.Cross(cp1.RA, p1) + MathUtils.Cross(cp2.RA, p2));

                            vB += mB * (p1 + p2);
                            wB += iB * (MathUtils.Cross(cp1.RB, p1) + MathUtils.Cross(cp2.RB, p2));

                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            // Postconditions
                            dv1 = vB + MathUtils.Cross(wB, cp1.rB) - vA - MathUtils.Cross(wA, cp1.rA);

                            // Compute normal velocity
                            vn1 = Vector2.Dot(dv1, normal);

                            Debug.Assert(Math.Abs(vn1 - cp1.VelocityBias) < k_errorTol);
#endif
                            break;
                        }

                        //
                        // Case 3: vn2 = 0 and x1 = 0
                        //
                        // vn1 = a11 * 0 + a12 * x2 + b1'
                        //   0 = a21 * 0 + a22 * x2 + b2'
                        //
                        x.X = 0.0f;
                        x.Y = -cp2.NormalMass * b.Y;
                        vn1 = vc.K.Ey.X * x.Y + b.X;
                        vn2 = 0.0f;

                        if (x.Y >= 0.0f && vn1 >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 p1 = d.X * normal;
                            Vector2 p2 = d.Y * normal;
                            vA -= mA * (p1 + p2);
                            wA -= iA * (MathUtils.Cross(cp1.RA, p1) + MathUtils.Cross(cp2.RA, p2));

                            vB += mB * (p1 + p2);
                            wB += iB * (MathUtils.Cross(cp1.RB, p1) + MathUtils.Cross(cp2.RB, p2));

                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;

#if B2_DEBUG_SOLVER
                            // Postconditions
                            dv2 = vB + MathUtils.Cross(wB, cp2.rB) - vA - MathUtils.Cross(wA, cp2.rA);

                            // Compute normal velocity
                            vn2 = Vector2.Dot(dv2, normal);

                            Debug.Assert(Math.Abs(vn2 - cp2.VelocityBias) < k_errorTol);
#endif
                            break;
                        }

                        //
                        // Case 4: x1 = 0 and x2 = 0
                        //
                        // vn1 = b1
                        // vn2 = b2;
                        x.X = 0.0f;
                        x.Y = 0.0f;
                        vn1 = b.X;
                        vn2 = b.Y;

                        if (vn1 >= 0.0f && vn2 >= 0.0f)
                        {
                            // Resubstitute for the incremental impulse
                            Vector2 d = x - a;

                            // Apply incremental impulse
                            Vector2 p1 = d.X * normal;
                            Vector2 p2 = d.Y * normal;
                            vA -= mA * (p1 + p2);
                            wA -= iA * (MathUtils.Cross(cp1.RA, p1) + MathUtils.Cross(cp2.RA, p2));

                            vB += mB * (p1 + p2);
                            wB += iB * (MathUtils.Cross(cp1.RB, p1) + MathUtils.Cross(cp2.RB, p2));

                            // Accumulate
                            cp1.NormalImpulse = x.X;
                            cp2.NormalImpulse = x.Y;
                        }

                        // No solution, give up. This is hit sometimes, but it doesn't seem to matter.
                        break;
                    }
                }

                velocities[indexA].V = vA;
                velocities[indexA].W = wA;
                velocities[indexB].V = vB;
                velocities[indexB].W = wB;
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Resets the step
        /// </summary>
        /// <param name="step">The step</param>
        /// <param name="count">The count</param>
        /// <param name="contacts">The contacts</param>
        /// <param name="positions">The positions</param>
        /// <param name="velocities">The velocities</param>
        public void Reset(TimeStep step, int count, Contact[] contacts, Position[] positions, Velocity[] velocities)
        {
            this.step       = step;
            this.count      = count;
            this.positions  = positions;
            this.velocities = velocities;
            this.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 < this.count; ++i)
            {
                Contact contact = contacts[i];

                Fixture  fixtureA = contact.FixtureA;
                Fixture  fixtureB = contact.FixtureB;
                Shape    shapeA   = fixtureA.Shape;
                Shape    shapeB   = fixtureB.Shape;
                float    radiusA  = shapeA.RadiusPrivate;
                float    radiusB  = shapeB.RadiusPrivate;
                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.Threshold    = contact.RestitutionThreshold;
                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 (step.WarmStarting)
                    {
                        vcp.NormalImpulse  = this.step.DeltaTimeRatio * cp.NormalImpulse;
                        vcp.TangentImpulse = this.step.DeltaTimeRatio * 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;
                }
            }
        }