Public Group

Breakout Collisions

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

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 on other sites
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 on other sites
Thank you so much that was what I needed.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• Forum Statistics

• Total Topics
634059
• Total Posts
3015292
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!