Hi, I'm having difficulty performing collision response between a 2d axis aligned box and some line segments.
Given a box with a velocity, a set of segments and a simulation time step, I'm current doing this:
Let d be the distance the box travels in the simulation time given that it collides with no segments.
1. Cast the box against all the line segements in the direction of it's velocity.
2. If the closest segment is a distance d0 away and if d0 is less than d, then move the box a distantance d0. Subtract away the simulation time
used by moving the box a distance d0 with it's current velocity.
If there were no intersections then move the box a distance d and end.
3. Subtract the velocity component normal to the segment that was collided with.
Now go to step 1 and use the updated time, position and velocity for the ray cast.
There are some details left out but that's the basic idea.
When colliding with single line segments there are no problems. However if there are two bordering line segments at an acute enough angle then the box will occasionally tunnel through. I believe the issue has to do with limited floating point accuracy. With small velocities the box is able to move arbitrarily close to a line segment which causes the ray cast in the next iteration to fail.
Here is the algorithm with more details:
while (sim_t > 0)
{
// cast box against segments
Vec2 closest_normal;
float closest_t = IntersectBoxSegments(box, box_velocity, &closest_normal);
// IntersectBoxSegments solves for the intersection between: box + box_velocity*t = segment
if (closest_t < sim_t)
{
// move the box so that it is a distance of box_velocity*FUDGE away from the closest segment
// the fudge is my attempt at keeping the box at a minimum distance away from the segment.
box.center += box_velocity*(closest_t - FUDGE);
sim_t -= closest_t;
// remove the velocity in the direction of the normal.
box_velocity -= Dot(box_velocity, closest_normal)*closest_normal;
}
else
{
box.center += box_velocity*sim_t;
break;
}
}
Below is an image showing a case where tunneling might occur.
![Dj3gd0d.png](http://i.imgur.com/Dj3gd0d.png)
I realize this is not the best method for resolving collisions so suggestions are welcome.
Thanks for the help.