0
\$\begingroup\$

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?

\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

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