Angular Velocity = Nan

Topics: Developer Forum
Jul 4, 2009 at 12:25 AM

I've been having a major issue with Farseer recently with the game throwing a Arithmetic Exception inside IntegrateVelocity(float dt). I've looked around and have seen that many people have tried just using different values, so I tried that out, and it's still crashing. It appears that as soon as there is one collision, it blows up. I've changed the values to more realistic numbers.

Here's the body and geom values i'm using in xml format:

      <Position>0 0</Position>
      <LinearVelocity>0 0</LinearVelocity>

I'm applying force or impulse of around 50-250 every 5th frame.


I also added a try/catch block around the code that errors, to keep it running, and it appears that everything in the simulator just goes to crazy values and flys off the screen.

Jul 4, 2009 at 12:48 AM

Not much to do about it than to tweak values to get something decent.

It happens when you apply a lot of force or impulse. The force/impulse can be generated by joints or a high speed collision. It can also come directly from you.
Try finding the place in your game where it happens and look at the code that is invoked. You should also try scaling all your values down by some factor. Scaled values will produce the same results, but will have NaN errors less often and can provide a more stable simulation at times.

Jul 4, 2009 at 1:03 AM

thanks for the quick reply.

Oh thank god for SVN Revert. Got it back working again.

I guess I just have to find the sweet spot.

Jul 4, 2009 at 9:33 PM

Question about scaling? When you say trying to scale all values down, what do you mean by all?  For example I am assuming you mean forces, impulses, mass, drags.  But do you also mean distances between vectors and MOI too?

Jul 4, 2009 at 10:18 PM

Scaling forces, impulse, mass and drag coefficients would give you the exact same simulation in the same dimensional scale. Scaling the size of the geometries can also have it's advantages.

As for MOI (Moment Of Inertia), it depends on mass and radius (I took circle as an exsample). If you scale down the mass, it will automatically change the MOI, changing radius will have the same effect.

It basically comes down to this:

Scaling the forces, impulses and the like will result in lower numbers for the solvers in the engine. Lower numbers means lower possibility of catching a NaN (Not a Number).

Scaling the size of units (and force, impulse and the like) will make it possible for you to zoom in your game. We have the ConvertUnits class in the engine to help you quickly convert between simulation units and display units. We use iterative solvers that come to a conclusion over several steps. They approximate the real value instead of determining it right away. We need to do that because it gives better performance. Iterative solvers (in our case) collect some kind of error that we would like to be as small as possible. We also collect values from the last step to use in the current for faster solving (called warm starting). All of this can benefit from scaling in some way or another.

It will become more apparent in Farseer Physics Engine 3.0 were we use the MKS (Meter Kilogram Second) system. The algorithms are tuned to use meters and using pixels directly can give an unstable simulation. Here it will be needed to scale everything.

Jul 7, 2009 at 3:52 AM

This may sound very crude, but it solved my problems and has had very few negitive effects. I was getting the NaN crash some time back but very rarely. So I simply put this code in where I was having the crash:

if (AngularVelocity == Nan)
    AngularVelocity = 0;

That might give you very strange behaviour, but for what I'm doing it worked great. 

Jul 10, 2010 at 12:17 PM
I had same problem, but when i set MomentOfInertia to my bodies it seems been solved. I hope :)