views:

166

answers:

3

Hello.

I am writting a collision detection engine for my game and I have some problems.

Indeed, since I have several fixed rectangle and one moving (the player), I need to know which side of the fixed one was collided at first by the player, to replace him correctly.

The fixed rectangle are NOT in a grid so they can be placed anywhere on the map and they can have diffents size. They are not rotated.

The player class stores it's direction vector.

Any idea?

KiTe

+3  A: 

The name is "Axis-Aligned Bounding Box" collision detection.

Now you know the name, you can Google for the rest.

Lie Ryan
+2  A: 

In a nutshell:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. If the top(Y) of the player is less than the bottom of an enemy then you don't need to check anymore because it's not possible that they're colliding. If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. It would help to define top, right, bottom, left of each object you intend to check inside the class. This will allow you to know which side is hit also. This should be enough to get you thinking and experimenting.
Have fun!

kirk.burleson
A: 

thanks to both of you for your help.

I've heard about AABB, but at first sight it didnt seem to fit to my needs (since I didn't understand it well).

But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB !

kite