Assert with Joints - b2Assert(kNormal > b2_epsilon);

Hey Guys,

I'm failing an assert in b2ContactSolver.cpp, line 96: b2Assert(kNormal > b2_epsilon);

How I get it:
In my game, I have a built in editor. While I'm in the edit mode, I no longer simulate physics, but I can edit the physics world (not sure if that is relevant or not).

In the game I have:
One long static rectangular body
Two boxes ontop of the rectangular body
A character I can move around using keyboard control

While I am in edit mode, I attach a distance joint from one box to the other, then I start playing the game.

I can use my character to push box A (bodyA in the joint) without any problem. It doesn't look like the joint is actually attached at this point as box B doesn't move around when I'm pushing box A. Once I go and try to push around boxB, I get the assert.

I saw another post here: viewtopic.php?f=4&t=3893&p=18914&hilit=kNormal+%3E+b2_epsilon#p18914
...but I dont think this is the issue I'm having.

...
When the assert happens, the variables around it have values of:
kNormal = 1.#QNAN00
bodyA->m_mass = 0.00000000
bodyB->m_mass = 2.8900001
bodyA->m_invMass = 0.00000000
bodyB->m_invMass = 0.34602076
bodyA->m_invI = 0.00000000
bodyB->m_invI = 0.71838206
rnA = 1.#QNAN00
rnB = 1.#QNAN00
ccp->rA = {x=1.#QNAN00 y=1.#QNAN00 }
ccp->rB = {x=1.#QNAN00 y=1.#QNAN00 }
cc->normal = {x=1.#QNAN00 y=1.#QNAN00 }
j = 0
worldManifold[j] = {x=1.#QNAN00 y=1.#QNAN00 }
bodyA->m_sweep.c = {x=1.#QNAN00 y=1.#QNAN00 }
bodyB->m_sweep.c = {x=17.219999 y=-0.86499977 }

...totally guessing, but it looks like all of the nonsense starts because bodyA->m_sweep.c = NaN. I wouldn't be surprised if it's something I've screwed up.

Any advice would be appreciated. Thanks.
I'm also running into an error with b2Assert(kNormal > b2_epsilon), using Alchemy with Box2D.

It happens randomly after a certain number of level transitions (between 10-20). One of the boxes or cannon balls in the level will throw an error. I can't get a clearer stack since I'm testing from Flash, but here is the order:

b2World::Step()---> Solve(step);
b2World::Solve()--->island.Solve(step, m_gravity, m_allowSleep);
b2Island::Solve()--->b2ContactSolver contactSolver(step, m_contacts, m_contactCount, m_allocator);
b2ContactSolver()::b2ContactSolver()--->b2Assert(kNormal > b2_epsilon);
Please try to reproduce this in the testbed. Make sure you are not passing bad/uninitialized data to Box2D.
I reproduced a similar situation, but haven't been able to reproduce the error in the test bed.

You can see the error in the game here: http://www.stencyl.com/play.php?game=cannon by going to level 6 (Phase or Break) and clicking the restart button (green arrow) multiple times.

Attachment:
7-27-2010 4-42-07 AM.png
7-27-2010 4-42-07 AM.png [ 12.93 KiB | Viewed 622 times ]


Here is the testbed code I used though just incase:

Code:
#ifndef MULTIBOX_H
#define MULTIBOX_H

class MultiBox : public Test
{
public:
   MultiBox()
   {
      m_world->SetGravity(b2Vec2(0.0f, -75.0f));

      {
         b2BodyDef bd;
         bd.position.Set(0,21.0f);
         b2Body* ground = m_world->CreateBody(&bd);         
         
         b2PolygonShape shape;
         shape.SetAsBox(40.0f, 2.0f);
         ground->CreateFixture(&shape, 0.0f);
      }

      {
         b2BodyDef bd;
         bd.position.Set(0.0f,0.0f);
         b2Body* ground = m_world->CreateBody(&bd);         
         
         b2PolygonShape shape;
         shape.SetAsBox(40.0f, 2.0f);
         ground->CreateFixture(&shape, 0.0f);
      }

      {
         b2PolygonShape shape;
         shape.SetAsBox(1.0f, 1.0f);         

         for (int32 r = 0; r < 6; r++)
         {
            for (int32 s = 0; s < 8; s++)
            {
               b2BodyDef bd;
               bd.position.Set(0.0f + (r*4),3.0f + (s*2.15));
               bd.type = b2_dynamicBody;
               b2Body* body = m_world->CreateBody(&bd);
               body->CreateFixture(&shape, 0.0f);
            }
         }
      }
   }

   static Test* Create()
   {
      return new MultiBox;
   }
};

#endif
We're still digging for this one, what we found was:

For the error, the m_I (and m_invI) of a body would instantly change to a negative after a couple of scene transitions, which causes the kNormal >b2_episilon assert to fail. We haven't figured out what is causing the m_I to suddenly become negative.

The "random" movement of the crates seemed to be caused by b2fixture->m_conveyorBeltSpeed (Alchemy Version exclusive?). Its used in b2ContactSolver::SolveVelocityConstraints(), but we have no clue where its being set. The Stencyl engine doesn't set it, and I couldn't find what set it in Box2D.

Here is its usage in b2ContactSolver::SolveVelocityConstraints():

Code:
/// AS3
dv.x += c->fixtureA->m_conveyorBeltSpeed * tangent.x;
dv.y += c->fixtureA->m_conveyorBeltSpeed * tangent.y;
dv.x -= c->fixtureB->m_conveyorBeltSpeed * tangent.x;
dv.y -= c->fixtureB->m_conveyorBeltSpeed * tangent.y;
/// END AS3


We're currently looking through b2Body::SetMassData(const b2MassData* massData) to see what is causing the m_I to go bad.
Many debuggers let you set a break point when a location in memory is overwritten. Try to do this with the m_I or m_invI value. The debugger in Microsoft Visual Studio has this feature.
Thanks! We found what was causing the problem: b2MassData->center sometimes would initialize its values incorrectly (didn't have default values assigned). The boxes that worked were at 0,0, while those that break would have different values, causing the m_I value to go into the negatives. Setting the center coords to 0,0 on b2MassData's creation seems to have solved the problem. Also seems to be the problem for m_conveyorBeltSpeed. Setting its default value to 0 fixed it.

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