EndContact Frame delay

Hi,

I have a platformer project where I change a player behaviour from jump to run based on landing on the ground. On entering the jump behaviour I apply a velocity using SetLinearVelocity. I can see the velocity and the position of the player shift upwards after the b2world step but it doesn't EndContact with the floor until the following frame due to the ordering of the code (i.e. the step coming after the m_ContactManager.Collide()) as shown here (see arrowed section) -

Code: Select all

void b2World::Step(float32 dt, int32 velocityIterations, int32 positionIterations)
{
   b2Timer stepTimer;

   // If new fixtures were added, we need to find the new contacts.
   if (m_flags & e_newFixture)
   {
      m_contactManager.FindNewContacts();
      m_flags &= ~e_newFixture;
   }

   m_flags |= e_locked;

   b2TimeStep step;
   step.dt = dt;
   step.velocityIterations   = velocityIterations;
   step.positionIterations = positionIterations;
   if (dt > 0.0f)
   {
      step.inv_dt = 1.0f / dt;
   }
   else
   {
      step.inv_dt = 0.0f;
   }

   step.dtRatio = m_inv_dt0 * dt;

   step.warmStarting = m_warmStarting;
   
---->   // Update contacts. This is where some contacts are destroyed.
   {
      b2Timer timer;
      m_contactManager.Collide();
      m_profile.collide = timer.GetMilliseconds();
   }<----

   // Integrate velocities, solve velocity constraints, and integrate positions.
   if (m_stepComplete && step.dt > 0.0f)
   {
      b2Timer timer;
      Solve(step);
      m_profile.solve = timer.GetMilliseconds();
   }

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

   if (step.dt > 0.0f)
   {
      m_inv_dt0 = step.inv_dt;
   }

   if (m_flags & e_clearForces)
   {
      ClearForces();
   }

   m_flags &= ~e_locked;

   m_profile.step = stepTimer.GetMilliseconds();
}


This means that in the update for the jump behaviour it believes it is on the ground for the first frame and switches it out of the jump and into the run (effectively not jumping). I hacked this at first by adding a count to allow it to not land based on it having a vertical velocity but it just didn't seem right. So to get the contact to update immediately I have shifted the highlighted code lower down so the contact will be ended on the same frame as so -

Code: Select all

void b2World::Step(float32 dt, int32 velocityIterations, int32 positionIterations)
{
   b2Timer stepTimer;

   // If new fixtures were added, we need to find the new contacts.
   if (m_flags & e_newFixture)
   {
      m_contactManager.FindNewContacts();
      m_flags &= ~e_newFixture;
   }

   m_flags |= e_locked;

   b2TimeStep step;
   step.dt = dt;
   step.velocityIterations   = velocityIterations;
   step.positionIterations = positionIterations;
   if (dt > 0.0f)
   {
      step.inv_dt = 1.0f / dt;
   }
   else
   {
      step.inv_dt = 0.0f;
   }

   step.dtRatio = m_inv_dt0 * dt;

   step.warmStarting = m_warmStarting;
   
---->/*   // Update contacts. This is where some contacts are destroyed.
   {
      b2Timer timer;
      m_contactManager.Collide();
      m_profile.collide = timer.GetMilliseconds();
   }
*/<----
   // Integrate velocities, solve velocity constraints, and integrate positions.
   if (m_stepComplete && step.dt > 0.0f)
   {
      b2Timer timer;
      Solve(step);
      m_profile.solve = timer.GetMilliseconds();
   }

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



   if (step.dt > 0.0f)
   {
      m_inv_dt0 = step.inv_dt;
   }

   if (m_flags & e_clearForces)
   {
      ClearForces();
   }

 ----> // Update contacts. This is where some contacts are destroyed.
   {
      b2Timer timer;
      m_contactManager.Collide();
      m_profile.collide = timer.GetMilliseconds();
   }<----

   m_flags &= ~e_locked;

   m_profile.step = stepTimer.GetMilliseconds();
}


This makes sense to me but I am concerned there is a reason the m_ContactManager call is made earlier so if anyone knows the reason it comes first or any issues I may face with having moved this call to be later that would be great.

Cheers
Perhaps you could just not allow it to switch out of the jump until a given time has passed, eg. 10 time steps - some reasonable value after which the body should have cleared the ground.
I had done exactly that and as mentioned in the initial post another similar way was not to allow ground collision with a positive Y velocity check. These seemed like hacks to me though so I removed them and looked at the route cause of the problem. If the body has evolved such that it isn't on the floor I think it is right that it should call EndContact or some kind of fix up immediately and not the frame after. There are no visible problems with moving the code but it is the consequences of moving this code and not understanding what it could cause in the future that concerns me. If moving it is not the right thing to do then is there a way the contacts could be fixed up at the end of the step function so there isn't a frame delay in the call to endcontact?
I see. My guess is that it will not matter that this section is moved, but at most it only gains you one time step, and this kind of problem is probably better tackled at a higher level 🙂
If for some reason the player still did not clear the ground after that one time step you would have the same situation again (although I guess then the "some reason" could likely be a good reason for the jump to be canceled anyway).

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