Body Position Becomes NaN

Do you guys know of any reason why a Body objects position might become NaN?

In my multi player game I've got the player spawning pretty instantly. If i hold down the keys ( which apply a force to the object ) when I spawn, SOMETIMES, the game crashes due to the bodies's x and y floats becoming NaN.

Its really frustrating me now, any ideas where this would be happening? I don't touch the bodies data, just apply force to it and take its position.


PS, im using the C++ 2.0.1 version.
maybe the Box2D project has one floating point model and your game has another one selected?
Have you got some sample code you could post (or even better, a testbed demo that shows the problem)? There's no reason that should happen, but it's either a bug in your code or in Box2d, and it will be easier to find if we can all reproduce it.
I can't really give you testbed as the game currently has allot of artwork.

I've commented out all my player update code ( weapons switching, chat etc ) other than this and I still get the problem:
Code:
void CEntitySoldier::Simulate( float dt, unsigned int callTime )
{
   b2Vec2 centerOfMass;
   b2Vec2 force;

   if ( p_Body->GetPosition().x != p_Body->GetPosition().x && !isNan ) {
      printf( "(CEntitySoldier::Simulate)Error: isNan \n" );
      isNan = true;
   }

   if ( input.up ) {
      force.Set( 0, -750 );
      p_Body->ApplyForce( force, centerOfMass );
   } else if ( input.down ) {
      force.Set( 0, 750 );
      p_Body->ApplyForce( force, centerOfMass );
   }
   
   if ( input.left ) {
      force.Set( -750, 0 );
      p_Body->ApplyForce( force, centerOfMass );
   } else if ( input.right ) {
      force.Set( 750, 0 );
      p_Body->ApplyForce( force, centerOfMass );
   }


   p.FromB2VEC2( p_Body->GetPosition() );
   v.FromB2VEC2( p_Body->GetLinearVelocity() );
}


"input" is just a class that holds player button data as booleans.

Here is the construction code for the players physics data:

Code:
   userData.entity = this;
   userData.object = false;

   b2BodyDef p_BodyDef;
   p_BodyDef.position.Set( p.x, p.y );
   p_BodyDef.linearDamping = 9;
   p_BodyDef.fixedRotation = true;
   p_BodyDef.userData = &userData;
   p_Body = world->CreateBody( &p_BodyDef );

   b2CircleDef s_ShapeDef;
   s_ShapeDef.radius = 1.5f;
   s_ShapeDef.density = 1;
   p_Body->CreateShape( &s_ShapeDef );
   p_Body->SetMassFromShapes();


The object it is colliding with ( Well sometimes, it also sometimes just NaN's out without touching anything ) is a tree and here is its physics data:
Code:
   b2BodyDef p_BodyDef;
   p_BodyDef.position.Set(p.x, p.y);
   p_BodyDef.userData = &userData;
   p_Body = a->spc->world->CreateBody( &p_BodyDef );

   b2CircleDef p_ShapeDef;
   p_ShapeDef.radius = 1.5f;
   p_ShapeDef.density = 0;
   p_Body->CreateShape( &p_ShapeDef );
   p_Body->SetMassFromShapes();



If i just leave the player controls alone when it starts up, its fine for a bit. I can run around and stuff, but then it will randomly NaN out again :/ i've never had this problem before.
Try turning on floating-point exceptions:
Code:
unsigned int prev;
_controlfp_s(&prev, unsigned int(~(_EM_ZERODIVIDE|_EM_INVALID)), _MCW_EM);


The floating-point unit will then trigger an exception when performing a divide-by-zero or performing an invalid floating-point operation, and your debugger will take you right to the source of the problem. It's really handy, and has already saved me a lot of debugging time.
Well that was interesting. I added what you suggested and tried it with:
Code:
   double tester = 333;
   double tester2 = 0.0;
   tester = tester / tester2;


And it works. 🙂

The only problem is, i was sure my problem was on both DEBUG and RELEASE mode, but I can't seem to get it to happen on DEBUG anymore and therefore can't catch the error 😳 after playing around with it this morning the NaN'd position definitely only happens in RELEASE mode of the program. What to do, what to do....
Believe it or not, i think its ZoneAlarm.

If i ignore a zone alarm pop up and continue playing my game, as soon as the pop up disappears it NaN's the position of my moving objects, so im guessing it must be a weird time step value or something, I will keep you updated.

EDIT: With zone alarm turned onto "Game Mode", it just accepts all requests and doesn't do any popups- If i turn this on, I have no problems. So definitely here somewhere.
Okay, i've rooted it to this function:
Code:
inline void b2Body::SynchronizeTransform()


I've changed it to:

Code:
inline void b2Body::SynchronizeTransform()
{
   m_xf.R.Set(m_sweep.a);
   m_xf.position = m_sweep.c - b2Mul(m_xf.R, m_sweep.localCenter);
   if ( m_xf.position.x != m_xf.position.x ) {
      printf( "ERROR_1\n" );
   }
}


By adding a break point on the printf i've managed to find out which effecting variable is causing the problem, its m_sweep.a = NaN

I'll keep looking into it, but any input would be great! Cheers
You can also use _isnan() to check for NAN results and _finite() to check if a result is finite and not NAN. They're the "normal" way of looking for bogus results when you aren't using floating-point exceptions.
I know 😉 just been busy scratching my head so havn't gone about anything the right way.

I think I've solved it even though I don't know how. In b2Island.cpp there is code that looks like this:

Code:
#ifdef TARGET_FLOAT32_IS_FIXED
            // Fixed point code written this way to prevent
            // overflows, float code is optimized for speed

      float32 vMagnitude = b->m_linearVelocity.Length();
      if(vMagnitude > b2_maxLinearVelocity) {
         b->m_linearVelocity *= b2_maxLinearVelocity/vMagnitude;
      }
      b->m_angularVelocity = b2Clamp(b->m_angularVelocity,
         -b2_maxAngularVelocity, b2_maxAngularVelocity);

#else

      if (b2Dot(b->m_linearVelocity, b->m_linearVelocity) > b2_maxLinearVelocitySquared)
      {
         b->m_linearVelocity.Normalize();
         b->m_linearVelocity *= b2_maxLinearVelocity;
      }
      if (b->m_angularVelocity * b->m_angularVelocity > b2_maxAngularVelocitySquared)
      {
         if (b->m_angularVelocity < 0.0f)
         {
            b->m_angularVelocity = -b2_maxAngularVelocity;
         }
         else
         {
            b->m_angularVelocity = b2_maxAngularVelocity;
         }
      }
#endif


As m_sweep.a is effected by m_angularvelocity I decided to see what happens when the upper code is actually turned on, so i commented out the unneeded and BINGO, it works.

I don't know why and I don't know what ZoneAlarm has to do with it, but for some odd reason it seems to work, we'll see for sure in time 😛

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