Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i'm trying to make a clone of brick breaker, one problem that i am running in to is to do with collision. i know how to detect if a collision has happened but i do not know how to check what side of the brick is hit.

some general background from the program. all the bricks are just 2 points (x,y) a top right point and a bottom left point. the ball is just 1 point with a radius. for my current collision detection i'm just checking if the balls coordinates is at or in between the two box coordinates

any help is appreciated and thanks in advance

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

assuming you are at a place where the ball is just contacting the brick;

You will have to create a structure that holds a point located on each edge, and a vector for each edge that is the edge's normal.

Give that structure a method that iterates through each point, creates a vector that is the difference between the ball's center & the point, dot it to the corresponding normal, and only one of the 4 results will be a positive number, that's the edge that has intersected.

pseudo code:

struct brick

Vector min, max, L, R, T, B; //letters repre points on edges
Vector normalLeft, normalRight, normalTop, normalBottom;

//ctor
Brick(min, max)
{
   //generate values for all the fields above
}

vector normalOfEdgeHit(Vector ballCtr)
{
   foreach(point i =  L, R, T, B)
   {
     if(Dot(ballCtr - i, normal[i]) > 0)
     {
       return normal[i];
     }

   }

}

in the following diagram, it would return normalB. notice how all the other normals point away from the ball (thus would return values less than 0). The points can be located anywhere on the edge, they do not need to be in the center.

It is possible to have two normals return positive when close to the corner. In that case, you must determine distance from point to line (as if the brick's edge were an infinitely long line) for both edges. Whichever is a greater distance is the edge that has intersected.

enter image description here

share|improve this answer
    
When two normals are returned it means circle collided a corner of rectangle instead of an edge. in that case normal value is a vector started from that edge and ended to the center of the circle. –  Ali.S Oct 7 '11 at 8:13
add comment

Your Answer

 
discard

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.