示例#1
0
        /*
         * 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);
        }
示例#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);
    }
示例#3
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));
            }
        }
示例#4
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;
            }
        }
示例#5
0
        public void Initialize(b2TimeStep step, List <b2Contact> contacts, int contactCount, object allocator)
        {
            b2Contact contact;

            m_step.Set(step);

            m_allocator = allocator;

            int     i;
            b2Vec2  tVec;
            b2Mat22 tMat;

            m_constraintCount = contactCount;

            // fill vector to hold enough constraints
            while (m_constraints.Count < m_constraintCount)
            {
                m_constraints.Add(new b2ContactConstraint());
            }

            for (i = 0; i < contactCount; ++i)
            {
                contact = contacts[i];
                b2Fixture  fixtureA    = contact.m_fixtureA;
                b2Fixture  fixtureB    = contact.m_fixtureB;
                b2Shape    shapeA      = fixtureA.m_shape;
                b2Shape    shapeB      = fixtureB.m_shape;
                float      radiusA     = shapeA.m_radius;
                float      radiusB     = shapeB.m_radius;
                b2Body     bodyA       = fixtureA.m_body;
                b2Body     bodyB       = fixtureB.m_body;
                b2Manifold manifold    = contact.GetManifold();
                float      friction    = b2Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction());;
                float      restitution = b2Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution());

                //var vA:b2Vec2 = bodyA.m_linearVelocity.Copy();
                float vAX = bodyA.m_linearVelocity.x;
                float vAY = bodyA.m_linearVelocity.y;
                //var vB:b2Vec2 = bodyB.m_linearVelocity.Copy();
                float vBX = bodyB.m_linearVelocity.x;
                float vBY = bodyB.m_linearVelocity.y;
                float wA  = bodyA.m_angularVelocity;
                float wB  = bodyB.m_angularVelocity;

                b2Settings.b2Assert(manifold.m_pointCount > 0);

                s_worldManifold.Initialize(manifold, bodyA.m_xf, radiusA, bodyB.m_xf, radiusB);

                float normalX = s_worldManifold.m_normal.x;
                float normalY = s_worldManifold.m_normal.y;

                b2ContactConstraint cc = m_constraints[i];
                cc.bodyA    = bodyA;      //p
                cc.bodyB    = bodyB;      //p
                cc.manifold = manifold;   //p
                //c.normal = normal;
                cc.normal.x   = normalX;
                cc.normal.y   = normalY;
                cc.pointCount = manifold.m_pointCount;
                cc.friction   = friction;
                //-----------------------------修改 2015/12/10 13:07 by kingBook------------------
                float bevel = 10.0f;
                float planeAngle;
                int   vx;
                if (!bodyA.m_allowBevelSlither || bodyA.m_uphillZeroFriction)
                {
                    planeAngle = Mathf.Atan2(normalY, normalX) * 57.3f + 90.0f;
                    vx         = (int)(bodyA.m_linearVelocity.x);
                    if (planeAngle < 0.0f)
                    {
                        planeAngle += 360.0f;
                    }
                    if ((planeAngle > bevel && planeAngle < 90.0f - bevel) || (planeAngle > 180.0f + bevel && planeAngle < 270.0f - bevel))//斜面 左上角-右下角
                    {
                        if (!bodyA.m_allowBevelSlither)
                        {
                            if (vx >= 0.0f)
                            {
                                cc.friction = 1.0f;
                            }
                        }
                        if (bodyA.m_uphillZeroFriction)
                        {
                            if (vx < 0.0f)
                            {
                                cc.friction = 0.0f;
                            }
                        }
                    }
                    else if ((planeAngle > 90.0f && planeAngle < 180.0f - bevel) || (planeAngle > 270.0f + bevel && planeAngle < 360.0f - bevel))//斜面 左下角-右上角
                    {
                        if (!bodyA.m_allowBevelSlither)
                        {
                            if (vx <= 0.0f)
                            {
                                cc.friction = 1.0f;
                            }
                        }
                        if (bodyA.m_uphillZeroFriction)
                        {
                            if (vx > 0.0f)
                            {
                                cc.friction = 0.0f;
                            }
                        }
                    }
                }
                else if (!bodyB.m_allowBevelSlither || bodyB.m_uphillZeroFriction)
                {
                    planeAngle = Mathf.Atan2(-normalY, -normalX) * 57.3f + 90.0f;
                    vx         = (int)(bodyB.m_linearVelocity.x);
                    if (planeAngle < 0.0f)
                    {
                        planeAngle += 360.0f;
                    }
                    if ((planeAngle > bevel && planeAngle < 90.0f - bevel) || (planeAngle > 180.0f + bevel && planeAngle < 270.0f - bevel))//斜面 左上角-右下角
                    {
                        if (!bodyB.m_allowBevelSlither)
                        {
                            if (vx >= 0.0f)
                            {
                                cc.friction = 1.0f;
                            }
                        }
                        if (bodyB.m_uphillZeroFriction)
                        {
                            if (vx < 0.0f)
                            {
                                cc.friction = 0.0f;
                            }
                        }
                    }
                    else if ((planeAngle > 90.0f && planeAngle < 180.0f - bevel) || (planeAngle > 270.0f + bevel && planeAngle < 360.0f - bevel))//斜面 左下角-右上角
                    {
                        if (!bodyB.m_allowBevelSlither)
                        {
                            if (vx <= 0.0f)
                            {
                                cc.friction = 1.0f;
                            }
                        }
                        if (bodyB.m_uphillZeroFriction)
                        {
                            if (vx > 0)
                            {
                                cc.friction = 0.0f;
                            }
                        }
                    }
                }

                if (bodyA.m_isIgnoreFrictionX || bodyB.m_isIgnoreFrictionX)
                {
                    if (Mathf.Abs(normalY) > 0.9f)
                    {
                        cc.friction = 0.0f;
                    }
                }
                else if (bodyA.m_isIgnoreFrictionY || bodyB.m_isIgnoreFrictionY)
                {
                    if (Mathf.Abs(normalX) > 0.9f)
                    {
                        cc.friction = 0.0f;
                    }
                }
                //-----------------------------修改结束------------------
                cc.restitution = restitution;

                cc.localPlaneNormal.x = manifold.m_localPlaneNormal.x;
                cc.localPlaneNormal.y = manifold.m_localPlaneNormal.y;
                cc.localPoint.x       = manifold.m_localPoint.x;
                cc.localPoint.y       = manifold.m_localPoint.y;
                cc.radius             = radiusA + radiusB;
                cc.type = manifold.m_type;

                for (int k = 0; k < cc.pointCount; ++k)
                {
                    b2ManifoldPoint          cp  = manifold.m_points[k];
                    b2ContactConstraintPoint ccp = cc.points[k];

                    ccp.normalImpulse  = cp.m_normalImpulse;
                    ccp.tangentImpulse = cp.m_tangentImpulse;

                    ccp.localPoint.SetV(cp.m_localPoint);

                    float rAX = ccp.rA.x = s_worldManifold.m_points[k].x - bodyA.m_sweep.c.x;
                    float rAY = ccp.rA.y = s_worldManifold.m_points[k].y - bodyA.m_sweep.c.y;
                    float rBX = ccp.rB.x = s_worldManifold.m_points[k].x - bodyB.m_sweep.c.x;
                    float rBY = ccp.rB.y = s_worldManifold.m_points[k].y - bodyB.m_sweep.c.y;

                    float rnA = rAX * normalY - rAY * normalX;            //b2Math.b2Cross(r1, normal);
                    float rnB = rBX * normalY - rBY * normalX;            //b2Math.b2Cross(r2, normal);

                    rnA *= rnA;
                    rnB *= rnB;

                    float kNormal = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rnA + bodyB.m_invI * rnB;
                    //b2Settings.b2Assert(kNormal > Number.MIN_VALUE);
                    ccp.normalMass = 1.0f / kNormal;

                    float kEqualized = bodyA.m_mass * bodyA.m_invMass + bodyB.m_mass * bodyB.m_invMass;
                    kEqualized += bodyA.m_mass * bodyA.m_invI * rnA + bodyB.m_mass * bodyB.m_invI * rnB;
                    //b2Assert(kEqualized > Number.MIN_VALUE);
                    ccp.equalizedMass = 1.0f / kEqualized;

                    //var tangent:b2Vec2 = b2Math.b2CrossVF(normal, 1.0);
                    float tangentX = normalY;
                    float tangentY = -normalX;

                    //var rtA:Number = b2Math.b2Cross(rA, tangent);
                    float rtA = rAX * tangentY - rAY * tangentX;
                    //var rtB:Number = b2Math.b2Cross(rB, tangent);
                    float rtB = rBX * tangentY - rBY * tangentX;

                    rtA *= rtA;
                    rtB *= rtB;

                    float kTangent = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rtA + bodyB.m_invI * rtB;
                    //b2Settings.b2Assert(kTangent > Number.MIN_VALUE);
                    ccp.tangentMass = 1.0f / kTangent;

                    // Setup a velocity bias for restitution.
                    ccp.velocityBias = 0.0f;
                    //b2Dot(c.normal, vB + b2Cross(wB, rB) - vA - b2Cross(wA, rA));
                    float tX = vBX + (-wB * rBY) - vAX - (-wA * rAY);
                    float tY = vBY + (wB * rBX) - vAY - (wA * rAX);
                    //var vRel:Number = b2Dot(cc.normal, t);
                    float vRel = cc.normal.x * tX + cc.normal.y * tY;
                    if (vRel < -b2Settings.b2_velocityThreshold)
                    {
                        ccp.velocityBias += -cc.restitution * vRel;
                    }
                }

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

                    float invMassA = bodyA.m_invMass;
                    float invIA    = bodyA.m_invI;
                    float invMassB = bodyB.m_invMass;
                    float invIB    = bodyB.m_invI;

                    //var rn1A:Number = b2Cross(ccp1.rA, normal);
                    //var rn1B:Number = b2Cross(ccp1.rB, normal);
                    //var rn2A:Number = b2Cross(ccp2.rA, normal);
                    //var rn2B:Number = b2Cross(ccp2.rB, normal);
                    float rn1A = ccp1.rA.x * normalY - ccp1.rA.y * normalX;
                    float rn1B = ccp1.rB.x * normalY - ccp1.rB.y * normalX;
                    float rn2A = ccp2.rA.x * normalY - ccp2.rA.y * normalX;
                    float rn2B = ccp2.rB.x * normalY - ccp2.rB.y * normalX;

                    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.col1.Set(k11, k12);
                        cc.K.col2.Set(k12, k22);
                        cc.K.GetInverse(cc.normalMass);
                    }
                    else
                    {
                        // The constraints are redundant, just use one.
                        // TODO_ERIN use deepest?
                        cc.pointCount = 1;
                    }
                }
            }

            //b2Settings.b2Assert(count == m_constraintCount);
        }