Public Group

# Breakout collision accuracy

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

## 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[j].active)
{
// Is the ball overlapping this block?...
if(pt_ball.x+BALL_WIDTH >= pt_blocks[j].x &&
pt_ball.x <= pt_blocks[j].x+BLOCK_WIDTH &&
pt_ball.y+BALL_HEIGHT >= pt_blocks[j].y &&
pt_ball.y <= pt_blocks[j].y+BLOCK_HEIGHT)
{
pt_blocks[j].active = false;

// Is the ball near the left or right edge of the block?...
if(abs(pt_ball.x - pt_blocks[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 :)

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013596
×