# Square collision: Detect which side of the wall it collided [duplicate]

This question already has an answer here:

I'm using a standard square collision in my 2D game. The player shoots little balls, and when they hit any wall it should bounce.

The problem is that I know when it hits a wall but I don't know if the collision happened on the side or top/bottom of the wall.

Here's my collision algorithm (pseudo code):

``````If
A.x < (B.x + B.width) AND
B.x < (A.x + A.width) AND
A.y < (B.y + B.height) AND
B.y < (A.y + A.height)
Then a collision happened
``````

Is it easy to detect if the collision happened on a side or top/bottom? If yes how should I do it?

-

## marked as duplicate by Sean Middleditch, ashes999, bummzack, Seth Battin, Byte56♦Aug 8 '13 at 13:21

This question was marked as an exact duplicate of an existing question.

## 1 Answer

The easiest and most common way is to maintain information of the current and last position of the moving projectile. You can get a direction vector. If you know that and the length of a timestep that passed since it was last updated, you can even determine the point of collision, reverse the appropriate speed (and apply friction and such if you wish) and move the object to that direction the remaining amount in the timestep. It makes the model more accurate.

-