While moving the ball it sometimes sticks itself to the y co-ordinate and moves around the same co-ordinate for both bricks as well as paddle. Please help. Been stuck on this for a while and can't figure what's causing the problem on my own. Code-

move(ball, xvelocity, yvelocity);
if (getX(ball) + getWidth(ball) >= getWidth(window) || getX(ball) <= 0)
   xvelocity = -xvelocity;
if (collision != NULL)
   if (collision == paddle ||strcmp(getType(collision),"GRect") == 0)
        yvelocity = -yvelocity;
else if (getY(ball) + getWidth(ball) >= getHeight(window))
        setLocation(ball, 190, 290);

1 Answer 1


It sounds like you've hit a known issue with this program. Is this what's happening? The ball hits the side of the paddle or a brick and it starts bouncing inside the paddle or brick until it emerges from the other end, or forever. Here's what's happening. Because the ball hits the side of the paddle or brick, it is able to get inside of the other object. Once this happens, the code sees this as a continuous collision and just keeps rapidly reversing direction. The solution is to apply a little logic and physics.

For the brick, the solution is simple. As soon as the ball collides with a brick, delete the brick. No more collision, but remember to bounce.

For the paddle, think about what can and should happen. The ball can only hit the paddle if it is moving down, a positive y velocity. if it were moving up, that means that it must have already bounced off the bottom of the window, but that means that the player just lost a life. So, that means that the ball can never hit the paddle with a negative y velocity. If you know this, then you can use that info in the logic. If the ball collides with the paddle, do this: y_velocity = -abs(y_velocity); This will force the ball to go up instead of bouncing back and forth inside the paddle.

This is the simple solution, and what I used. A far more complicated solution is to detect which side of the paddle is being hit by the ball and making the appropriate velocity change. Hitting the top or bottom would change the y_velocity, while hitting either side means reversing the x_velocity. The complicated part would be to detect which side of the paddle is being hit, and is beyond the scope of the assignment. (maybe in the hacker edition?)

IF this answers your question, please accept this answer to mark the question as answered. Let's keep up on forum housekeeping. ;-)

  • Thanks a lot it works perfectly fine now :) I really appreciate the help. Now I can move on to next week :) Commented May 28, 2015 at 7:58
  • This is the best fix to this problem! Thank you so much.
    – peachykeen
    Commented Nov 20, 2015 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .