Box2DX find collision points

Discuss issues specific to the C#/XNA ports of Box2D
Posts: 10
Joined: Wed Aug 11, 2010 5:18 am

Box2DX find collision points

Postby minas1 » Sun Mar 11, 2012 12:36 pm

Hi. I am making a game using Box2DX.

Basically I have a player and some gameObjects. All gameObjects (and the player) have a Body.

Collision works fine. Now I want to get the points of contact. I have found no documentation about how to do it.

So I used Box2DX.Dynamics.PolygonContact.

Here's my code:

Code: Select all

// find GameObjects that collide with the Player
            foreach (GameObject o in gameObjects)
                Box2DX.Dynamics.PolygonContact contact = null;

                Box2DX.Collision.Shape s1 = player.obj.body.GetShapeList();
                Box2DX.Collision.Shape s2 = o.body.GetShapeList();

                while( s1 != null )
                    s2 = o.body.GetShapeList();
                    while (s2 != null)
                        contact = new Box2DX.Dynamics.PolygonContact(s1, s2);

                        if (contact.GetManifoldCount() > 0)
                            Console.WriteLine("Contact {0}", contact.GetManifoldCount());

                        s2 = s2.GetNext();
                    s1 = s1.GetNext();

Nothing is printed though (because contact.GetManifoldCount() is always zero). What is wrong here?

irresistible force
Posts: 1988
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo

Re: Box2DX find collision points

Postby irresistible force » Mon Mar 12, 2012 5:10 am

The recommended way to detect and handle contacts is with a 'contact listener' <- search the manual for that phrase. It gives you a notification when two fixtures begin and end contact, and allows you to manipulate the characteristics of the contact while the fixtures are touching. Check out this page, it's C++ but might help you with finding the contact points:

Return to “C Sharp”

Who is online

Users browsing this forum: No registered users and 1 guest