Search code examples
c++runtime-errorbox2dgame-physicsassert

Box2D assertion failed: kNormal > 1.19209289550781250000e-7F


I was running a Box2D simulation in a C++ program, when this error aborted the program:

a.out: ./Box2D/Dynamics/Contacts/b2ContactSolver.cpp:96: b2ContactSolver::b2ContactSolver(b2Contact**, int32, b2StackAllocator*, float32): Assertion `kNormal > 1.19209289550781250000e-7F' failed.
  • What does this assert fail indicate?
  • What could have caused it?
  • In what ways could I fix it?

I don't have any further context that could relate to the issue.


Solution

  • The assert means at least three things:

    One: You're running a debug build of Box2D.

    Two: You're running an older version of Box2D than the source code hosted at Erin's Box2D GitHub repo.

    After searching around, it seems that the most recent source code that was released by Erin which has this assert in it is Box2D_v2.1.2. The uploaded date for this zip file is April 17, 2010.

    The source code for b2ContactSolver.cpp from this older Box2D version 2.1.2 that you're running, shows the following relevant code surrounding the assert on line 96:

    float32 kNormal = bodyA->m_invMass + bodyB->m_invMass + bodyA->m_invI * rnA + bodyB->m_invI * rnB;
    
    b2Assert(kNormal > b2_epsilon);
    ccp->normalMass = 1.0f / kNormal; 
    

    Three: The sum of the inverse masses of body A and body B and their effective inverse rotational-based masses, is not greater than b2_epsilon where in this release of Box2D b2_epsilon is set to FLT_EPSILON (in b2Settings.h).

    This could happen for a variety of reasons like both bodies somehow having zero inverse masses. If any of the component values of kNormal was NaN for instance, I believe the greater-than check would also fail. kNormal being less than zero would of course also cause this check to fail.


    As for what you could do to further asses and fix the problem, here's some ideas that come to mind...

    1. You could review your source code that uses Box2D to see if there's any way that your bodies have invalid masses, invalid inverse masses, invalid rotational inertias, or invalid inverse rotational inertias.
    2. You could upgrade to a more recent version of Box2D and see if the problem goes away.
    3. You could use a non-debug build of Box2D and see if you get a divide by zero fault or not.