Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.