When implementing breakout, i am having a hard time trying to figure out how to check if the ball hits the horizontal edges of the paddle. So far everything else works fine, but when the ball touches the paddle edges, the ball goes crazy. I know the problem has to do with how i set up the velocity, but i am unsure how to check this case. Please help?

Below is a snippet of my code.

//move ball in horizontal direction move(ball, 5*horizontalVelocity, verticalVelocity);

        //detect collision
        GObject object = detectCollision(window, ball);

        //check if ball hits paddle or bricks
            if(object != NULL){
                if(strcmp(getType(object), "GRect") == 0){
                    if(object == paddle){
                        verticalVelocity = -verticalVelocity;

                        //if ball hits bricks
                    verticalVelocity = -verticalVelocity;

  • your last if condition does not make sense - you do the same in both cases. – user2477 Feb 20 '15 at 8:55

In pseudocode I did something like this:

Once I'm sure the ball hit the paddle

1. Get the ball and paddle coordinates.
2. If the ball bottom is greather than paddle top plus some gap
  2.1 If the vel is greather than 0 (moving to the right) and the right edge of ball is less than left paddle edge plus some gap.
    2.1.1 Then velx = -velx
  2.2 Else, if the vel is less than 0 (moving to the left), and left ball edge is greather than right paddle edge plus minus some gap.
    2.2.1 Then velx = -velx
| improve this answer | |

You must log in to answer this question.

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