Box2D plus elastic collisions

Please don't post Bullet support questions here, use the above forums instead.
Post Reply
mcneja
Posts: 2
Joined: Tue Aug 21, 2007 1:06 pm
Contact:

Box2D plus elastic collisions

Post by mcneja » Tue Aug 21, 2007 1:29 pm

Thanks for the Box2D sample, Erin! I've been going over it the past few weeks and attempting to modify my own physics engine based on what I'm learning.

One thing I ran into this past weekend is that Box2D assumes collisions are perfectly inelastic. The relevant line is in Arbiter::ApplyImpulse, where it says:

dPn = c->massNormal * -vn;

(For those not looking at the code, dPn is a delta to the normal impulse; it's a corrective impulse which is summed up over iterations to produce a total impulse for the contact for that time step. c is the contact data structure, and massNormal is the denominator of the equation for computing impulses based on relative velocity in the normal direction. vn is the relative velocity in the normal direction at the contact, with postiive meaning objects are moving away from each other.)

With elasticity you'd have something like this:

dPn = c->massNormal * -(1.0f + elasticity) * vn;

Ordinarily you'd wrap this in an if-statement to avoid generating an impulse if the two objects are moving away from each other at the contact point, since the impulse would be negative and would thus pull them back together.

If I understand Box2D right, it doesn't do this because a negative impulse is sometimes needed to correct an overly-large positive impulse from a previous step in the iterative solution. As long as the total impulse generated by the solution is positive, you are fine (in the inelastic case), and it does clamp the accumulated impulse to always be positive.

I want to support elasticity in my collisions so I'm trying to figure out how to reconcile the collision's bounce impulse with the iteratively-solved non-penetration-constraint impulse. I will be working some examples on paper where I know what I want to have happen to see if I can come up with the right algorithm, but any pointers would be appreciated, especially if I've missed something obvious.

Thanks,
James

crashlander
Posts: 41
Joined: Sat Apr 08, 2006 11:20 am

Post by crashlander » Tue Aug 21, 2007 4:40 pm

There is a good example of it in this open source engine:

http://wiki.slembcke.net/main/published/Chipmunk

Erin Catto
Posts: 324
Joined: Fri Jul 01, 2005 5:29 am
Location: Irvine
Contact:

Post by Erin Catto » Tue Aug 21, 2007 5:21 pm

What you want is possible and I'm guessing that Chipmunk gets it right.

To include restitution, you should adjust the velocity bias in the setup stage. There you should check the normal velocity against some tolerance.

If you try to apply restitution directly in the iterations, you end up chasing your tail.

ewjordan
Posts: 26
Joined: Sat Jun 30, 2007 4:34 am

Post by ewjordan » Tue Aug 21, 2007 6:11 pm

If you do Java, Phys2d (another Box2d port/edit, I'm not sure how close it sticks to the original) implements bouncing, at least according to the Javadoc, but I'm not sure exactly how, where, or whether it's the right way or not.

http://www.cokeandcode.com/phys2d/

mcneja
Posts: 2
Joined: Tue Aug 21, 2007 1:06 pm
Contact:

Post by mcneja » Wed Aug 22, 2007 3:18 am

Thanks for the replies. I'm looking through Chipmunk now.

Post Reply