/*
         * public virtual void SetManifold(ref b2Manifold m)
         * {
         *  m_manifold = m;
         *  m_manifold.CopyPointsFrom(ref m);
         * }
         */
        public virtual void GetWorldManifold(ref b2WorldManifold worldManifold)
        {
            b2Body  bodyA  = FixtureA.Body;
            b2Body  bodyB  = FixtureB.Body;
            b2Shape shapeA = FixtureA.Shape;
            b2Shape shapeB = FixtureB.Shape;

            worldManifold.Initialize(m_manifold, ref bodyA.Transform, shapeA.Radius, ref bodyB.Transform, shapeB.Radius);
        }
Beispiel #2
0
    /// Get the world manifold.
    public void GetWorldManifold(b2WorldManifold worldManifold)
    {
        b2Body  bodyA  = m_fixtureA.GetBody();
        b2Body  bodyB  = m_fixtureB.GetBody();
        b2Shape shapeA = m_fixtureA.GetShape();
        b2Shape shapeB = m_fixtureB.GetShape();

        worldManifold.Initialize(m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius);
    }
        private void CalculateDetails()
        {
            _DetailsCalculated = true;
            _ContactPoints     = new TypeVector2 <Vector2>();

            b2WorldManifold worldManifold = new b2WorldManifold();

            _Contact.GetWorldManifold(worldManifold);

            _Normal          = worldManifold.normal.ToVector2();
            _ContactPoints.x = Box2d.b2Vec2Array_getitem(worldManifold.points, 0).ToVector2();
            _ContactPoints.y = Box2d.b2Vec2Array_getitem(worldManifold.points, 1).ToVector2();
        }
        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));
            }
        }
Beispiel #5
0
        public override void PreSolve(b2Contact contact, ref 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];
                if (cp == null)
                {
                    cp = new ContactPoint();
                    m_points[m_pointCount] = cp;
                }
                cp.fixtureA = fixtureA;
                cp.fixtureB = fixtureB;
                cp.position = worldManifold.points[i];
                cp.normal   = worldManifold.normal;
                cp.state    = state2[i];
                ++m_pointCount;
            }
        }
Beispiel #6
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.Identity, xfB = b2Transform.Identity;
                xfA.q.Set(aA);
                xfB.q.Set(aB);
                xfA.p = cA - b2Math.b2Mul(ref xfA.q, ref localCenterA);
                xfB.p = cB - b2Math.b2Mul(ref xfB.q, ref 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(ref vcp.rA, ref vc.normal);
                    float rnB = b2Math.b2Cross(ref vcp.rB, ref vc.normal);

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

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

                    b2Vec2 tangent = vc.normal.UnitCross(); //  b2Math.b2Cross(vc.normal, 1.0f);

                    float rtA = b2Math.b2Cross(ref vcp.rA, ref tangent);
                    float rtB = b2Math.b2Cross(ref vcp.rB, ref 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, ref vcp.rB) - vA - b2Math.b2Cross(wA, ref 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(ref vcp1.rA, ref vc.normal);
                    float rn1B = b2Math.b2Cross(ref vcp1.rB, ref vc.normal);
                    float rn2A = b2Math.b2Cross(ref vcp2.rA, ref vc.normal);
                    float rn2B = b2Math.b2Cross(ref vcp2.rB, ref 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;
            }
        }
Beispiel #7
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(b2WorldManifold obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Beispiel #8
0
 public void GetWorldManifold(b2WorldManifold worldManifold)
 {
     Box2DPINVOKE.b2Contact_GetWorldManifold(swigCPtr, b2WorldManifold.getCPtr(worldManifold));
 }