# Resolving collisions after initial collision

Given a sweep test for a ray, how do we check collisions after the 1st collision has been detected, and the ray has been deflected.

Physics system steps are:

1. Update velocities (gravity, etc)
2. Resolve collisions
• The ray will intersect the floor, and will be moved back at the penetration amount.
• The new velocity is calculated as a reflection
3. Update positions
• The ray is now on the wrong side of the wall

This all happens in 1 frame

If the orange ray hits the floor just before the wall, the new velocity will tunnel the ray through the wall within one frame. The naive way to fix this would be to after each collision check all the bodies again with the new velocity, but this can take multiple iterations if the ray bounces around in a corner. There is no way of knowing how many times the ray will need to be processed.

What would be a good way to fix this issue?

It sounds like you already understand the solution:

Keep iterating.

When the swept test intersects an object, reflect it, and perform a second sweep along the new direction and remaining distance.

This remaining distance only decreases with each bounce (especially if you apply a small skin offset to prevent tunnelling), so this will terminate eventually.

If you're worried about performance cost spikes from the occasional tight bounce, you can also enforce something like a 3-bounce limit and accept discarding some time as an approximation error. On the last bounce, place the object at the beginning of the reflected ray, rather than at the end, and resume its sweep testing motion next frame.

You can tune the limit to your liking to find the right balance between performance control and accuracy — from a 0-bounce limit where objects hesitate a frame on every contact (large error), to a 10-bounce limit where hopefully very few objects will ever hit the cap in a single frame. You can even adjust the cap dynamically based on load, or object importance.

Well, you need to know the time to collision. Then negate it from the simulation time: and pass new velocity with new time to the 2. So you will check the collisions along the reflection vector and still simulate collisions until the simulation time is 0. The step 2 should look like: while (time >= some_tiny_epsilon_as_close_as_possible_to_zero) { Perform ray traces and collisions... }

In case when move is completed and no collision occured just set the time to 0.

Hope it helps