Box2D Demo & Tutorial

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

Box2D Demo & Tutorial

Post by Erin Catto »

I wrote a tutorial for an impulse based constraint solver. The zip file comes with slides and source code for a 2D engine.

http://www.gphysics.com/files/GDC2006_ErinCatto.zip
dog
Posts: 40
Joined: Fri Jul 22, 2005 8:00 pm
Location: Santa Monica

Post by dog »

I don't think you cover this in your slides, but how do you handle non-zero restitutions in such a scheme?
Erin Catto
Posts: 316
Joined: Fri Jul 01, 2005 5:29 am
Location: Irvine
Contact:

Post by Erin Catto »

Restitution can be handled (approximately) by adding a "bounce velocity" into the normal impulse computation, just like the bias velocity used for resolving penetration.
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Post by Dirk Gregorius »

You could also use the Guendelman integration:

1) Move to candidates and handle collisions
2) Integrate velocity
3) Handle contact
4) Integrate position
jiangwei
Posts: 23
Joined: Wed Nov 30, 2005 11:07 am
Location: China

Post by jiangwei »

Why do not write a tutorial for an LCP based constraint solver,because i saw that your introduced method in your this paper:"Iterative Dynamics with Temporal Coherence " is LCP based constraint solver
Erin Catto
Posts: 316
Joined: Fri Jul 01, 2005 5:29 am
Location: Irvine
Contact:

Post by Erin Catto »

Why do not write a tutorial for an LCP based constraint solver,because i saw that your introduced method in your this paper:"Iterative Dynamics with Temporal Coherence " is LCP based constraint solver
It turns out that the sequential impulse algorithm is very similar to the iterative LCP algorithm, but is easier to understand and implement. Think of it as Revision 2.
DevO
Posts: 95
Joined: Fri Mar 31, 2006 7:13 pm

Post by DevO »

Thanks for this impulse based method!
I like it very well, it is easy to understand and implement.

I finally implimented 3D simulation and it looks prety stabele and fast, even without optimisation.

The only problem seems to make bias:
if it is smal the the stacking will be stable but the collision looks not well.
but if is is biiger to make collision looks well the stacking scenario gains too many extra energy and becomes jumpily.

Da you have any ideas how to avoid or compensate this extra energy from bias?
Erin Catto
Posts: 316
Joined: Fri Jul 01, 2005 5:29 am
Location: Irvine
Contact:

Post by Erin Catto »

I haven't tried this yet, but you should be able to propagate two velocity states. One velocity state includes the bias and is used to update position. The second velocity state does not include the biase and is carried over to the next step.

Even with this method you shouldn't use a large bias factor because the overlap correction can overshoot and you will lose coherence.
User avatar
Spider100
Posts: 1
Joined: Sat Jun 17, 2006 6:13 pm

Post by Spider100 »

Yo

I implement your collision response in my physics engine and it is great
http://www.dathox.unit1.pl/files/phylum2d.rar

I tried to create player in my demo and I have one problem.
Sometimes, when player hits the ground, he doesnt slide :/. friction = 0.5.
http://www.dathox.unit1.pl/files/Slide.rar
any idea ?
kevglass
Posts: 7
Joined: Mon Aug 07, 2006 6:48 pm
Location: Davis, CA, USA
Contact:

Post by kevglass »

I've been trying to implement good looking restitution in my Box2D dervied Java code for a while now. I've assigned a "hardness" factor (0 -> 1.0) and use this to dervice a value to apply to the normal impulse calculation as suggested above.

However, the results I'm getting are pretty unstable - I think most due to the acceptence of penetration and result incorrect relative positions of the bodies. The current implementation is reasonable in that it results in some bounce back based on the hardness value assigned - the value applied has to be tuned for different situations.

I wonder if theres a more formal solution in this case or whether there are some resources I could read up on to better understand how restitution might be resolve in impulse based case.

Thanks for any help,

Kev
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Post by Erwin Coumans »

Adding restitution can be done by calculating its value at the start (NOT during the solver iterations).
Then, using this restitution as target velocity instead of 0.
SimdScalar velocityError = cpd->m_restitution - rel_vel;

Bullet implements a 3D version of Sequential Impulse, and includes restitution support.

See calculation of restitution:
http://www.continuousphysics.com/Bullet ... tml#l00116
and its application:
http://www.continuousphysics.com/Bullet ... tml#l00051

I'm not familiar with the Box2D code, but if this info is not enough, ask again, and I can check it out (or Erin Catto?).

Thanks,
Erwin

kevglass wrote:I've been trying to implement good looking restitution in my Box2D dervied Java code for a while now. I've assigned a "hardness" factor (0 -> 1.0) and use this to dervice a value to apply to the normal impulse calculation as suggested above.

However, the results I'm getting are pretty unstable - I think most due to the acceptence of penetration and result incorrect relative positions of the bodies. The current implementation is reasonable in that it results in some bounce back based on the hardness value assigned - the value applied has to be tuned for different situations.

I wonder if theres a more formal solution in this case or whether there are some resources I could read up on to better understand how restitution might be resolve in impulse based case.

Thanks for any help,

Kev
kevglass
Posts: 7
Joined: Mon Aug 07, 2006 6:48 pm
Location: Davis, CA, USA
Contact:

Post by kevglass »

Thanks for the prompt reply Erwin. I've applied what you've shown in your source and it looks good for direct collisions. However, when I get an collisions where the velocities are at an angle to the normal of collision things seem to go astray in that I *appear* to get extra velocity into the system somehow.

Are there any obvious mistakes I might have made that would have these symptoms? I realise this is a long shot :)

Thanks for your help up to now anyway, it's really helped me to understand the original code.

Kev
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Post by Erwin Coumans »

You might introduce extra energy/verlocity due to penetration depth recovery. This can be fixed by keeping the impulses split. So you can download a new Box2D.zip from the quote below.

As Erin mentioned on the ODE list:
Erin on ODE mailinglist 6-26-2006 wrote: I didn't have time to check the patch, but I updated my 2D physics engine
Box2D. This engine uses sequential impulses, which are mathematically
equivalent to PGS. Here's how I did it:

- Each body has a bias velocity that is only used for correcting overlap.
- A separate bias impulse is accumulated for each contact.
- The bias velocity and impulse are initialized to zero each time step (no
warm starting).

The bias impulse and velocity are completely independent of the regular
impulses and velocity, so they could be solved in parallel. However, it was
convenient to solve them together and that should be more cache friendly.

You can get the code here: http://www.gphysics.com/files/Box2D.zip

You can switch between the two methods by using the macro
BIAS_PRESERVES_MOMENTUM in Arbiter.cpp. With the split velocity I was able
to use a large bias factor (0.8). This means that overlap is reduced 80% per
step. Such a large bias factor leads to serious bouciness with the merged
velocity.

I also tried using a split velocity for joints. The results were not nearly
so nice, especially for my suspension bridge test. I think that particular
arrangement is more stable with some compliance in the joints.

Erin
Make sure to clip the restitution to 0, don't make it negative.
(Minor) As the friction depends on the normal impulse/force, it might be magnified by the restitution. Try to not add positive restitution to the friction component.

Erwin
kevglass
Posts: 7
Joined: Mon Aug 07, 2006 6:48 pm
Location: Davis, CA, USA
Contact:

Post by kevglass »

Thanks for the second pointer. I've integrated the seperating of bias impulse and actual impulse into my java code. That generally seems to make the system more accurate.

Unfortunately, doesn't seem to resolve my restitution issues. Maybe I'm measuring the energy wrong. Is there something wrong with summing up the length of all the velocities (speed?) given my test in this case is frictionless, no damping and with 10 circles all of equal mass. It's a pool/snooker triangle - the cue ball appraoches at a speed of 150. Am I wrong in thinking that the sum of the speeds of all bodies should never exceed 150 (given they're all equal masses) ?

Wow - don't I sound newb? :) Questioning everything I thought I knew now :)

Thanks again for the pointer, nice to keep up to date. Is the ODE mailing list public or do you have to get an invite?

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

Post by Erin Catto »

Kev,

Try it with just two spheres colliding. You should be able to compare the results with theory and figure out where the numbers go wrong.
Post Reply