public void BeginContact(Contact contact)
        {
            GameObject A = (GameObject)contact.GetFixtureA().GetUserData();
            GameObject B = (GameObject)contact.GetFixtureB().GetUserData();

            Manifold manifold;
            Transform xfA;
            Transform xfB;
            float radiusA;
            float radiusB;

            contact.GetFixtureA().GetBody().GetTransform(out xfA);
            contact.GetFixtureB().GetBody().GetTransform(out xfB);
            radiusA = contact.GetFixtureA().GetShape()._radius;
            radiusB = contact.GetFixtureB().GetShape()._radius;

            contact.GetManifold(out manifold);

            WorldManifold worldManifold = new WorldManifold(ref manifold,ref xfA,radiusA,ref xfB,radiusB);

            Vector2 ptA = worldManifold._points[0];
            Vector2 ptB = worldManifold._points[1];

            //System.Console.Write(" point {0} {1}\n", ptA, ptB);

            ColHdr cHdr;
            cHdr.goID1 = A.getIndexNum();
            cHdr.goID2 = B.getIndexNum();
            cHdr.pos = ptA;

            InputHdr iHdr;
            iHdr.colInfo = cHdr;
            iHdr.input = InputType.Collision;
            iHdr.player = PlayerID.none;
            iHdr.networked = false;

            OutputQueue.pushHeader(iHdr);

            //if (A.CollideAvailable == true && B.CollideAvailable == true)
            //{
            //    if (A.type < B.type)
            //    {
            //        A.Accept(B, ptA);
            //    }
            //    else
            //    {
            //        B.Accept(A, ptA);
            //    }
            //}

            //if (A.type == GameObjType.p1missiles || A.type == GameObjType.p2missiles)
            //{
            //    A.CollideAvailable = false;
            //}

            //if (B.type == GameObjType.p1missiles || B.type == GameObjType.p2missiles)
            //{
            //    B.CollideAvailable = false;
            //}
        }
Exemple #2
0
        public void Initialize(Contact[] contacts, int count, Body toiBody)
        {
            _count   = count;
            _toiBody = toiBody;
            if (_constraints.Length < _count)
            {
                _constraints = new TOIConstraint[Math.Max(_constraints.Length * 2, _count)];
            }

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

                Fixture  fixtureA = contact.GetFixtureA();
                Fixture  fixtureB = contact.GetFixtureB();
                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);

                Debug.Assert(manifold._pointCount > 0);

                TOIConstraint constraint = _constraints[i];
                constraint.bodyA       = bodyA;
                constraint.bodyB       = bodyB;
                constraint.localNormal = manifold._localNormal;
                constraint.localPoint  = manifold._localPoint;
                constraint.type        = manifold._type;
                constraint.pointCount  = manifold._pointCount;
                constraint.radius      = radiusA + radiusB;

                for (int j = 0; j < constraint.pointCount; ++j)
                {
                    constraint.localPoints[j] = manifold._points[j].LocalPoint;
                }

                _constraints[i] = constraint;
            }
        }
Exemple #3
0
        protected void PreSolveCalcPoints(Contact contact, ref Manifold oldManifold)
        {
            Manifold manifold;
            contact.GetManifold(out manifold);

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

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

            FixedArray2<PointState> state1, state2;
            Collision.GetPointStates(out state1, out state2, ref oldManifold, ref manifold);

            WorldManifold worldManifold;
            contact.GetWorldManifold(out worldManifold);

            for (int i = 0; i < manifold._pointCount && _pointCount < k_maxContactPoints; ++i)
            {
                if (fixtureA == null)
                {
                    _points[i] = new ContactPoint();
                }
                ContactPoint cp = _points[_pointCount];
                cp.fixtureA = fixtureA;
                cp.fixtureB = fixtureB;
                cp.position = worldManifold._points[i];
                cp.normal = worldManifold._normal;
                cp.state = state2[i];
                _points[_pointCount] = cp;
                ++_pointCount;
            }
        }
        public override void BeginContact(Contact contact)
        {
            collisionObjA = (V2DSprite)contact.GetFixtureA().GetBody().GetUserData();
            collisionObjB = (V2DSprite)contact.GetFixtureB().GetBody().GetUserData();

            SmuckPlayer p = null;
            V2DSprite nonPlayerObj = null;
            if(collisionObjA is SmuckPlayer)
            {
                p = (SmuckPlayer)collisionObjA;
                nonPlayerObj = collisionObjB;
            }
            else if(collisionObjB is SmuckPlayer)
            {
                p = (SmuckPlayer)collisionObjB;
                nonPlayerObj = collisionObjA;
            }

            if (p != null)
            {
                LaneVehicle v = nonPlayerObj is LaneVehicle ? (LaneVehicle)nonPlayerObj : null;
                if (v != null)
                {
                    if (v.Lane.LaneKind == LaneKind.DrownWater && !collideWithBoats)
                    {
                        if (p.LivingState == LivingState.Alive)
                        {
                            p.aboardVehicle = v;
                        }
                        else
                        {
                            p.DestroyAfterUpdate(); // dont want drowning amin over boats
                        }
                    }
                    else
                    {
                        Manifold m;
                        contact.GetManifold(out m);
                        Vector2 dir = m._localNormal * (p == collisionObjA ? 20 : -20) + v.body.GetLinearVelocity() * 10;
                        if (Math.Abs(dir.Y) < 60)
                        {
                            dir.Y += rnd.Next(-400, 400);
                        }
                        p.isExploding = false;
                        p.body.ApplyLinearImpulse(dir, p.body.GetPosition());
                        float torque = dir.Y > 0 ? 1 : -1;
                        p.body.ApplyTorque(torque);
                        p.body.SetAngularVelocity(rnd.Next(15) * torque);

                        if (p.LivingState == LivingState.Alive) // first hit a whack
                        {
                            stage.audio.PlaySound(Sfx.whack);
                        }
                        stage.audio.PlaySound(Sfx.secondWhack);

                        this.KillPlayer(p);
                    }
                }
                else
                {
                    if (nonPlayerObj.InstanceName.StartsWith("water"))
                    {
                        p.IsOnWater = true;
                        if (p.aboardVehicle == null)
                        {
                            p.Lane = lanes[GetLaneFromY((int)nonPlayerObj.Y)];
                            p.LivingState = LivingState.Dying;
                            stage.audio.PlaySound(Sfx.bigSplash);
                        }
                    }
                    else if (p.LivingState == LivingState.Dying && nonPlayerObj.InstanceName.StartsWith("border"))
                    {
                        // no death icon when flying off left or right side of highway
                        if (nonPlayerObj.Index == 0 || nonPlayerObj.Index == 2)
                        {
                            p.skipDeathMarker = true;
                        }
                        p.DestroyAfterUpdate();
                    }
                }
            }
            else if (collisionObjA is LaneVehicle && collisionObjB is LaneVehicle)
            {
                LaneVehicle vA = (LaneVehicle)collisionObjA;
                LaneVehicle vB = (LaneVehicle)collisionObjB;
                const float boost = 15;
                if (vA.Lane.movesRight && vB.Lane.movesRight)
                {
                    if (vA.Position.X > vB.Position.X)
                    {
                        vA.MaxSpeed = vA.Lane.vehicleSpeed + boost;
                        vB.MaxSpeed = vA.MaxSpeed - boost;
                    }
                    else
                    {
                        vB.MaxSpeed = vB.Lane.vehicleSpeed + boost;
                        vA.MaxSpeed = vB.MaxSpeed - boost;
                    }
                }
                else if (!vA.Lane.movesRight && !vB.Lane.movesRight)
                {
                    if (vA.Position.X > vB.Position.X)
                    {
                        vB.MaxSpeed = vB.Lane.vehicleSpeed + boost;
                        vA.MaxSpeed = vB.MaxSpeed - boost;
                    }
                    else
                    {
                        vA.MaxSpeed = vA.Lane.vehicleSpeed + boost;
                        vB.MaxSpeed = vA.MaxSpeed - boost;
                    }
                }
            }
        }
        public void Reset(Contact[] contacts, int contactCount, float impulseRatio)
        {
            _contacts = contacts;

            _constraintCount = contactCount;

            // grow the array
            if (_constraints == null || _constraints.Length < _constraintCount)
            {
                _constraints = new ContactConstraint[_constraintCount * 2];
            }

            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.localNormal = manifold._localNormal;
                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  = impulseRatio * cp.NormalImpulse;
                    ccp.tangentImpulse = impulseRatio * cp.TangentImpulse;

                    ccp.localPoint = cp.LocalPoint;

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

#if MATH_OVERLOADS
                    float rnA = MathUtils.Cross(ccp.rA, cc.normal);
                    float rnB = MathUtils.Cross(ccp.rB, cc.normal);
#else
                    float rnA = ccp.rA.X * cc.normal.Y - ccp.rA.Y * cc.normal.X;
                    float rnB = ccp.rB.X * cc.normal.Y - ccp.rB.Y * cc.normal.X;
#endif
                    rnA *= rnA;
                    rnB *= rnB;

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

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

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

                    float rtA = MathUtils.Cross(ccp.rA, tangent);
                    float rtB = MathUtils.Cross(ccp.rB, tangent);
#else
                    Vector2 tangent = new Vector2(cc.normal.Y, -cc.normal.X);

                    float rtA = ccp.rA.X * tangent.Y - ccp.rA.Y * tangent.X;
                    float rtB = ccp.rB.X * tangent.Y - ccp.rB.Y * tangent.X;
#endif
                    rtA *= rtA;
                    rtB *= rtB;
                    float kTangent = bodyA._invMass + bodyB._invMass + bodyA._invI * rtA + bodyB._invI * rtB;

                    Debug.Assert(kTangent > Settings.b2_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 = -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.
                    const 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;
            }
        }