Rotation and NaN exception

Oct 17, 2007 at 3:20 AM
I am currently experiencing some rather odd behavior with certain components of the engine. I am getting a NaN exception when my game entities begin rotating at a very high rate. Realistically this isn't really a problem as I would never actually intend for my objects to be rotating at near infinite speeds. I suppose that brings me to the problem at hand... has any one else experienced objects rotating at abnormally high speeds? I have read several other posts regarding the NaN exception and it would seem as though this is in the same vein, however, I would like to avoid the rotation altogether.

The scenario is as follows: I have a bullet with a mass of .01 and a ship with a mass of 40,000. When the bullet collides with the ship at just the right spot, the ship begins to spin uncontrollably and moves in the opposite direction at an extremely high velovity; the exception follows. The moment of inertia, rotationalDragCoefficient, and LinearDragCoefficient for the ship are all set rather high, so I am not entirely sure how to resolve the problem. I know this is not really a solution, but is there a way to completely disable rotation? Thanks for your time!
Oct 17, 2007 at 9:39 AM
Sounds like you are reaching the limts of the engine. High speed low mass objects will have a tendancy to 'blow-up' (ie. go out of control). You should try to keep masses within the same order of magnitude. I think it's going to be a matter of tuning your varibles till you get something that works.

Oct 17, 2007 at 6:27 PM
Thanks Jeff! I am currently at work, but will try to tinker with the variables once I get home. It did seem odd though, that the ship and the bullet both went haywire and not just the bullet. I would think that since the ship is so much heavier than the bullet, it would not be affected. Nonetheless, I am looking forward to trying a few things out to see if it makes a difference. Thanks again!
Oct 17, 2007 at 11:26 PM
You said your RotationalDragCoef is high. How high? RotationalDrag acts in the opposite direction the body is spinning. Possibly you have it set so high it is unstable?? Just a guess.
Oct 18, 2007 at 1:41 PM
Ah yes! My RotationalDrag was set over 1000.00, so that probably had a lot to do with the problem. I tried it again with all of the default values and did not have the problem at all; thanks for the suggestion!

Also, since I don't really want any of my objects to rotate, I am just setting the body rotation value to 0.0 on every update call. I know this is fairly "hacky," but I am not sure of another way get around this issue. Nonetheless, it works for now. Thanks again!
Oct 18, 2007 at 11:22 PM
If you don’t want bodies to rotate you should set moment of inertia to float.PositiveInfinity.

Crashlander I also had problems with rotating objects angle getting too large for floats that’s why I clamp their angle every time step in my engine.

Here is the code for that:
        public static Scalar ClampAngle(Scalar angle)
            if (-PI <= angle && angle < PI) {     return angle; }
            Scalar rem = (angle + PI) % (TWO_PI);
            return rem + ((rem < 0) ? (PI) : (-PI));
Oct 19, 2007 at 11:41 AM
Edited Oct 19, 2007 at 11:42 AM
I'll see about adding that, BioSlayer.

I originally didn't clamp my angles because it was messing with my angular springs where I need to take the difference between two angles. I wanted to allow the angle diff to be greater than 2Pi. Think of winding the spring up several revolutions. I assumed float.Infinity would be large enough that no body would reach that many rotations. I guess I was wrong.

Maybe I can clamp the angle to some higher value so as to allow "winding" up the spring but not past a certain point.

Jan 23, 2008 at 11:42 AM
Edited Jan 23, 2008 at 11:45 AM

Anything new on that? It's making me crazy.

I can play with the values as much as I want and it's still sometimes happening.

How can I help to solve this? Is there some kind of tool with which I can export all engine values when it happens? Also, the engine doesn't crash always. Sometimes I just get a black screen and the user interface is still functional. So the "export values" functionality should be also callable manually.

I think I'll try to limit all values. It may cost quite some speed but that's ok.
Jan 23, 2008 at 5:03 PM
The variables all turn NaN in sort of a chain-reaction way. Crazy math calculations result in a float.NegativeInfinity (I thought I found where, but I can't remember at the moment. At work.) which is later passed into a Matrix. For some reason I am unaware of, -Infinity * 0 = NaN. So this matrix multiples a bunch of 0s to a bunch of -Infinities when it runs. What the matrix return is then NaN of course. That's passed to either a poition or impulse variable (again, can't remember) which is then used in calculating other variables, and so on. After a cycle or so, it crashes.

About that rotation clamping, it first needs to be done in the Body object. See my last comment (not the main post) in the issue tracker: (The code I gave uses MathHelper which I think is an XNA class, so watch out for that.)
That TwoPi through -TwoPi thing has thrown off a lot of my projects.

As for the anglular springs, I'd just say have it clamp the ammount of angular force it applies. Of course, expose a way to modify where it clamps.)
Jan 24, 2008 at 1:51 PM
Edited Jan 24, 2008 at 1:51 PM
The changes suggested by in Yota's issue tracker item have been made and will be in the next release.

Angular springs already have a "MaxTorque" property. It defaults to infinity. You should be able to set this to keep it from exploding.

One thing, though. If you have something that is 'exploding' (causing NaNs), clamping values is only fixing the symptom. Most likely there is something wrong with the underlying set of physics parameters or there really is an bug in Farseer. Either way, clamping the values might hide the true problem.

That said, if you are stuck and it's holding you up, then clamp away! :-)
Jan 25, 2008 at 8:23 AM
Edited Jan 25, 2008 at 8:25 AM
That said, if you are stuck and it's holding you up, then clamp away! :-)

... or I'll let the vehicles explode.
Case closed xD.

I mean, if the collision produces such violence then they would be destroyed anyway oO. I think :) ... I'll just have to make sure that the engine doesn't crash ... uhm ... yeah :). I'll try. It doesn't crash always, there is only one place in the code where it happens. So I guess I'll put in a new property which indicates: Blow this vehicle up. Somehow. An event would be nice ...

I'll post my solution the next days and maybe someone will tell me if it's ok that way ;D.