예제 #1
0
        /// <summary>
        /// Update the contact manifold and touching status.
        /// Note: do not assume the fixture AABBs are overlapping or are valid.
        /// </summary>
        /// <param name="contactManager">The contact manager.</param>
        internal void Update(ContactManager contactManager)
        {
            Body bodyA = FixtureA.Body;
            Body bodyB = FixtureB.Body;

            if (FixtureA == null || FixtureB == null)
            {
                return;
            }

            if (!ContactManager.CheckCollisionConditions(FixtureA, FixtureB))
            {
                Enabled = false;
                return;
            }

            Manifold oldManifold = Manifold;

            // Re-enable this contact.
            Enabled = true;

            bool touching;
            bool wasTouching = IsTouching;

            bool sensor = FixtureA.IsSensor || FixtureB.IsSensor;

            // Is this contact a sensor?
            if (sensor)
            {
                Shape shapeA = FixtureA.Shape;
                Shape shapeB = FixtureB.Shape;
                touching = Collision.TestOverlap(shapeA, ChildIndexA, shapeB, ChildIndexB, ref bodyA._xf, ref bodyB._xf);

                // Sensors don't generate manifolds.
                Manifold.PointCount = 0;
            }
            else
            {
                Evaluate(ref Manifold, ref bodyA._xf, ref bodyB._xf);
                touching = Manifold.PointCount > 0;

                // Match old contact ids to new contact ids and copy the
                // stored impulses to warm start the solver.
                for (int i = 0; i < Manifold.PointCount; ++i)
                {
                    ManifoldPoint mp2 = Manifold.Points[i];
                    mp2.NormalImpulse  = 0.0f;
                    mp2.TangentImpulse = 0.0f;
                    ContactID id2 = mp2.Id;

                    for (int j = 0; j < oldManifold.PointCount; ++j)
                    {
                        ManifoldPoint mp1 = oldManifold.Points[j];

                        if (mp1.Id.Key == id2.Key)
                        {
                            mp2.NormalImpulse  = mp1.NormalImpulse;
                            mp2.TangentImpulse = mp1.TangentImpulse;
                            break;
                        }
                    }

                    Manifold.Points[i] = mp2;
                }

                if (touching != wasTouching)
                {
                    bodyA.Awake = true;
                    bodyB.Awake = true;
                }
            }

            IsTouching = touching;

            if (wasTouching == false)
            {
                if (touching)
                {
                    if (Settings.AllCollisionCallbacksAgree)
                    {
                        bool enabledA = true, enabledB = true;

                        // Report the collision to both participants. Track which ones returned true so we can
                        // later call OnSeparation if the contact is disabled for a different reason.
                        if (FixtureA.OnCollision != null)
                        {
                            foreach (OnCollisionEventHandler handler in FixtureA.OnCollision.GetInvocationList())
                            {
                                enabledA = handler(FixtureA, FixtureB, this) && enabledA;
                            }
                        }

                        // Reverse the order of the reported fixtures. The first fixture is always the one that the
                        // user subscribed to.
                        if (FixtureB.OnCollision != null)
                        {
                            foreach (OnCollisionEventHandler handler in FixtureB.OnCollision.GetInvocationList())
                            {
                                enabledB = handler(FixtureB, FixtureA, this) && enabledB;
                            }
                        }

                        Enabled = enabledA && enabledB;

                        // BeginContact can also return false and disable the contact
                        if (enabledA && enabledB && contactManager.BeginContact != null)
                        {
                            Enabled = contactManager.BeginContact(this);
                        }
                    }
                    else
                    {
                        //Report the collision to both participants:
                        if (FixtureA.OnCollision != null)
                        {
                            foreach (OnCollisionEventHandler handler in FixtureA.OnCollision.GetInvocationList())
                            {
                                Enabled = handler(FixtureA, FixtureB, this);
                            }
                        }

                        //Reverse the order of the reported fixtures. The first fixture is always the one that the
                        //user subscribed to.
                        if (FixtureB.OnCollision != null)
                        {
                            foreach (OnCollisionEventHandler handler in FixtureB.OnCollision.GetInvocationList())
                            {
                                Enabled = handler(FixtureB, FixtureA, this);
                            }
                        }

                        //BeginContact can also return false and disable the contact
                        if (contactManager.BeginContact != null)
                        {
                            Enabled = contactManager.BeginContact(this);
                        }
                    }

                    // If the user disabled the contact (needed to exclude it in TOI solver) at any point by
                    // any of the callbacks, we need to mark it as not touching and call any separation
                    // callbacks for fixtures that didn't explicitly disable the collision.
                    if (!Enabled)
                    {
                        IsTouching = false;
                    }
                }
            }
            else
            {
                if (touching == false)
                {
                    //Report the separation to both participants:
                    if (FixtureA != null && FixtureA.OnSeparation != null)
                    {
                        FixtureA.OnSeparation(FixtureA, FixtureB);
                    }

                    //Reverse the order of the reported fixtures. The first fixture is always the one that the
                    //user subscribed to.
                    if (FixtureB != null && FixtureB.OnSeparation != null)
                    {
                        FixtureB.OnSeparation(FixtureB, FixtureA);
                    }

                    if (contactManager.EndContact != null)
                    {
                        contactManager.EndContact(this);
                    }
                }
                else
                {
                    if (contactManager.StayContact != null)
                    {
                        contactManager.StayContact(this);
                    }
                }
            }

            if (sensor)
            {
                return;
            }

            if (contactManager.PreSolve != null)
            {
                contactManager.PreSolve(this, ref oldManifold);
            }
        }
예제 #2
0
 internal void Collide()
 {
     for (int i = 0; i < this.ContactList.Count; i++)
     {
         Contact contact     = this.ContactList[i];
         Fixture fixtureA    = contact.FixtureA;
         Fixture fixtureB    = contact.FixtureB;
         int     childIndexA = contact.ChildIndexA;
         int     childIndexB = contact.ChildIndexB;
         Body    body        = fixtureA.Body;
         Body    body2       = fixtureB.Body;
         bool    flag        = !body.Enabled || !body2.Enabled;
         if (!flag)
         {
             bool filterFlag = contact.FilterFlag;
             if (filterFlag)
             {
                 bool flag2 = !body2.ShouldCollide(body);
                 if (flag2)
                 {
                     Contact contact2 = contact;
                     this.Destroy(contact2);
                     goto IL_198;
                 }
                 bool flag3 = !ContactManager.ShouldCollide(fixtureA, fixtureB);
                 if (flag3)
                 {
                     Contact contact3 = contact;
                     this.Destroy(contact3);
                     goto IL_198;
                 }
                 bool flag4 = this.ContactFilter != null && !this.ContactFilter(fixtureA, fixtureB);
                 if (flag4)
                 {
                     Contact contact4 = contact;
                     this.Destroy(contact4);
                     goto IL_198;
                 }
                 contact.FilterFlag = false;
             }
             bool flag5 = body.Awake && body.BodyType > BodyType.Static;
             bool flag6 = body2.Awake && body2.BodyType > BodyType.Static;
             bool flag7 = !flag5 && !flag6;
             if (!flag7)
             {
                 int  proxyId  = fixtureA.Proxies[childIndexA].ProxyId;
                 int  proxyId2 = fixtureB.Proxies[childIndexB].ProxyId;
                 bool flag8    = this.BroadPhase.TestOverlap(proxyId, proxyId2);
                 bool flag9    = !flag8;
                 if (flag9)
                 {
                     Contact contact5 = contact;
                     this.Destroy(contact5);
                 }
                 else
                 {
                     contact.Update(this);
                 }
             }
         }
         IL_198 :;
     }
 }
예제 #3
0
        private void AddPair(ref FixtureProxy proxyA, ref FixtureProxy proxyB)
        {
            Fixture fixture     = proxyA.Fixture;
            Fixture fixture2    = proxyB.Fixture;
            int     childIndex  = proxyA.ChildIndex;
            int     childIndex2 = proxyB.ChildIndex;
            Body    body        = fixture.Body;
            Body    body2       = fixture2.Body;
            bool    flag        = body == body2;

            if (!flag)
            {
                for (ContactEdge contactEdge = body2.ContactList; contactEdge != null; contactEdge = contactEdge.Next)
                {
                    bool flag2 = contactEdge.Other == body;
                    if (flag2)
                    {
                        Fixture fixtureA    = contactEdge.Contact.FixtureA;
                        Fixture fixtureB    = contactEdge.Contact.FixtureB;
                        int     childIndexA = contactEdge.Contact.ChildIndexA;
                        int     childIndexB = contactEdge.Contact.ChildIndexB;
                        bool    flag3       = fixtureA == fixture && fixtureB == fixture2 && childIndexA == childIndex && childIndexB == childIndex2;
                        if (flag3)
                        {
                            return;
                        }
                        bool flag4 = fixtureA == fixture2 && fixtureB == fixture && childIndexA == childIndex2 && childIndexB == childIndex;
                        if (flag4)
                        {
                            return;
                        }
                    }
                }
                bool flag5 = !body2.ShouldCollide(body);
                if (!flag5)
                {
                    bool flag6 = !ContactManager.ShouldCollide(fixture, fixture2);
                    if (!flag6)
                    {
                        bool flag7 = this.ContactFilter != null && !this.ContactFilter(fixture, fixture2);
                        if (!flag7)
                        {
                            bool flag8 = fixture.BeforeCollision != null && !fixture.BeforeCollision(fixture, fixture2);
                            if (!flag8)
                            {
                                bool flag9 = fixture2.BeforeCollision != null && !fixture2.BeforeCollision(fixture2, fixture);
                                if (!flag9)
                                {
                                    Body body3  = null;
                                    Body item   = null;
                                    bool flag10 = body.SpecialSensor > BodySpecialSensor.None;
                                    if (flag10)
                                    {
                                        body3 = body;
                                        item  = body2;
                                    }
                                    else
                                    {
                                        bool flag11 = body2.SpecialSensor > BodySpecialSensor.None;
                                        if (flag11)
                                        {
                                            body3 = body2;
                                            item  = body;
                                        }
                                    }
                                    bool flag12 = body3 != null;
                                    if (flag12)
                                    {
                                        bool flag13 = Collision.TestOverlap(body.FixtureList[0].Shape, childIndex, body2.FixtureList[0].Shape, childIndex2, ref body._xf, ref body2._xf);
                                        if (!flag13)
                                        {
                                            return;
                                        }
                                        body3._specialSensorResults.Add(item);
                                        bool flag14 = body3.SpecialSensor == BodySpecialSensor.ActiveOnce;
                                        if (flag14)
                                        {
                                            body3.disabled = true;
                                            return;
                                        }
                                    }
                                    Contact contact = Contact.Create(fixture, childIndex, fixture2, childIndex2);
                                    bool    flag15  = contact == null;
                                    if (!flag15)
                                    {
                                        fixture  = contact.FixtureA;
                                        fixture2 = contact.FixtureB;
                                        body     = fixture.Body;
                                        body2    = fixture2.Body;
                                        this.ContactList.Add(contact);
                                        contact._nodeA.Contact = contact;
                                        contact._nodeA.Other   = body2;
                                        contact._nodeA.Prev    = null;
                                        contact._nodeA.Next    = body.ContactList;
                                        bool flag16 = body.ContactList != null;
                                        if (flag16)
                                        {
                                            body.ContactList.Prev = contact._nodeA;
                                        }
                                        body.ContactList       = contact._nodeA;
                                        contact._nodeB.Contact = contact;
                                        contact._nodeB.Other   = body;
                                        contact._nodeB.Prev    = null;
                                        contact._nodeB.Next    = body2.ContactList;
                                        bool flag17 = body2.ContactList != null;
                                        if (flag17)
                                        {
                                            body2.ContactList.Prev = contact._nodeB;
                                        }
                                        body2.ContactList = contact._nodeB;
                                        bool flag18 = !fixture.IsSensor && !fixture2.IsSensor;
                                        if (flag18)
                                        {
                                            body.Awake  = true;
                                            body2.Awake = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        internal void Update(ContactManager contactManager)
        {
            Body body  = this.FixtureA.Body;
            Body body2 = this.FixtureB.Body;
            bool flag  = this.FixtureA == null || this.FixtureB == null;

            if (!flag)
            {
                bool flag2 = !ContactManager.CheckCollisionConditions(this.FixtureA, this.FixtureB);
                if (flag2)
                {
                    this.Enabled = false;
                }
                else
                {
                    Manifold manifold = this.Manifold;
                    this.Enabled = true;
                    bool isTouching = this.IsTouching;
                    bool flag3      = this.FixtureA.IsSensor || this.FixtureB.IsSensor;
                    bool flag4      = flag3;
                    bool flag5;
                    if (flag4)
                    {
                        Shape shape  = this.FixtureA.Shape;
                        Shape shape2 = this.FixtureB.Shape;
                        flag5 = Collision.TestOverlap(shape, this.ChildIndexA, shape2, this.ChildIndexB, ref body._xf, ref body2._xf);
                        this.Manifold.PointCount = 0;
                    }
                    else
                    {
                        this.Evaluate(ref this.Manifold, ref body._xf, ref body2._xf);
                        flag5 = (this.Manifold.PointCount > 0);
                        for (int i = 0; i < this.Manifold.PointCount; i++)
                        {
                            ManifoldPoint manifoldPoint = this.Manifold.Points[i];
                            manifoldPoint.NormalImpulse  = 0f;
                            manifoldPoint.TangentImpulse = 0f;
                            ContactID id = manifoldPoint.Id;
                            for (int j = 0; j < manifold.PointCount; j++)
                            {
                                ManifoldPoint manifoldPoint2 = manifold.Points[j];
                                bool          flag6          = manifoldPoint2.Id.Key == id.Key;
                                if (flag6)
                                {
                                    manifoldPoint.NormalImpulse  = manifoldPoint2.NormalImpulse;
                                    manifoldPoint.TangentImpulse = manifoldPoint2.TangentImpulse;
                                    break;
                                }
                            }
                            this.Manifold.Points[i] = manifoldPoint;
                        }
                        bool flag7 = flag5 != isTouching;
                        if (flag7)
                        {
                            body.Awake  = true;
                            body2.Awake = true;
                        }
                    }
                    this.IsTouching = flag5;
                    bool flag8 = !isTouching;
                    if (flag8)
                    {
                        bool flag9 = flag5;
                        if (flag9)
                        {
                            bool flag10 = true;
                            bool flag11 = true;
                            bool flag12 = this.FixtureA.OnCollision != null;
                            if (flag12)
                            {
                                Delegate[] invocationList = this.FixtureA.OnCollision.GetInvocationList();
                                for (int k = 0; k < invocationList.Length; k++)
                                {
                                    OnCollisionEventHandler onCollisionEventHandler = (OnCollisionEventHandler)invocationList[k];
                                    flag10 = (onCollisionEventHandler(this.FixtureA, this.FixtureB, this) & flag10);
                                }
                            }
                            bool flag13 = this.FixtureB.OnCollision != null;
                            if (flag13)
                            {
                                Delegate[] invocationList2 = this.FixtureB.OnCollision.GetInvocationList();
                                for (int l = 0; l < invocationList2.Length; l++)
                                {
                                    OnCollisionEventHandler onCollisionEventHandler2 = (OnCollisionEventHandler)invocationList2[l];
                                    flag11 = (onCollisionEventHandler2(this.FixtureB, this.FixtureA, this) & flag11);
                                }
                            }
                            this.Enabled = (flag10 & flag11);
                            bool flag14 = (flag10 & flag11) && contactManager.BeginContact != null;
                            if (flag14)
                            {
                                this.Enabled = contactManager.BeginContact(this);
                            }
                            bool flag15 = !this.Enabled;
                            if (flag15)
                            {
                                this.IsTouching = false;
                            }
                        }
                    }
                    else
                    {
                        bool flag16 = !flag5;
                        if (flag16)
                        {
                            bool flag17 = this.FixtureA != null && this.FixtureA.OnSeparation != null;
                            if (flag17)
                            {
                                this.FixtureA.OnSeparation(this.FixtureA, this.FixtureB);
                            }
                            bool flag18 = this.FixtureB != null && this.FixtureB.OnSeparation != null;
                            if (flag18)
                            {
                                this.FixtureB.OnSeparation(this.FixtureB, this.FixtureA);
                            }
                            bool flag19 = contactManager.EndContact != null;
                            if (flag19)
                            {
                                contactManager.EndContact(this);
                            }
                        }
                        else
                        {
                            bool flag20 = contactManager.StayContact != null;
                            if (flag20)
                            {
                                contactManager.StayContact(this);
                            }
                        }
                    }
                    bool flag21 = flag3;
                    if (!flag21)
                    {
                        bool flag22 = contactManager.PreSolve != null;
                        if (flag22)
                        {
                            contactManager.PreSolve(this, ref manifold);
                        }
                    }
                }
            }
        }