Public Group

# Breakout - ball to block collision

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

## 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 on other sites
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 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)
{

}

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 19
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633380
• Total Posts
3011578
• ### Who's Online (See full list)

There are no registered users currently online

×