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

I'm trying to find out how to check collision with different rectangles. Now the cubes I have have a x, y, height, and width to them.

So it might look like this on the screen:

This is the orientation a rectangle should have in my game

And not like this:

This is the orientation a rectangle should not have in my game

Now I want to detect collision and then determine what to do. This is where I'm stuck. I don't know how to detect if two rectangles are inside each other or not and how to determine which side can the object not move to. If anyone knows what algorithm to use here to determine these thing help is much appreciated.

share|improve this question
Just to be clear, the objects theselves are not tilted? Or just the collision box? – Mitchell Mar 22 '13 at 8:43
Well, pretty much the collision box is straight. Basically I was trying to ask about how to detect a non tilted rectangle collision box with a x, y, width, and height. – LiquidFeline Mar 22 '13 at 19:59

This sounds like you're looking for basic AABB bounding box collision, which is essentially rather trivial.

Essentially you've got two similar problems here (the same problem in two different dimensions):

Determine whether two ranges overlap.

Why? Simple: If the ranges in one dimension overlap, both rectangles are at least in some way on the same height (i.e. next to each other; even if that distance is a bit far). If the ranges in both dimensions overlap, your rectangles overlap as well (i.e. they collide).

Determining whether two ranges in one dimension overlap is a rather trivial problem:

  • The right border of rectangle a must not be left of the left border of rectangle b.
  • The right border of rectangle b must not be left of the left border of rectangle a.
  • If both conditions are true, the ranges overlap.

Applying this to both dimensions is rather easy:

collide_x = a.x + a.w > b.x && b.x + b.w > a.x; // collide horizontally
collide_y = a.y + a.h > b.y && b.y + b.h > a.y; // collide vertically
collide = collide_x && collide_y; // collide in both dimensions

Or, unified to check everything within one line:

collide = a.x + a.w > b.x && b.x + b.w > a.x && a.y + a.h > b.y && b.y + b.h > a.y;

Once this is done, you can simply compare the x and y coordinates to determine in which direction the collision happens. You can also calculate how far these overlap by doing some more math with coordinates, e.g. using something like this:

distance_x = min(abs(a.x + a.w - b.x), abs(b.x + b.w - a.x));
distance_y = min(abs(a.y + a.h - b.y), abs(b.y + b.h - a.y));
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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