# Breakout collision accuracy

## Recommended Posts

Hi Im making a breakout game using direct draw. Everything seems to be working ok except for the collision between the ball and the blocks. Here is what I do to make the ball bounce back from the screen edges:
...
if(pt_ball.x < 0 || pt_ball.x > SCREEN_WIDTH-BALL_WIDTH)
{
ball_angle += PI - ((ball_angle - PI) * 2);
}

if(pt_ball.y < 0 || pt_ball.y > SCREEN_HEIGHT-BALL_HEIGHT)
{
ball_angle += PI + ((PI/2 - ball_angle) * 2);
}
...

I realized that this seemed to work, so I left it like that. Here is how I detect collision between the ball and the blocks:
...
if(pt_blocks[i][j].active)
{
// Is the ball overlapping this block?...
if(pt_ball.x+BALL_WIDTH >= pt_blocks[i][j].x &&
pt_ball.x <= pt_blocks[i][j].x+BLOCK_WIDTH &&
pt_ball.y+BALL_HEIGHT >= pt_blocks[i][j].y &&
pt_ball.y <= pt_blocks[i][j].y+BLOCK_HEIGHT)
{
pt_blocks[i][j].active = false;

// Is the ball near the left or right edge of the block?...
if(abs(pt_ball.x - pt_blocks[i][j].x) >= BLOCK_WIDTH / 2 - ball_speed_x)
{
//
}
else
{
ball_angle = -ball_angle;
}
}
}
...

This seems to work when the ball hits either the top or bottom side of the block. What Im thinking right now is that the if test Im doing to determine if the ball is close to the edge of the block is not accurate enough. So my question is simply, is there a way to determine which of the four sides of the block the ball hits?

##### Share on other sites
You arn't the first to ask this :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982243

• 10
• 9
• 11
• 24
• 11