Take the 2-minute tour ×

A square tile collides with another square tile. The bartender says...

I have:

  • The height, width, x, and y of both tiles.
  • The 2D vector of the movement which caused the collision.

I need to know from what SIDE the collision occurred (e.g. top, bottom, left, right) in order to reset the location appropriately.

I will give a mental cookie to whoever can answer this question, because I've been trying for too many hours and this seems fundamental.

share|improve this question
are you looking for a coding solution in any particular language or just algorithmic tips? –  Joe Feb 21 '11 at 6:06
I'm coding this in node.js javascript, but really it's just an algorithm question at this point. I don't have access to anything particularly fancy as far as math libraries. –  slifty Feb 21 '11 at 6:08
actually, after thinking about it for a while, with the given information, it's not possible to work out the side of impact because say r1 is moving down on r2, it then clips, overlaps on 2 sides, then overlap on 1 side, then 2 sides on the opposite side then leaves. how do i know whether i'm calculating the contact overlap or the leaving overlap... –  Joe Feb 21 '11 at 6:43
Well, you do know the vector that directed it through, so you know what the original position was. This should make it possible to figure out which one is the right one. –  slifty Feb 21 '11 at 6:45
basically, all you can do here is use the coordinates r1 and r2 and the velocity of the moving rect to predict (using y=mx+b) where it will collide rather than use actual collision detection –  Joe Feb 21 '11 at 6:46

4 Answers 4

up vote 2 down vote accepted

Given r1 and r2 (r2 being stationary), first find the closest corner of r2 to r1. This point is (c1.x,c1.y) and imagine now you extend this out into two planes, one parallel to the x axis and one to the y.

Now get the closest corner of r1 to r2 (call it c2) and use it in the following formula y = mx + b where b is c2.x and m is your vector. and x is c1.x

So if y is greater than c1.y then it means at the point of x contact (width) you've already hit the top. If it's less, then you haven't hit it yet. Invert for bottom/top.

share|improve this answer
Remember I'm not moving pixel by pixel, I'm moving with a vector. What happens if more than one side has overlap? –  slifty Feb 21 '11 at 6:29
I've updated my answer, it's not exact but i think with a bit of vector maths you can get somewhere... –  Joe Feb 21 '11 at 6:37
float player_bottom = player.get_y() + player.get_height();
float tiles_bottom = tiles.get_y() + tiles.get_height();
float player_right = player.get_x() + player.get_width();
float tiles_right = tiles.get_x() + tiles.get_width();

float b_collision = tiles_bottom - player.get_y();
float t_collision = player_bottom - tiles.get_y();
float l_collision = player_right - tiles.get_x();
float r_collision = tiles_right - player.get_x();

if (t_collision < b_collision && t_collision < l_collision && t_collision < r_collision )
//Top collision
if (b_collision < t_collision && b_collision < l_collision && b_collision < r_collision)                        
//bottom collision
if (l_collision < r_collision && l_collision < t_collision && l_collision < b_collision)
//Left collision
if (r_collision < l_collision && r_collision < t_collision && r_collision < b_collision )
//Right collision

This doesn't solve when object is inside one of the other. But it does work with overlapping

share|improve this answer
Just wanted to expand on this because it wasn't immediately clear what's happening here: player/tile bottom and right represent the bottom and right sides of the player and tile respectively. b_collision represents the distance between the bottom of the tile and the top of the player. This is the distance between sides that will be the smallest if the player is colliding with the tile from the bottom, which is precisely what is checked to see if there is indeed a bottom collision. The other collisions are checked doing the same things with the other sides. –  Benjamin Kovach Aug 15 '13 at 18:56
was looking for this , thanks! –  user63898 Feb 6 at 11:07

Assuming you have a way to detect collisions, understanding which sides collided is straight forward. You simply need to examine the x and y positions of each square.

Square1 : (x1, y1)
Square2 : (x2, y2)

I'll work from the assumption that the top left corner of your work area is (0,0) and that x values increase as you move right, and y values increase as you move down.

With this in mind:

If (x1 < x2), the right side of square 1 collided with the left side of square 2
If (x1 > x2), the left side of square 1 collided with the right side of square 2
if (y1 < y2), the bottom side of square 1 collided with the top side of square 2
if (y1 > y2), the top side of square 1 collided with the bottom side of square 2

I suggest you draw yourself a few pictures, and it should become clear to you.

share|improve this answer
I know how to detect top vs bottom and left vs right -- but what happens when the top and the left overlap? My trouble is figuring out which overlapped first. –  slifty Feb 21 '11 at 6:16
Take the difference between x1 and x2 and y1 and y2. The larger (absolute) difference will be the side that impacted first. –  Nathanael Feb 21 '11 at 6:21
that's not necessarily true. if you have wide rectangle that inches upwards, then the collision should be from top/bottom but the x2-x1 diff would be much larger given the width –  Joe Feb 21 '11 at 6:27
What Joe said -- that was my akin to my first attempt and then I realized that edge case. –  slifty Feb 21 '11 at 6:28
You're right - my bad - my excuse is it's late at night where I am :P –  Nathanael Feb 21 '11 at 6:29

you have to model your square to capture the orientation too , just with (x,y,height) of the square , orientation i mean whether its in first ,second ,third of fourth quadrant , may be by modeling all four corner vertices of square .

then you need to determine , all the four vectors that make this square

find cosine between the 2D Vector you have with each side of square for example if cosine of given side and 2D Vector is 1 , then both are orthogonal or 0 they are perpendicular , any other value would fall in between

or any other vector algebra tricks on how you want to determine/define your collision !

share|improve this answer

Your Answer


