void OnEndContact(Box2DX.Dynamics.Contact contact)
 {
     if ((contact.GetShape1().UserData is Actor) && (contact.GetShape2().UserData is Actor))
     {
         (contact.GetShape1().UserData as Actor).OnEndContact((contact.GetShape2().UserData as Actor), contact.GetShape1(), contact.GetShape2());
         (contact.GetShape2().UserData as Actor).OnEndContact((contact.GetShape1().UserData as Actor), contact.GetShape2(), contact.GetShape1());
     }
     else
     {
         throw new Exceptions.PhysicsBodyDataIsBroken("This contact listener requires for user data to be owner of the component to properly operate");
     }
 }
Beispiel #2
0
 public void Report(ContactConstraint[] constraints)
 {
     if (this._listener != null)
     {
         for (int i = 0; i < this._contactCount; i++)
         {
             Contact           contact           = this._contacts[i];
             ContactConstraint contactConstraint = constraints[i];
             ContactResult     contactResult     = new ContactResult();
             contactResult.Shape1 = contact.GetShape1();
             contactResult.Shape2 = contact.GetShape2();
             Body       body          = contactResult.Shape1.GetBody();
             int        manifoldCount = contact.GetManifoldCount();
             Manifold[] manifolds     = contact.GetManifolds();
             for (int j = 0; j < manifoldCount; j++)
             {
                 Manifold manifold = manifolds[j];
                 contactResult.Normal = manifold.Normal;
                 for (int k = 0; k < manifold.PointCount; k++)
                 {
                     ManifoldPoint          manifoldPoint          = manifold.Points[k];
                     ContactConstraintPoint contactConstraintPoint = contactConstraint.Points[k];
                     contactResult.Position       = body.GetWorldPoint(manifoldPoint.LocalPoint1);
                     contactResult.NormalImpulse  = contactConstraintPoint.NormalImpulse;
                     contactResult.TangentImpulse = contactConstraintPoint.TangentImpulse;
                     contactResult.ID             = manifoldPoint.ID;
                     this._listener.Result(contactResult);
                 }
             }
         }
     }
 }
Beispiel #3
0
        public static void Destroy(Contact contact)
        {
            Box2DXDebug.Assert(Contact.s_initialized);
            if (contact.GetManifoldCount() > 0)
            {
                contact.GetShape1().GetBody().WakeUp();
                contact.GetShape2().GetBody().WakeUp();
            }
            ShapeType type  = contact.GetShape1().GetType();
            ShapeType type2 = contact.GetShape2().GetType();

            Box2DXDebug.Assert(ShapeType.UnknownShape < type && type < ShapeType.ShapeTypeCount);
            Box2DXDebug.Assert(ShapeType.UnknownShape < type2 && type2 < ShapeType.ShapeTypeCount);
            ContactDestroyFcn destroyFcn = Contact.s_registers[(int)type][(int)type2].DestroyFcn;

            destroyFcn(contact);
        }
Beispiel #4
0
 public void Collide()
 {
     for (Contact contact = this._world._contactList; contact != null; contact = contact.GetNext())
     {
         Body body  = contact.GetShape1().GetBody();
         Body body2 = contact.GetShape2().GetBody();
         if (!body.IsSleeping() || !body2.IsSleeping())
         {
             contact.Update(this._world._contactListener);
         }
     }
 }
        // This is the top level collision call for the time step. Here
        // all the narrow phase collision is processed for the world
        // contact list.
        public void Collide()
        {
            // Update awake contacts.
            for (Contact c = _world._contactList; c != null; c = c.GetNext())
            {
                Body body1 = c.GetShape1().GetBody();
                Body body2 = c.GetShape2().GetBody();
                if (body1.IsSleeping() && body2.IsSleeping())
                {
                    continue;
                }

                c.Update(_world._contactListener);
            }
        }
        // This is a callback from the broadphase when two AABB proxies begin
        // to overlap. We create a Contact to manage the narrow phase.
        public override object PairAdded(object proxyUserData1, object proxyUserData2)
        {
            Shape shape1 = proxyUserData1 as Shape;
            Shape shape2 = proxyUserData2 as Shape;

            Body body1 = shape1.GetBody();
            Body body2 = shape2.GetBody();

            if (body1.IsStatic() && body2.IsStatic())
            {
                return(_nullContact);
            }

            if (shape1.GetBody() == shape2.GetBody())
            {
                return(_nullContact);
            }

            if (body2.IsConnected(body1))
            {
                return(_nullContact);
            }

            if (_world._contactFilter != null && _world._contactFilter.ShouldCollide(shape1, shape2) == false)
            {
                return(_nullContact);
            }

            // Call the factory.
            Contact c = Contact.Create(shape1, shape2);

            if (c == null)
            {
                return(_nullContact);
            }

            // Contact creation may swap shapes.
            shape1 = c.GetShape1();
            shape2 = c.GetShape2();
            body1  = shape1.GetBody();
            body2  = shape2.GetBody();

            // Insert into the world.
            c._prev = null;
            c._next = _world._contactList;
            if (_world._contactList != null)
            {
                _world._contactList._prev = c;
            }
            _world._contactList = c;

            // Connect to island graph.

            // Connect to body 1
            c._node1.Contact = c;
            c._node1.Other   = body2;

            c._node1.Prev = null;
            c._node1.Next = body1._contactList;
            if (body1._contactList != null)
            {
                body1._contactList.Prev = c._node1;
            }
            body1._contactList = c._node1;

            // Connect to body 2
            c._node2.Contact = c;
            c._node2.Other   = body1;

            c._node2.Prev = null;
            c._node2.Next = body2._contactList;
            if (body2._contactList != null)
            {
                body2._contactList.Prev = c._node2;
            }
            body2._contactList = c._node2;

            ++_world._contactCount;
            return(c);
        }
        public void Destroy(Contact c)
        {
            Shape shape1 = c.GetShape1();
            Shape shape2 = c.GetShape2();
            Body  body1  = shape1.GetBody();
            Body  body2  = shape2.GetBody();

            ContactPoint cp = new ContactPoint();

            cp.Shape1      = shape1;
            cp.Shape2      = shape2;
            cp.Friction    = Settings.MixFriction(shape1.Friction, shape2.Friction);
            cp.Restitution = Settings.MixRestitution(shape1.Restitution, shape2.Restitution);

            // Inform the user that this contact is ending.
            int manifoldCount = c.GetManifoldCount();

            if (manifoldCount > 0 && _world._contactListener != null)
            {
                Manifold[] manifolds = c.GetManifolds();

                for (int i = 0; i < manifoldCount; ++i)
                {
                    Manifold manifold = manifolds[i];
                    cp.Normal = manifold.Normal;

                    for (int j = 0; j < manifold.PointCount; ++j)
                    {
                        ManifoldPoint mp = manifold.Points[j];
                        cp.Position = body1.GetWorldPoint(mp.LocalPoint1);
                        Vec2 v1 = body1.GetLinearVelocityFromLocalPoint(mp.LocalPoint1);
                        Vec2 v2 = body2.GetLinearVelocityFromLocalPoint(mp.LocalPoint2);
                        cp.Velocity   = v2 - v1;
                        cp.Separation = mp.Separation;
                        cp.ID         = mp.ID;
                        _world._contactListener.Remove(cp);
                    }
                }
            }

            // Remove from the world.
            if (c._prev != null)
            {
                c._prev._next = c._next;
            }

            if (c._next != null)
            {
                c._next._prev = c._prev;
            }

            if (c == _world._contactList)
            {
                _world._contactList = c._next;
            }

            // Remove from body 1
            if (c._node1.Prev != null)
            {
                c._node1.Prev.Next = c._node1.Next;
            }

            if (c._node1.Next != null)
            {
                c._node1.Next.Prev = c._node1.Prev;
            }

            if (c._node1 == body1._contactList)
            {
                body1._contactList = c._node1.Next;
            }

            // Remove from body 2
            if (c._node2.Prev != null)
            {
                c._node2.Prev.Next = c._node2.Next;
            }

            if (c._node2.Next != null)
            {
                c._node2.Next.Prev = c._node2.Prev;
            }

            if (c._node2 == body2._contactList)
            {
                body2._contactList = c._node2.Next;
            }

            // Call the factory.
            Contact.Destroy(c);
            --_world._contactCount;
        }
        public void Destroy(Contact c)
        {
            Shape shape1 = c.GetShape1();
            Shape shape2 = c.GetShape2();
            Body body1 = shape1.GetBody();
            Body body2 = shape2.GetBody();

            ContactPoint cp = new ContactPoint();
            cp.Shape1 = shape1;
            cp.Shape2 = shape2;
            cp.Friction = Settings.MixFriction(shape1.Friction, shape2.Friction);
            cp.Restitution = Settings.MixRestitution(shape1.Restitution, shape2.Restitution);

            // Inform the user that this contact is ending.
            int manifoldCount = c.GetManifoldCount();
            if (manifoldCount > 0 && _world._contactListener!=null)
            {
                Manifold[] manifolds = c.GetManifolds();

                for (int i = 0; i < manifoldCount; ++i)
                {
                    Manifold manifold = manifolds[i];
                    cp.Normal = manifold.Normal;

                    for (int j = 0; j < manifold.PointCount; ++j)
                    {
                        ManifoldPoint mp = manifold.Points[j];
                        cp.Position = body1.GetWorldPoint(mp.LocalPoint1);
                        Vec2 v1 = body1.GetLinearVelocityFromLocalPoint(mp.LocalPoint1);
                        Vec2 v2 = body2.GetLinearVelocityFromLocalPoint(mp.LocalPoint2);
                        cp.Velocity = v2 - v1;
                        cp.Separation = mp.Separation;
                        cp.ID = mp.ID;
                        _world._contactListener.Remove(cp);
                    }
                }
            }

            // Remove from the world.
            if (c._prev != null)
            {
                c._prev._next = c._next;
            }

            if (c._next != null)
            {
                c._next._prev = c._prev;
            }

            if (c == _world._contactList)
            {
                _world._contactList = c._next;
            }

            // Remove from body 1
            if (c._node1.Prev != null)
            {
                c._node1.Prev.Next = c._node1.Next;
            }

            if (c._node1.Next != null)
            {
                c._node1.Next.Prev = c._node1.Prev;
            }

            if (c._node1 == body1._contactList)
            {
                body1._contactList = c._node1.Next;
            }

            // Remove from body 2
            if (c._node2.Prev != null)
            {
                c._node2.Prev.Next = c._node2.Next;
            }

            if (c._node2.Next != null)
            {
                c._node2.Next.Prev = c._node2.Prev;
            }

            if (c._node2 == body2._contactList)
            {
                body2._contactList = c._node2.Next;
            }

            // Call the factory.
            Contact.Destroy(c);
            --_world._contactCount;
        }
		public static void Destroy(Contact contact)
		{

            //Box2DXDebug.Assert(s_initialized == true); // COMMENT IS HACK HACK HACK

			if (contact.GetManifoldCount() > 0)
			{
				contact.GetShape1().GetBody().WakeUp();
				contact.GetShape2().GetBody().WakeUp();
			}

			ShapeType type1 = contact.GetShape1().GetType();
			ShapeType type2 = contact.GetShape2().GetType();

			Box2DXDebug.Assert(ShapeType.UnknownShape < type1 && type1 < ShapeType.ShapeTypeCount);
			Box2DXDebug.Assert(ShapeType.UnknownShape < type2 && type2 < ShapeType.ShapeTypeCount);

            try
            {
                ContactDestroyFcn destroyFcn = s_registers[(int)type1][(int)type2].DestroyFcn;
                destroyFcn(contact);
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine("NullReferenceException caught in Box2DX.Dynamics.Contact#Destroy");
            }
		}
Beispiel #10
0
        public override object PairAdded(object proxyUserData1, object proxyUserData2)
        {
            Shape  shape  = proxyUserData1 as Shape;
            Shape  shape2 = proxyUserData2 as Shape;
            Body   body   = shape.GetBody();
            Body   body2  = shape2.GetBody();
            object result;

            if (body.IsStatic() && body2.IsStatic())
            {
                result = this._nullContact;
            }
            else
            {
                if (shape.GetBody() == shape2.GetBody())
                {
                    result = this._nullContact;
                }
                else
                {
                    if (body2.IsConnected(body))
                    {
                        result = this._nullContact;
                    }
                    else
                    {
                        if (this._world._contactFilter != null && !this._world._contactFilter.ShouldCollide(shape, shape2))
                        {
                            result = this._nullContact;
                        }
                        else
                        {
                            Contact contact = Contact.Create(shape, shape2);
                            if (contact == null)
                            {
                                result = this._nullContact;
                            }
                            else
                            {
                                shape         = contact.GetShape1();
                                shape2        = contact.GetShape2();
                                body          = shape.GetBody();
                                body2         = shape2.GetBody();
                                contact._prev = null;
                                contact._next = this._world._contactList;
                                if (this._world._contactList != null)
                                {
                                    this._world._contactList._prev = contact;
                                }
                                this._world._contactList = contact;
                                contact._node1.Contact   = contact;
                                contact._node1.Other     = body2;
                                contact._node1.Prev      = null;
                                contact._node1.Next      = body._contactList;
                                if (body._contactList != null)
                                {
                                    body._contactList.Prev = contact._node1;
                                }
                                body._contactList      = contact._node1;
                                contact._node2.Contact = contact;
                                contact._node2.Other   = body;
                                contact._node2.Prev    = null;
                                contact._node2.Next    = body2._contactList;
                                if (body2._contactList != null)
                                {
                                    body2._contactList.Prev = contact._node2;
                                }
                                body2._contactList = contact._node2;
                                this._world._contactCount++;
                                result = contact;
                            }
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #11
0
        public void Destroy(Contact c)
        {
            Shape        shape        = c.GetShape1();
            Shape        shape2       = c.GetShape2();
            Body         body         = shape.GetBody();
            Body         body2        = shape2.GetBody();
            ContactPoint contactPoint = new ContactPoint();

            contactPoint.Shape1      = shape;
            contactPoint.Shape2      = shape2;
            contactPoint.Friction    = Settings.MixFriction(shape.Friction, shape2.Friction);
            contactPoint.Restitution = Settings.MixRestitution(shape.Restitution, shape2.Restitution);
            int manifoldCount = c.GetManifoldCount();

            if (manifoldCount > 0 && this._world._contactListener != null)
            {
                Manifold[] manifolds = c.GetManifolds();
                for (int i = 0; i < manifoldCount; i++)
                {
                    Manifold manifold = manifolds[i];
                    contactPoint.Normal = manifold.Normal;
                    for (int j = 0; j < manifold.PointCount; j++)
                    {
                        ManifoldPoint manifoldPoint = manifold.Points[j];
                        contactPoint.Position = body.GetWorldPoint(manifoldPoint.LocalPoint1);
                        Vec2 linearVelocityFromLocalPoint  = body.GetLinearVelocityFromLocalPoint(manifoldPoint.LocalPoint1);
                        Vec2 linearVelocityFromLocalPoint2 = body2.GetLinearVelocityFromLocalPoint(manifoldPoint.LocalPoint2);
                        contactPoint.Velocity   = linearVelocityFromLocalPoint2 - linearVelocityFromLocalPoint;
                        contactPoint.Separation = manifoldPoint.Separation;
                        contactPoint.ID         = manifoldPoint.ID;
                        this._world._contactListener.Remove(contactPoint);
                    }
                }
            }
            if (c._prev != null)
            {
                c._prev._next = c._next;
            }
            if (c._next != null)
            {
                c._next._prev = c._prev;
            }
            if (c == this._world._contactList)
            {
                this._world._contactList = c._next;
            }
            if (c._node1.Prev != null)
            {
                c._node1.Prev.Next = c._node1.Next;
            }
            if (c._node1.Next != null)
            {
                c._node1.Next.Prev = c._node1.Prev;
            }
            if (c._node1 == body._contactList)
            {
                body._contactList = c._node1.Next;
            }
            if (c._node2.Prev != null)
            {
                c._node2.Prev.Next = c._node2.Next;
            }
            if (c._node2.Next != null)
            {
                c._node2.Next.Prev = c._node2.Prev;
            }
            if (c._node2 == body2._contactList)
            {
                body2._contactList = c._node2.Next;
            }
            Contact.Destroy(c);
            this._world._contactCount--;
        }
Beispiel #12
0
		public static void Destroy(Contact contact)
		{
			Box2DXDebug.Assert(s_initialized == true);

			if (contact.GetManifoldCount() > 0)
			{
				contact.GetShape1().GetBody().WakeUp();
				contact.GetShape2().GetBody().WakeUp();
			}

			ShapeType type1 = contact.GetShape1().GetType();
			ShapeType type2 = contact.GetShape2().GetType();

			Box2DXDebug.Assert(ShapeType.UnknownShape < type1 && type1 < ShapeType.ShapeTypeCount);
			Box2DXDebug.Assert(ShapeType.UnknownShape < type2 && type2 < ShapeType.ShapeTypeCount);

			ContactDestroyFcn destroyFcn = s_registers[(int)type1][(int)type2].DestroyFcn;
			destroyFcn(contact);
		}
Beispiel #13
0
 public void Destroy(Contact c)
 {
     Shape shape = c.GetShape1();
     Shape shape2 = c.GetShape2();
     Body body = shape.GetBody();
     Body body2 = shape2.GetBody();
     ContactPoint contactPoint = new ContactPoint();
     contactPoint.Shape1 = shape;
     contactPoint.Shape2 = shape2;
     contactPoint.Friction = Settings.MixFriction(shape.Friction, shape2.Friction);
     contactPoint.Restitution = Settings.MixRestitution(shape.Restitution, shape2.Restitution);
     int manifoldCount = c.GetManifoldCount();
     if (manifoldCount > 0 && this._world._contactListener != null)
     {
         Manifold[] manifolds = c.GetManifolds();
         for (int i = 0; i < manifoldCount; i++)
         {
             Manifold manifold = manifolds[i];
             contactPoint.Normal = manifold.Normal;
             for (int j = 0; j < manifold.PointCount; j++)
             {
                 ManifoldPoint manifoldPoint = manifold.Points[j];
                 contactPoint.Position = body.GetWorldPoint(manifoldPoint.LocalPoint1);
                 Vec2 linearVelocityFromLocalPoint = body.GetLinearVelocityFromLocalPoint(manifoldPoint.LocalPoint1);
                 Vec2 linearVelocityFromLocalPoint2 = body2.GetLinearVelocityFromLocalPoint(manifoldPoint.LocalPoint2);
                 contactPoint.Velocity = linearVelocityFromLocalPoint2 - linearVelocityFromLocalPoint;
                 contactPoint.Separation = manifoldPoint.Separation;
                 contactPoint.ID = manifoldPoint.ID;
                 this._world._contactListener.Remove(contactPoint);
             }
         }
     }
     if (c._prev != null)
     {
         c._prev._next = c._next;
     }
     if (c._next != null)
     {
         c._next._prev = c._prev;
     }
     if (c == this._world._contactList)
     {
         this._world._contactList = c._next;
     }
     if (c._node1.Prev != null)
     {
         c._node1.Prev.Next = c._node1.Next;
     }
     if (c._node1.Next != null)
     {
         c._node1.Next.Prev = c._node1.Prev;
     }
     if (c._node1 == body._contactList)
     {
         body._contactList = c._node1.Next;
     }
     if (c._node2.Prev != null)
     {
         c._node2.Prev.Next = c._node2.Next;
     }
     if (c._node2.Next != null)
     {
         c._node2.Next.Prev = c._node2.Prev;
     }
     if (c._node2 == body2._contactList)
     {
         body2._contactList = c._node2.Next;
     }
     Contact.Destroy(c);
     this._world._contactCount--;
 }