• Advertisement
Sign in to follow this  

Breakout - ball to block collision

This topic is 4145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am currently working on a breakout clone and am having some difficulty with this particular piece of code. I'm using the Allegro Library. Blocks are draw according to a 2D array. This is also in C plus plus. TILE_H = 10 TILE_W = 40 i = current tile_x in brick array j = current tile_y in brick array All of the following code takes place in a for loop. It just loops through the entire brick array. The following code correctly identifies the ball colliding with the bottom of a brick.
if ((ball1.x >= (i*TILE_W)) && ball1.x <= ((i*TILE_W)+TILE_W))
{
     //collision on the x axis
     if ((ball1.y >= (j*TILE_H)) && ball1.y <= (j*TILE_H)+TILE_H)
	{
	// collision on the y axis
	allegro_message("Collision:BOTTOM");
        }
}

I can't seem to figure out what the "opposite" code would be to detect a collision on the top of the brick? I've tried a few different ways but to no avail. Any help would be greatly appreciated. -Jay

Share this post


Link to post
Share on other sites
Advertisement
This code just detects if a point is inside a rectangle.To detect if it's on the top or the bottom,check the sign of the y component of the velocity vector.

Share this post


Link to post
Share on other sites
I would have the position referens in the center.

ball.centerYoffset = ball.height >> 1;

ball.bottomEdge = ball.y - ball.centerYoffset;

if (ball.bottomEdge =< tile.y && ball.bottomEdge >= tile.topEdge)
{

}
else
if (ball.topEdge >= tile.y && ball.topEdge =< tile.bottomEdge)
{

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement