Пример #1
0
        void DrawShape(Fixture fixture, XForm xf, Color color)
        {
            Color coreColor = ColorEx.FromScRgb(0.9f, 0.6f, 0.6f);

            switch (fixture.ShapeType)
            {
            case ShapeType.Circle:
            {
                CircleShape circle = (CircleShape)fixture.GetShape();

                Vector2 center = MathUtils.Multiply(ref xf, circle._p);
                float   radius = circle._radius;
                Vector2 axis   = xf.R.col1;

                DebugDraw.DrawSolidCircle(center, radius, axis, color);
            }
            break;

            case ShapeType.Polygon:
            {
                PolygonShape poly        = (PolygonShape)fixture.GetShape();
                int          vertexCount = poly._vertexCount;
                Debug.Assert(vertexCount <= Settings.b2_maxPolygonVertices);
                FixedArray8 <Vector2> vertices = new FixedArray8 <Vector2>();

                for (int i = 0; i < vertexCount; ++i)
                {
                    vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]);
                }

                DebugDraw.DrawSolidPolygon(ref vertices, vertexCount, color);
            }
            break;
            }
        }
Пример #2
0
        public void Reset(ref TimeStep step, List <Contact> contacts)
        {
            _step     = step;
            _contacts = contacts;

            int contactCount    = contacts.Count;
            int constraintCount = contactCount;

            _constraints.Clear();
            for (int i = 0; i < constraintCount; i++)
            {
                _constraints.Add(new ContactConstraint());
            }

            for (int i = 0; i < constraintCount; ++i)
            {
                Contact contact = contacts[i];

                Fixture  fixtureA = contact._fixtureA;
                Fixture  fixtureB = contact._fixtureB;
                Shape    shapeA   = fixtureA.GetShape();
                Shape    shapeB   = fixtureB.GetShape();
                float    radiusA  = shapeA._radius;
                float    radiusB  = shapeB._radius;
                Body     bodyA    = fixtureA.GetBody();
                Body     bodyB    = fixtureB.GetBody();
                Manifold manifold;
                contact.GetManifold(out manifold);

                float friction    = Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction());
                float restitution = Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution());

                Vector2 vA = bodyA._linearVelocity;
                Vector2 vB = bodyB._linearVelocity;
                float   wA = bodyA._angularVelocity;
                float   wB = bodyB._angularVelocity;

                Debug.Assert(manifold._pointCount > 0);

                WorldManifold worldManifold = new WorldManifold(ref manifold, ref bodyA._xf, radiusA, ref bodyB._xf, radiusB);

                ContactConstraint cc = _constraints[i];
                cc.bodyA       = bodyA;
                cc.bodyB       = bodyB;
                cc.manifold    = manifold;
                cc.normal      = worldManifold._normal;
                cc.pointCount  = manifold._pointCount;
                cc.friction    = friction;
                cc.restitution = restitution;

                cc.localPlaneNormal = manifold._localPlaneNormal;
                cc.localPoint       = manifold._localPoint;
                cc.radius           = radiusA + radiusB;
                cc.type             = manifold._type;

                for (int j = 0; j < cc.pointCount; ++j)
                {
                    ManifoldPoint          cp  = manifold._points[j];
                    ContactConstraintPoint ccp = cc.points[j];

                    ccp.normalImpulse  = cp.NormalImpulse;
                    ccp.tangentImpulse = cp.TangentImpulse;

                    ccp.localPoint = cp.LocalPoint;

                    ccp.rA = worldManifold._points[j] - bodyA._sweep.c;
                    ccp.rB = worldManifold._points[j] - bodyB._sweep.c;

                    float rnA = MathUtils.Cross(ccp.rA, cc.normal);
                    float rnB = MathUtils.Cross(ccp.rB, cc.normal);
                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA._invMass + bodyB._invMass + bodyA._invI * rnA + bodyB._invI * rnB;

                    Debug.Assert(kNormal > Settings.b2_FLT_EPSILON);
                    ccp.normalMass = 1.0f / kNormal;

                    float kEqualized = bodyA._mass * bodyA._invMass + bodyB._mass * bodyB._invMass;
                    kEqualized += bodyA._mass * bodyA._invI * rnA + bodyB._mass * bodyB._invI * rnB;

                    Debug.Assert(kEqualized > Settings.b2_FLT_EPSILON);
                    ccp.equalizedMass = 1.0f / kEqualized;

                    Vector2 tangent = MathUtils.Cross(cc.normal, 1.0f);

                    float rtA = MathUtils.Cross(ccp.rA, tangent);
                    float rtB = MathUtils.Cross(ccp.rB, tangent);
                    rtA *= rtA;
                    rtB *= rtB;

                    float kTangent = bodyA._invMass + bodyB._invMass + bodyA._invI * rtA + bodyB._invI * rtB;

                    Debug.Assert(kTangent > Settings.b2_FLT_EPSILON);
                    ccp.tangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.velocityBias = 0.0f;
                    float vRel = Vector2.Dot(cc.normal, vB + MathUtils.Cross(wB, ccp.rB) - vA - MathUtils.Cross(wA, ccp.rA));
                    if (vRel < -Settings.b2_velocityThreshold)
                    {
                        ccp.velocityBias = -cc.restitution * vRel;
                    }

                    cc.points[j] = ccp;
                }

                // If we have two points, then prepare the block solver.
                if (cc.pointCount == 2)
                {
                    ContactConstraintPoint ccp1 = cc.points[0];
                    ContactConstraintPoint ccp2 = cc.points[1];

                    float invMassA = bodyA._invMass;
                    float invIA    = bodyA._invI;
                    float invMassB = bodyB._invMass;
                    float invIB    = bodyB._invI;

                    float rn1A = MathUtils.Cross(ccp1.rA, cc.normal);
                    float rn1B = MathUtils.Cross(ccp1.rB, cc.normal);
                    float rn2A = MathUtils.Cross(ccp2.rA, cc.normal);
                    float rn2B = MathUtils.Cross(ccp2.rB, cc.normal);

                    float k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
                    float k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
                    float k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;

                    // Ensure a reasonable condition number.
                    float k_maxConditionNumber = 100.0f;
                    if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12))
                    {
                        // K is safe to invert.
                        cc.K          = new Mat22(new Vector2(k11, k12), new Vector2(k12, k22));
                        cc.normalMass = cc.K.GetInverse();
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.pointCount = 1;
                    }
                }

                _constraints[i] = cc;
            }
        }
Пример #3
0
        void DrawShape(Fixture fixture, XForm xf, Color color)
        {
            Color coreColor = ColorEx.FromScRgb(0.9f, 0.6f, 0.6f);

            switch (fixture.ShapeType)
            {
            case ShapeType.Circle:
                {
                    CircleShape circle = (CircleShape)fixture.GetShape();

                    Vector2 center = MathUtils.Multiply(ref xf, circle._p);
                    float radius = circle._radius;
                    Vector2 axis = xf.R.col1;

                    DebugDraw.DrawSolidCircle(center, radius, axis, color);
                }
                break;

            case ShapeType.Polygon:
                {
                    PolygonShape poly = (PolygonShape)fixture.GetShape();
                    int vertexCount = poly._vertexCount;
                    Debug.Assert(vertexCount <= Settings.b2_maxPolygonVertices);
                    FixedArray8<Vector2> vertices = new FixedArray8<Vector2>();

                    for (int i = 0; i < vertexCount; ++i)
                    {
                        vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]);
                    }

                    DebugDraw.DrawSolidPolygon(ref vertices, vertexCount, color);
                }
                break;
            }
        }