Jump to content
  • Advertisement
Sign in to follow this  

breakout and collision detection

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

/source void TimerFunction(int value) { // Reverse direction when you reach left or right edge if(x > windowWidth-rsize || x < -windowWidth) xstep = -xstep; // Reverse direction when you reach top edge if(y > windowHeight ) ystep = -ystep; // Actually move the square x = xstep + x; y = ystep + y; // Check bounds. This is in case the window is made // smaller while the rectangle is bouncing and the // rectangle suddenly finds itself outside the new // clipping volume if(x > (windowWidth-rsize + xstep)) x = windowWidth-rsize-1; else if(x < -(windowWidth + xstep)) x = -windowWidth -1; if(y > (windowHeight + ystep)) y = windowHeight-1; // else if(y < -(windowHeight - rsize + ystep)) // y = -windowHeight + rsize - 1; // Redraw the scene with new coordinates glutPostRedisplay(); glutTimerFunc(33,TimerFunction, 1); } /source I am working on breakout game, again, I need to determine collision detection.

Share this post

Link to post
Share on other sites
There's not really a question here, but I'll help you conceptually at least. You've got the idea of the wall collisions, but I'm giving you it again in textual form, almost pseudocode, so that you can figure the code out yourself.

Think about the collisions that occur in Breakout. You have the ball bouncing off the walls, the ball hitting the blocks, and the ball hitting the paddle. What you're needing to do is check on each frame where the ball is and if it's collided with anything.

Remember, in terms of sprites, it's generally taken as read that the origin of the sprite is the top-left most pixel (if you draw it out on a coordinate plane i.e. graph paper, it'll be (0,0)) so that's the basis I'm working on here.

In terms of the blocks, I would store them in some kind of data structure, like std::vector. This allows you to run a loop on every frame checking if the ball falls inside the rectangular area of any of the blocks in the vector and, if it does, remove that block and possibly reverse the direction of the ball to send it back the way it came (depending on how your particular version of the game works; are you going to keep the ball progressing forward after it's hit a block or will it immediately go back the way it came back down towards the paddle?).

All you need to do for the walls is check the following:

  • Check whether the ball's x-coordinate is < 0 (the left wall)

  • Check whether the ball's x-coordinate is > the width of the window + the ball's width (the right wall)

  • Check for the top as you may have a situation where there's a gap causing the ball to go straight off the top of the screen. For that, just check if the ball's y-coordinate + the ball's height < 0

  • Check for the bottom of the screen; for this, you'll want the ball's y-coordinate + the ball's height > window height

For the ball and bat collision:

  • Check whether the ball's y-coordinate + ball's height is >= bat's y-coordinate AND <= bat's y-coordinate + bat's height

  • THEN check whether the ball's x-coordinate is >= bat's x-coordinate AND <= bat's x-coordinate + ball's width

For each block:

  • Loop through the data structure holding your blocks:

    for each block in data structure
    check if ball's y-coordinate is <= block's y-coordinate and >= block's y-coordinate + block height
    check whether ball's x-coordinate >= block's x-coordinate and <= block's x-coordinate + block's' width

  • Run your code for destroying the block (you might have an animation or something)

  • Remove the block from the data structure; looping over the same data structure in your render loop will cause the block to disappear as it no longer exists to be drawn)


I hope this helps you. My idea now is that you'll take these ideas, be able to carefully dissect and understand how it's all supposed to work, and write your own code around it.

Bear in mind that this is the way I would personally do it; it's not guaranteed to be the best or most efficient and it's highly likely that somebody will come up with a better way.

Good luck, let us know how it goes and if you need any further help.

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.

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!