Overflow with fixed point

Hi everyone,

We are using Box2D in a project here and recently, because we need to port the code to a platform with no floating point processor, we switched box2d to fixed point.

That was causing objects to go through each other, so we increased the number of iterations and it worked. Well, kind of. Objects don't go through each other but they don't bounce either.

As I implemented some physics engines in the past, I tracked the calculations of restitution and found an overflow problem in

b2ContactSolver.cpp: 119:
float32 lambda = -ccp->normalMass * (vn - ccp->velocityBias);

in that line, the multiplication is involving two huge negative numbers and the overflow is making the result look like a negative number (instead of a huge positive one).

So in the next line of code,

float32 newImpulse = b2Max(ccp->normalImpulse + lambda, 0.0f);

we select 0.0f as the new impulse, instead of the correct one.


I can try to fix this locally but I worry about seeing other unforeseen problems later. Is there a parameter somewhere to avoid this problems altogether?

Thanks!

Cesar Castro
You should probably look at the dimensions of your system. Why are the velocity numbers so huge? Are you try to use pixels units? Box2D is tuned for meters and objects between 0.1 and 10 meters.
That was exactly the problem! We were mapping pixels-units 1-1.

Thanks for the help!

Cheers,

Cesar

Original thread: https://box2d.org/forum/viewtopic.php?t=4058