Sensor Contacts are NOT reported until next b2World::Step()

Take a static body and add a circle fixture. Take a dynamic body "above" the static body also with a circle fixture but set that as a sensor. Let the dynamic body fall until it contacts the static circle fixture.

What I find is that the BeginContact reported to my collision listener occurs after the circle fixtures overlap (I'm not talking about their respective AABB). If neither fixture is a sensor then the BeginContact is reported when they overlap.

When I trace the Box2D code I see that a contact is generated when the two shape AABB overlap as expected however, because one shape is a sensor, the contact is only reported to the contact listener (BeginContact) here during the b2World::step()

Code:
   // Update contacts. This is where some contacts are destroyed.
   {
      b2Timer timer;
      m_contactManager.Collide();  <--- This ends-up reporting all contacts involving sensors.
      m_profile.collide = timer.GetMilliseconds();
   }


This is done *prior* to the solve. In other words, sensor contacts are generated *after* this but won't be reported until you call step again. This is not the case where a fixture is not a sensor because here after the solve:

Code:
   // Handle TOI events.
   if (m_continuousPhysics && step.dt > 0.0f)
   {
      b2Timer timer;
      SolveTOI(step);
      m_profile.solveTOI = timer.GetMilliseconds();
   }


... any contacts that are overlapping will be reported to the contact listener. The problem however is that there's the following line in the TOI calculation to skip sensors:

Code:
            // Is there a sensor?
            if (fA->IsSensor() || fB->IsSensor())
            {
               continue;
            }


... which means any sensors overlapping won't get reported until the next step!!

You can't remove this line but you can add an explicit call to update the contact like this:

Code:
            // Is there a sensor?
            if (fA->IsSensor() || fB->IsSensor())
            {
               c->Update(m_contactManager.m_contactListener);
               continue;
            }


... which works. Now I get collision contacts reporting at the same time I get sensor contacts. My problem is that I'm not sure this is the best solution. I suspect what I need to do is in-between the Solve and SolveTOI is perform an iteration of the existing contacts and explicitly call updates for contacts involving sensors.

This is actually a very subtle effect and easy to miss because you will get sensor contacts reported to BeginContact however this is during the next call to b2World::Step().

Thoughts?
Here's what I ended-up adding in-between the call to Solve() and SolveTOI()...

Code:
   // Update sensor contacts immediately as this isn't handled
   for (b2Contact* c = m_contactManager.m_contactList; c; c = c->m_next)
   {
      // Is this contact disabled?
      if (c->IsEnabled() == false)
      {
         continue;
      }

      // Is there a sensor?
      if (c->GetFixtureA()->IsSensor() || c->GetFixtureB()->IsSensor())
      {
         // Yes, so update it.
         c->Update(m_contactManager.m_contactListener);
      }
   }
Sensors don't participate in continuous collision. If you turn off continuous collision, you won't get non-sensor contacts until the next time step either. Also I have to report collisions during continuous collision because they may disappear by the end of the time step (e.g. a bouncing ball).

The way contacts are reported will never make everyone happy. As it is, I compute contacts before the constraint solver so you won't get contacts reported that match the geometry at the end of the time step. If I compute contacts after the solver, you won't get the impulse results until the next step (or you may never get them if the contact stops touching). Or I could compute contacts before and after the constraint solver, making it super expensive.

You could argue that I should organize the step like this:

1. Adjust positions according the velocity
2. Collide
3. Apply gravity
4. Solve velocity constraints
5. Report contacts

This looks great until you realize that step 1 may cause tunneling through bodies that are newly created.

I'm open to new ways to organize the time step, but every permutation I consider seems to have some downsides.
I can see your dilemma there and I appreciate how producing a generic solution isn't always easy.

From a pure reporting/rendering point-of-view, having sensors reported "out-of-sync" with non-sensors can produce visually/logically odd results if you perform a physics update then render, only to see that sensors are clearly overlapping something but no sensor callback has happened which of course will happen during the next update, it's just pending.

If nothing else, I presume that what I'm doing in my second post wouldn't have any dire side-effects? The reason this is so important to me is because I'm the guy tasked with integrating Box2D into Unity for their 2D physics solution.

Anyway, I appreciate you taking the time to respond.

Thanks.

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