Jump to content
  • Advertisement
Sign in to follow this  
BobTheElder

Breakout Collisions

This topic is 4726 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

I am trying to make a breakout game where a ball bounces around the screen and destroys the blocks. I have gotten the program to tell when a block and the ball collide, but i can't figure out how to know which way the ball should bounce. For example, when the ball hits on the left or right side of the block, the ball should change its left-right direction. I cannot figure out how to tell if it hits the sides or the top and bottom. Any suggestions would be appreciated. Thanks. [Edited by - BobTheElder on June 16, 2005 11:03:54 AM]

Share this post


Link to post
Share on other sites
Advertisement
Here's a nice way of doing it:


int left = brick->x;
int top = brick->y;
int right = left + BRICK_WIDTH;
int bottom = top + BRICK_HEIGHT;

/*
* Which side of the brick we are bouncing off? First, we determine the
* horiz. and vert. distances to each side from the centre of the ball.
*/


int distToLeft = std::abs((ball->x + BALL_RADIUS) - left);
int distToRight = std::abs((ball->x - BALL_RADIUS) - right);

int distToTop = std::abs((ball->y + BALL_RADIUS) - top);
int distToBottom = std::abs((ball->y - BALL_RADIUS) - bottom);


/*
* Then we need to know which side is the shortest distance from the ball.
*/


int xMinDistance = std::min( distToRight, distToLeft );
int yMinDistance = std::min( distToTop, distToBottom );


if ( xMinDistance < yMinDistance )
ball->dx *= -1.0; // Change ball's x-direction.
else
ball->dy *= -1.0; // Change ball's y-direction.














I even wrote a tutorial on how to make breakout on my website.

[Edited by - EnemyBoss on June 16, 2005 1:55:32 PM]

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!