4
\$\begingroup\$

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 checking if the ball's coordinates are at or in between the two box coordinates.

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Ali1S232 Oct 7 '11 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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