# breakout - ball takes out too many bricks

I have breakout mostly working, but I can't figure one issue out. Sometimes, my ball will break through multiple bricks. Instead of hitting one and bouncing back towards the bottom, it rips through a bunch.

``````// move ball along at angle
move(ball, velocityX, velocityY);

// bounce off right edge of window
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
velocityX = -velocityX;
}

// bounce off left edge of window
else if (getX(ball) <= 0)
{
velocityX = -velocityX;
}

// bounce off top edge of window
else if (getY(ball) <= 0)
{
velocityY = -velocityY;
}
// hits the bottom edge of window
else if (getY(ball) + getHeight(ball) >= (getHeight(window) - getHeight(paddle) - 5))
{
lives = lives - 1;
printf("Lives remaining: %i\n",lives);
setLocation(ball, (WIDTH / 2) - (RADIUS / 2), (HEIGHT / 2) - (RADIUS / 2));
waitForClick();
}
// linger before moving again
pause(10);

// detect ball collision
GObject object = detectCollision(window, ball);
if (object != NULL)
{
// if ball collides with a rectangle
if (strcmp(getType(object), "GRect") == 0)
{
// reverse y velocity if ball collides with paddle
{
velocityY = -velocityY;
}
// remove brick, decrease brick count, increase points count
else
{
velocityY = -velocityY;
removeGWindow(window, object);
points = points + 1;
bricks = bricks - 1;
}
}
}
}
``````
• How big is your ball and how much space is there between your bricks? May 12 '15 at 4:49
• @SimpleSnail my ball has a radius of 10 and the space between my bricks is 3
– JR34
May 13 '15 at 1:16

Probably the ball hits a brick, the velocity on the y-axis gets inverted and the ball goes down, the ball hits another nearby brick, the velocity on the y-axis gets inverted again and the ball goes up again and so on.

I imagine the fix could be a bit difficult to implement but feel free to try! My suggestion is to implement something as follows:

``````1. if the ball hits a brick
2.     handle brick collision // remove the brick, update the score, etc.
3.     if the brick is hit from its bottom
4.         change the velocity on the y-axis so that the ball moves down
5.     else if the brick is hit from the top
6.         change the velocity on the y-axis so that the ball moves up
``````

You may also implement the way the ball should bounce off horizontally (on the x-axis).

• Thanks @Kareem for the suggestion. I don't think this is it, but it may be that this is happening very quickly. It looks more like sometimes it just blows through a bunch of them, not bouncing up and down repeatedly.
– JR34
May 13 '15 at 1:18
• @JR34 Indeed it happens quickly, if it happens. Did you try using `gdb` to figure out what was going on?
– kzidane
May 13 '15 at 10:58
• I slowed down the speed and you were right. On your fix, isn't this what is already happening? Also, if I submitted this do you think I would I have points deducted?
– JR34
May 13 '15 at 21:08
• @JR34, not quite. You're inverting the velocity on the y-axis depending solely on whether the ball hit a brick. There are situations where you don't want the velocity of the ball on the y-axis to be inverted. For example, physically speaking, in a situation like this, where the ball hits an a brick as it was going down as a result of hitting another brick earlier shouldn't make the ball go up again unless the second brick was hit from the top. For your other question, I'm afraid breakout isn't checked (i.e., you get a 1.0 if you submit it).
– kzidane
May 13 '15 at 22:03
• ahh I get it now. Thanks for all the help!
– JR34
May 13 '15 at 23:43