This will show how to retrieve contact points when the two bodies collide. It must be noted that when two flat bodies collide at zero angle, there will be two contact points.

1) We are going to make use of "userData" here.
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.userData = 'Box';
Add userData to each body you create. This will act as a POINTER to the body.

2) Set up Contact Listener. Here we will make use of "BeginContact" event available in Contact Listener. This is called when two fixtures begin to overlap. This is called for sensors and non-sensors. This event can only occur inside the time step.
var w_manifold = new Box2D.Collision.b2WorldManifold();
var points = new b2Vec2; // Creating a vector to store points
var num;
var b1="",b2=""; // b1 & b2 store userdata for body1 and body2 colliding
var listener = new Box2D.Dynamics.b2ContactListener; // Setting up listener
listener.BeginContact = function(contact) {
	 num = contact.GetManifold().m_pointCount; // Gets the number of point of contact between two bodies.
	 points = w_manifold.m_points; // stores all the points
	 for(i=0; i < num; i++)	{
		 b1=contact.GetFixtureA().GetBody().GetUserData(); //Gets userdata for body 1
		 b2=contact.GetFixtureB().GetBody().GetUserData(); //Gets userdata for body 2
		 $('#dd').prepend(b1+" with "+b2+' x: '+points[i].x+' y: '+points[i].y+'
'); } }
3) Add the following line in update()

For better understanding look at the following example and then its source code:

By administrator at 12:19:16 AM 1 Comment(s)



We just wanted to let you know that domain CODINGOWLS.COM is being released back to the market.

Since you own CODINGOWL.COM, we believe that you might have interest in securing CODINGOWLS.COM as well.

If you are interested, please go to: to get more information and confirm your interest.

All the additional information is available on our web site, but feel free to reply back to this email and we will be more than happy to help you.

Kind regards,

Jim Horvath

By Jim Horvath on 12 Jun, 2016 at 11:25:07 AM

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha