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) -
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 -
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
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