Jump to content
  • Advertisement
Sign in to follow this  
pulpfist

Breakout collision accuracy

This topic is 4490 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 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 this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!