Hi all,

I am trying to write my own 2D physics code from scratch using Christer Ericson's book. I'm just going for extremely basic 2D AABB collision, which I have been able to implement correctly.

While I am able to find tfirst and tlast on the collision inside of a step, and simulate upto that point, I do not know how to continue the simulation for the rest of the physics step. The strategy I had was something like (pseudocode):

t = 0;
while (t < 1)
count = 0
t_hit = 1 - t
for each pair of AABB boxes a, b
if (sweep(a, b, &hit_normal, &t_hit))
//only run if t_hit becomes smaller
pairs[++count] = (a, b, hit_normal, t_hit)
for each AABB a
a.position += a.velocity * timestep * t_hit
for (int i = count - 1; 0 <= i; --i)
pairs[i].update_velocities(a, b, hit_normal, t_hit)
if (i == 0 or pairs[i].t_hit < pairs[i-1].t_hit)
break;
t += t_hit


the problem I have is that if an object responds to a collision by sticking or sliding, or if at the start of a step two boxes already overlap, the timestep never grows. Numerical issues make it so that if I sim by exactly t_hit, the boxes may still overlap. I dont know what the correct way to handle updating the overall sim is, because you clearly cannot step past whenever the most recent hit is. I'm sure I can put some epsilon somewhere but I dont know where it is safe to do so.

any help would be appreciated!