Box2DX: Heisenbug discovered (and a quick-fix solution)

Hi folks,

I've been using Box2DX quite extensively and recently found a heisenbug.

Under certain circumstances, Collision.FindMaxSeparation will enter an infinite loop. In my experience this only happened when I was running my game from the .exe, not when I launched it from the IDE - beware!

From what I can tell, this happens when C# tries to compare the variable s with bestSeparation. Under very specific values of s, when s = bestSeparation, the system will think that s > bestSeparation. When this happens, it assigns bestSeparation = s and goes through another iteration of the loop. In the next iteration, s = bestSeparation, but again the system thinks that s > bestSeparation, thus causing an infinite loop.

(Oddly enough, if you insert a System.Console.Write(s+ " ") before the greater-than check, it will correct the aberrant behaviour. That's why this is a heisenbug. I would hardly consider that to be a proper fix, though.)

Off the top of my head I can think of several ways to work around this, but here's a quick-fix I threw together. If your game is locking up inexplicably when run from the .exe, even though it seems to run fine in the IDE, try this and see if it works for you.

In the file Collision.CollidePoly.cs:

Change 1:

Original code:

Code: Select all

         else
         {
            edgeIndex = edge;
            return s;
         }

         // Perform a local search for the best edge normal.
         for (; ; )
         {
            if (increment == -1)
               edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
            else
               edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;

Modified code:

Code: Select all


         else
         {
            edgeIndex = edge;
            return s;
         }

            int firstEdge = -1;

         // Perform a local search for the best edge normal.
         for (; ; )
         {
            if (increment == -1)
               edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
            else
               edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;


Change 2:

Original code:

Code: Select all

                if (s > bestSeparation)  //-0.95 heisenbug
                {
                    bestEdge = edge;
                    bestSeparation = s;
                }
                else
                {
                    break;
                }
         }

            edgeIndex = bestEdge;

Modified code:

Code: Select all

                if (s > bestSeparation)  //-0.95 heisenbug
                {
                    bestEdge = edge;
                    bestSeparation = s;
                }
                else
                {
                    break;
                }

                if (firstEdge == -1)
                {
                    firstEdge = edge;
                }
                else if (edge == firstEdge)
                {
                    break; //nattyhack: if we have looped around to the same edge that we checked before, break out.
                }

         }

            edgeIndex = bestEdge;


I'm sure there are neater solutions, but this one seems to work for me. Hope it helps.

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