Contact events dispatched in the wrong order? :S

Heya guys, a bit of a long post here, but any help is appreciated 😉

We have a big SVG being parsed in and converted into a bunch of b2Shapes from Boris's SVG parser. The problem we're having is that we have a bike-like object running along the shape created. We're trying to monitor when the bike is 'airborne'.. in terms of when both the wheel have been off contact with the terrain for more than x ms.

We've been trying to use BEGINCONTACT and ENDCONTACT on the terrain body itself, but it's giving some really strange results. Basically sometimes a BEGINCONTACT event will be dispatched, immediately followed by an ENDCONTACT;

Code:
var terrainDispatcher:EventDispatcher = new EventDispatcher();
terrainDispatcher.addEventListener(b2World.BEGINCONTACT, onTerrainContact);
terrainDispatcher.addEventListener(b2World.ENDCONTACT, onTerrainContactEnd);
_terrainBody.SetEventDispatcher(terrainDispatcher);


Code:
private function onTerrainContactEnd(e:b2ContactEvent):void
{
   if (e.contact.GetFixtureA().GetBody() == _frontWheel || e.contact.GetFixtureB().GetBody() == _frontWheel)
   {
      // front wheel would be off the ground here
   }
   if (e.contact.GetFixtureA().GetBody() == _rearWheel || e.contact.GetFixtureB().GetBody() == _rearWheel)
   {
      // rear wheel would be off the ground here
   }
   if (_rearWheelAirBorne && _frontWheelAirBorne) // both wheels should logically be off here
}


The problem being that when the wheels leave part of the terrain, they report being BEGINCONTACT _before_ they have left the previous segment they were on, and hence the ENDCONTACT comes trailing afterwards, telling the application that the bike is airborne, which it's really not.

We tried looking at the contactlist of the terrain itself, but that seems utterly bizarre and reports contact that should not apply. When the bike is nowhere near the terrain, the front wheel and rear wheel are still in the contact list of the terrain.

At a massive dead end here and would appreciate any insight on the contactlisteners. Essentially all we're trying to distinguish is whether or not both wheels are actually off the ground.

Oh, if it helps, the ground itself is created as such;
Code:
var paths:Vector.<Vector.<b2Vec2>> =  b2SVG.parseSVG(XML(_levelDetails.svg.toString()), PHYSIC_WORLD_SCALE, 10);
for each(var path:Vector.<b2Vec2> in paths)
{
   var last:b2Vec2;
   for each(var v:b2Vec2 in path)
   {
      if (last)
      {
         var s:b2Shape = b2PolygonShape.AsEdge(last, v);
         _terrainBody.CreateFixture2(s, 0.0);
      }
      last = v;
   }
}
The problem you're running into is that the begin contact with the second segment may actually happen before the end contact with the first - even if the wheel is a circle, it might very slightly overlap both at the same time because of the way the solver works.

What you really need to do is store a contact count with each wheel, increment it on each BEGINCONTACT, and decrement it on each ENDCONTACT. Then you can check if it's greater than zero to see if it's touching something.
Sorry, I forgot to follow up on this post.

ewjordan, you're absolutely correct. This is pretty much were we ended up after a bunch of confused hours. Thanks for the reply either way though, legend.

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