示例#1
0
        protected override void Draw(Settings settings)
        {
            base.Draw(settings);

            b2Manifold manifold = new b2Manifold();
            b2Collision.b2CollidePolygons(manifold, m_polygonA, ref m_transformA, m_polygonB, ref m_transformB);

            b2WorldManifold worldManifold = new b2WorldManifold();
            worldManifold.Initialize(manifold, ref m_transformA, m_polygonA.Radius, ref m_transformB, m_polygonB.Radius);

            m_debugDraw.DrawString(5, m_textLine, "point count = {0}", manifold.pointCount);
            m_textLine += 15;

            {
                b2Color color = new b2Color(0.9f, 0.9f, 0.9f);
                b2Vec2[] v = new b2Vec2[b2Settings.b2_maxPolygonVertices];
                for (int i = 0; i < m_polygonA.VertexCount; ++i)
                {
                    v[i] = b2Math.b2Mul(m_transformA, m_polygonA.Vertices[i]);
                }
                m_debugDraw.DrawPolygon(v, m_polygonA.VertexCount, color);

                for (int i = 0; i < m_polygonB.VertexCount; ++i)
                {
                    v[i] = b2Math.b2Mul(m_transformB, m_polygonB.Vertices[i]);
                }
                m_debugDraw.DrawPolygon(v, m_polygonB.VertexCount, color);
            }

            for (int i = 0; i < manifold.pointCount; ++i)
            {
                m_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, new b2Color(0.9f, 0.3f, 0.3f));
            }
        }
示例#2
0
        // Initialize position dependent portions of the velocity constraints.
        public virtual 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 = pc.localCenterA;
                b2Vec2 localCenterB = 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 = b2Transform.Create(), xfB = b2Transform.Create();
                xfA.q.Set(aA);
                xfB.q.Set(aB);
                xfA.p = cA - b2Math.b2Mul(xfA.q, localCenterA);
                xfB.p = cB - b2Math.b2Mul(xfB.q, localCenterB);

                b2WorldManifold worldManifold = new b2WorldManifold();
                worldManifold.Initialize(ref 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 = b2Math.b2Cross(vcp.rA, vc.normal);
                    float rnB = b2Math.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 = b2Math.b2Cross(vc.normal, 1.0f);

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

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

                    float rn1A = b2Math.b2Cross(vcp1.rA, vc.normal);
                    float rn1B = b2Math.b2Cross(vcp1.rB, vc.normal);
                    float rn2A = b2Math.b2Cross(vcp2.rA, vc.normal);
                    float rn2B = b2Math.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.
                    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;
                    }
                }
                m_positionConstraints[i] = pc;
                m_velocityConstraints[i] = vc;
            }
        }
示例#3
0
        public virtual void GetWorldManifold(b2WorldManifold worldManifold)
        {
            b2Body bodyA = m_fixtureA.Body;
            b2Body bodyB = m_fixtureB.Body;
            b2Shape shapeA = m_fixtureA.Shape;
            b2Shape shapeB = m_fixtureB.Shape;

            worldManifold.Initialize(ref m_manifold, bodyA.Transform, shapeA.Radius, bodyB.Transform, shapeB.Radius);
        }
示例#4
0
            public override void PreSolve(Box2D.Dynamics.Contacts.b2Contact contact, ref Box2D.Collision.b2Manifold oldManifold)
            {
                b2Manifold manifold = contact.GetManifold();

                if (manifold.pointCount == 0)
                {
                    return;
                }

                b2Fixture fixtureA = contact.GetFixtureA();
                b2Fixture fixtureB = contact.GetFixtureB();

                b2PointState[] state1 = new b2PointState[b2Settings.b2_maxManifoldPoints];
                b2PointState[] state2 = new b2PointState[b2Settings.b2_maxManifoldPoints];
                b2Collision.b2GetPointStates(state1, state2, ref oldManifold, ref manifold);

                b2WorldManifold worldManifold = new b2WorldManifold();
                contact.GetWorldManifold(ref worldManifold);

                for (int i = 0; i < manifold.pointCount &&  m_pointCount < k_maxContactPoints; ++i)
                {
                    ContactPoint cp = m_points[m_pointCount];
                    cp.fixtureA = fixtureA;
                    cp.fixtureB = fixtureB;
                    cp.position = worldManifold.points[i];
                    cp.normal = worldManifold.normal;
                    cp.state = state2[i];
                    m_points[m_pointCount] = cp;
                    ++m_pointCount;
                }
            }