2d Collision detecton axis aligned boxes

I have recently run into some trouble with my collision detection. My game is composed of a map made out of axis aligned squares. The player has an x and y velocity, and I want to know how I can find out with which side the player is colliding. So if they move left, I want to know that if they collide with a square, and with which side they collide with it.

Edit: Basically, what I want to know is how to find out which face the player is colliding with.

• It's pretty important to know which physics engine you are using or if you are making your own. The latter would require a much more advanced answer. May 1, 2011 at 16:32
• It is the latter. I don't really have much of a physics engine, it's just has velocity. No friction or anything advanced. Velocity is set to 0 as soon as you stop pressing buttons as well, so it's not too hard. May 1, 2011 at 17:00
• Is your player modelled with an AABB box? May 1, 2011 at 20:59
• Well the player never gets rotated but yeah they do have a bounding box. May 1, 2011 at 21:01

The "right" way to do this is probably with swept collisions.

http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php?page=3

Was the result of 5 seconds of googling, but if you look for swept and AABB, you should be able to find a lot of resources.

If you're ok with using c# example code, here is an example of 2d polygon detection. It may get you started, there's a lot to put into a QA answer.

http://www.codeproject.com/KB/GDI-plus/PolygonCollision.aspx

Otherwise, just google polygon detection. Once you know which ones are colliding, finding out on which face should be easy.

• That's my problem, finding out which face is colliding, as in the point of entry. May 1, 2011 at 20:46
• To further this question, everything is a square so just querying every block the player can be touching in it's bounding box is easy. The problem I am facing is seeing if it is a vertical collision or a horizontal collision. I don't want to do the whole move the x axis, then move the y axis first, because that leaves weird traces when moving both left and down. As in you would probably stop moving on a wall that you would normally overcome. May 1, 2011 at 23:27

There are solutions for this all over the internet, stackoverflow and gamedev.SE.

Here are a couple of random ones: