Sign in to follow this  
pulpfist

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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this