Example #1
0
    public void Initialize(b2ContactPositionConstraint pc, b2Transform xfA, b2Transform xfB, int index)
    {
        Debug.Assert(pc.pointCount > 0);

        switch (pc.type)
        {
        case b2Manifold.Type.e_circles:
        {
            b2Vec2 pointA = Utils.b2Mul(xfA, pc.localPoint);
            b2Vec2 pointB = Utils.b2Mul(xfB, pc.localPoints[0]);
            normal = pointB - pointA;
            normal.Normalize();
            point      = 0.5f * (pointA + pointB);
            separation = Utils.b2Dot(pointB - pointA, normal) - pc.radiusA - pc.radiusB;
        }
        break;

        case b2Manifold.Type.e_faceA:
        {
            normal = Utils.b2Mul(xfA.q, pc.localNormal);
            b2Vec2 planePoint = Utils.b2Mul(xfA, pc.localPoint);

            b2Vec2 clipPoint = Utils.b2Mul(xfB, pc.localPoints[index]);
            separation = Utils.b2Dot(clipPoint - planePoint, normal) - pc.radiusA - pc.radiusB;
            point      = clipPoint;
        }
        break;

        case b2Manifold.Type.e_faceB:
        {
            normal = Utils.b2Mul(xfB.q, pc.localNormal);
            b2Vec2 planePoint = Utils.b2Mul(xfB, pc.localPoint);

            b2Vec2 clipPoint = Utils.b2Mul(xfA, pc.localPoints[index]);
            separation = Utils.b2Dot(clipPoint - planePoint, normal) - pc.radiusA - pc.radiusB;
            point      = clipPoint;

            // Ensure normal points from A to B
            normal = -normal;
        }
        break;
        }
    }
Example #2
0
    public b2ContactSolver(b2ContactSolverDef def)
    {
        m_step  = def.step;
        m_count = def.count;
        m_positionConstraints = Arrays.InitializeWithDefaultInstances <b2ContactPositionConstraint>(m_count);
        m_velocityConstraints = Arrays.InitializeWithDefaultInstances <b2ContactVelocityConstraint>(m_count);
        m_positions           = def.positions;
        m_velocities          = def.velocities;
        m_contacts            = def.contacts;

        // Initialize position independent portions of the constraints.
        for (int i = 0; i < m_count; ++i)
        {
            b2Contact contact = m_contacts[i];

            b2Fixture  fixtureA = contact.m_fixtureA;
            b2Fixture  fixtureB = contact.m_fixtureB;
            b2Shape    shapeA   = fixtureA.GetShape();
            b2Shape    shapeB   = fixtureB.GetShape();
            float      radiusA  = shapeA.m_radius;
            float      radiusB  = shapeB.m_radius;
            b2Body     bodyA    = fixtureA.GetBody();
            b2Body     bodyB    = fixtureB.GetBody();
            b2Manifold manifold = contact.GetManifold();

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

            b2ContactVelocityConstraint vc = m_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.SetZero();
            vc.normalMass.SetZero();

            b2ContactPositionConstraint pc = m_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)
            {
                b2ManifoldPoint           cp  = manifold.points[j];
                b2VelocityConstraintPoint vcp = vc.points[j];

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

                vcp.rA.SetZero();
                vcp.rB.SetZero();
                vcp.normalMass   = 0.0f;
                vcp.tangentMass  = 0.0f;
                vcp.velocityBias = 0.0f;

                pc.localPoints[j] = cp.localPoint;
            }
        }
    }
Example #3
0
    // Sequential position solver for position constraints.
    public bool SolveTOIPositionConstraints(int toiIndexA, int toiIndexB)
    {
        float minSeparation = 0.0f;

        for (int i = 0; i < m_count; ++i)
        {
            b2ContactPositionConstraint pc = m_positionConstraints[i];

            int    indexA       = pc.indexA;
            int    indexB       = pc.indexB;
            b2Vec2 localCenterA = new b2Vec2(pc.localCenterA);
            b2Vec2 localCenterB = new b2Vec2(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;
            }

            b2Vec2 cA = m_positions[indexA].c;
            float  aA = m_positions[indexA].a;

            b2Vec2 cB = m_positions[indexB].c;
            float  aB = m_positions[indexB].a;

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

                b2PositionSolverManifold psm = new b2PositionSolverManifold();
                psm.Initialize(pc, xfA, xfB, j);
                b2Vec2 normal = new b2Vec2(psm.normal);

                b2Vec2 point      = new b2Vec2(psm.point);
                float  separation = psm.separation;

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

                // Track max constraint error.
                minSeparation = Utils.b2Min(minSeparation, separation);

                // Prevent large corrections and allow slop.
                float C = Utils.b2Clamp(Settings.b2_toiBaugarte * (separation + Settings.b2_linearSlop), -Settings.b2_maxLinearCorrection, 0.0f);

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

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

                b2Vec2 P = impulse * normal;

                cA -= mA * P;
                aA -= iA * Utils.b2Cross(rA, P);

                cB += mB * P;
                aB += iB * Utils.b2Cross(rB, P);
            }

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

            m_positions[indexB].c = cB;
            m_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.b2_linearSlop);
    }
Example #4
0
    // Initialize position dependent portions of the velocity constraints.
    public void InitializeVelocityConstraints()
    {
        for (int i = 0; i < m_count; ++i)
        {
            b2ContactVelocityConstraint vc = m_velocityConstraints[i];
            b2ContactPositionConstraint pc = m_positionConstraints[i];

            float      radiusA  = pc.radiusA;
            float      radiusB  = pc.radiusB;
            b2Manifold 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;
            b2Vec2 localCenterA = new b2Vec2(pc.localCenterA);
            b2Vec2 localCenterB = new b2Vec2(pc.localCenterB);

            b2Vec2 cA = m_positions[indexA].c;
            float  aA = m_positions[indexA].a;
            b2Vec2 vA = m_velocities[indexA].v;
            float  wA = m_velocities[indexA].w;

            b2Vec2 cB = m_positions[indexB].c;
            float  aB = m_positions[indexB].a;
            b2Vec2 vB = m_velocities[indexB].v;
            float  wB = m_velocities[indexB].w;

            Debug.Assert(manifold.pointCount > 0);

            b2Transform xfA = new b2Transform();
            b2Transform xfB = new b2Transform();
            xfA.q.Set(aA);
            xfB.q.Set(aB);
            xfA.p = cA - Utils.b2Mul(xfA.q, localCenterA);
            xfB.p = cB - Utils.b2Mul(xfB.q, localCenterB);

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

            vc.normal = worldManifold.normal;

            int pointCount = vc.pointCount;
            for (int j = 0; j < pointCount; ++j)
            {
                b2VelocityConstraintPoint vcp = vc.points[j];

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

                float rnA = Utils.b2Cross(vcp.rA, vc.normal);
                float rnB = Utils.b2Cross(vcp.rB, vc.normal);

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

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

                b2Vec2 tangent = Utils.b2Cross(vc.normal, 1.0f);

                float rtA = Utils.b2Cross(vcp.rA, tangent);
                float rtB = Utils.b2Cross(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 = Utils.b2Dot(vc.normal, vB + Utils.b2Cross(wB, vcp.rB) - vA - Utils.b2Cross(wA, vcp.rA));
                if (vRel < -Settings.b2_velocityThreshold)
                {
                    vcp.velocityBias = -vc.restitution * vRel;
                }
            }

            // If we have two points, then prepare the block solver.
            if (vc.pointCount == 2 && Utils.g_blockSolve)
            {
                b2VelocityConstraintPoint vcp1 = vc.points[0];
                b2VelocityConstraintPoint vcp2 = vc.points[1];

                float rn1A = Utils.b2Cross(vcp1.rA, vc.normal);
                float rn1B = Utils.b2Cross(vcp1.rB, vc.normal);
                float rn2A = Utils.b2Cross(vcp2.rA, vc.normal);
                float rn2B = Utils.b2Cross(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.pointCount = 1;
                }
            }
        }
    }