Definition of Impulse: Impulse is force multiplied by timestep. This will give the strength of the collision. We might need this information while making a game. For example, we might need to destroy a particular body when it collides with another body when impulse is greater than a certain value.

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 "Post-Solve Event" event available in Contact Listener. The post solve event is where you can gather collision impulse results.
var w_manifold = new Box2D.Collision.b2WorldManifold();
var points = new b2Vec2(); // Creating a vector to store points
var num = 0;
var bb1="",bb2=""; // bb1 & bb2 store userdata for body1 and body2 colliding
var listener = new Box2D.Dynamics.b2ContactListener; // Setting up listener
listener.PostSolve = function(contact, impulse)	{
	 num = contact.GetManifold().m_pointCount; // Number of contact points between two bodies
	 points = w_manifold.m_points; // Stores all contact points
	 for(j=0; j < num; j++)	{ // Iterating through all contact points
		 if(impulse.normalImpulses[j]>10)	{ // Will show only if Normal Impulse is greater than 10
			 $('#dd').prepend(bb1+" with "+bb2+' x: '+points[j].x+' y: '+points[j].y+'

'); $('#dd').prepend(bb1+' with '+bb2+' : Normal Impulse: '+impulse.normalImpulses[j]+'
'+'Tangent Impulse: '+impulse.tangentImpulses[j]+'
'); } } }
3) Add the following line in update()

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

By administrator at 12:36:53 AM 2 Comment(s)



Does anyone know how to figure out the collision normal?

By d13 on 30 Nov, 2012 at 02:17:35 AM

A vector is a product of magnitude and direction so dividing a vector by its magnitude will give its direction. In this case you know the normal impulse vector so divide it by its magnitude to get the collision normal.

By administrator on 3 Dec, 2012 at 03:58:41 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