-3

I am trying to make a ball bounce off the edges of a rectangular shaped paddle, what I have done so far, is making the ball bounce off the top edge of the paddle. But whenever the ball touches any other side (right or left), it gets stuck within the paddle, and won't bounce off.

In order to make the ball bounce off the top of the paddle I have used the below straight forward code:

// bounce ball off paddle
        if (object == paddle)
        {
                velocityY = -velocityY;
        }

Can anyone help me enhance this code, so that it can detect, whether the ball has touched the right or left side of the paddle, and make it bounce away.

Please note, that I am using C.

Thank you,

Yamen Tawk
  • 67
  • 2
  • 10

1 Answers1

0

Just like you did with Y axis, after a collision set

velocityX = -velocityX

If you want something "more polished" you can try to make something like

if (currentX + ball_radius >= MAX_WIDTH)
    velocityX = -velocityX;
if (currentY + ball_radius >= MAX_HEIGHT)
    velocityY = -velocityY;

where the point (currentX, currentY) represents the center of the ball.

igng
  • 340
  • 5
  • 16