# 2D Collision detection: rewinding to moment of first intersection

I am working on a collision system in a Platformer tile-based game. All tiles are AABB.

I am trying to implement "Rewind to Moment of First Intersection" mentioned in this post's second point.

The following case is to be considered:

Without vectors for clarity: With vectors: I am trying to find the vector $$\\vec{w}\$$, which is the vector $$\\color{red}{\vec{v}}\$$ multiplied by a factor $$\f \in (0, 1) \$$. All other vectors are known.

$$\\color{red}{\vec{v}}\$$ represents the reversed movement (or velocity) vector between current and previous frame, whereas $$\\vec{w}\$$ represents $$\\color{red}{\vec{v}}\$$ scaled in such a way, that translating the player object over $$\\vec w\$$ leaves the player in the moment of first intersection.

$$\\color{green}{\vec{a}, \vec{b}}, \color{blue}{\vec{c}, \vec{d}}\$$ represent the penetration depths of colliding objects on corresponding horizontal/vertical axises.

Tiles' width and height is also known.

Assuming that tiles are AABB, strictly-aligned, immovable and all share the same width/height - how do I find the vector $$\\vec{w}\$$?

Seeing pseudocode would be awesome.

I am also having surprisingly hard time finding any collision solving learning materials that would suit my needs; if anyone knows such materials, I would be thankful for sharing.

• Would you change the problem space here, in that you know that the next frame the current velocity will cause a collision, could you solve this by calculating the velocity to just touch, rather than trying to test back tracking. You have an AABB so you have 4 points, which you could do a ray cast from each corner in the direction of your velocity. I believe this thread might help -> gamedev.stackexchange.com/questions/18436/… . Just a thought. – ErnieDingo Jan 2 '20 at 22:45

## 1 Answer

I managed to solve collisions problem by splitting the collision handling into horizontal and vertical components, one at a time. The implemented idea is:

1. Move player on horizontal axis.
2. Check for collisions.
3. Solve collision on horizontal axis.
4. Move player on vertical axis.
5. Check for collisions.
6. Solve collisions on vertical axis.

This system does not support high-velocity movements, of course. Other than that I did not found any collision-related 'bugs' in my tile-based game. Not sure about the efficiency.

I am still, however, very interested on the question I asked.