Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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.