Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 am writing a breakout clone (my first game) and am totally stuck as to how I figure out which side of the brick was hit.

I have a collision detection method that looks like this:

DetectCollision(Object a, Object b)

   x = distance(a.x, b.x);
   y = distance(a.y, b.y);

   if (x is smaller than the combined width &  y is smaller is than combined height {
       return true;
return false;  

This works totally fine, but I need to know the side of the collision, and location relative to the center in order to respond properly.

I've spent the past few days snooping around but am lost.

share|improve this question
up vote 10 down vote accepted

This can all be gleaned from the position of the ball relative to the position of the brick it collided with.

Once you have detected a collision:

if(ballPosition.y <= brickPosition.y - (brickHeight/2))
  //Hit was from below the brick

if(ballPosition.y >= brickPosition.y + (brickHeight/2))
  //Hit was from above the brick

if(ballPostion.x < brickPosition.x)
  //Hit was on left

if(ballPostion.x > brickPosition.x)
  //Hit was on right

The first two check to see if the ball is above or below the brick. If neither than it must be next to the brick, so check which side it's on. This will need to be tweeked to fit where you're taking the location from, i.e. brickPosition is the center of the brick or brickPosition is the top left corner.

share|improve this answer

You could use Vector Math to discover the angles between them!

Here's a short answer:

ballToBrick = ballPosition - brickPosition;
brickFacing = Vector2(0,1).Normalize();

float angle = acos( Dot(ballToBrick, brickFacing) );

The Brick facing is a tricky vector, it is the "start point" of the angle calculation. and if you want the vector to point up, make it (0, 1), point left (-1, 0), right (1, 0), point down(0, -1). Assuming you're in OpenGL axis, where up and right are positive.

If you dont know, here's the definitions of each function used:

    float length = squareroot(x * x + y * y);
    x = x / length;
    y = y / length;
    return this;

Dot(Vector2 a, Vector2 b)
    return a.x * b.x + a.y + b.y



For further answer you, this will give you the following:

0 Degrees: Straight up;
90 Degrees: Straight Right; 180 Degrees: Straight Down; 270 Degrees: Straight Left;

So lets test.

0 - 45 = 360 - 45 = 315
0 + 45 = 45

Between 315 and 360 we have up. Between 0 and 45 too.

90 - 45 = 45
90 + 45 = 135

Between 45 and 135 we have left.

180 - 45 = 135
180 + 45 = 225

Between 135 and 225 we have down.

270 - 45 = 225
270 + 45 = 315

Between 225 and 315 we have left.

That's it! Easy enough, you can just build yours 'ifs' now.

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.