We will check which side of the paddle the ball has hit to choose the side where it will move next. It's important to have the motion to follow through any directional hits as it would in a realistic environment. With every paddle collision, we want to make sure that the ball goes in the up direction. For this, follow these steps:
bounce()
for the ball after the movePaddle()
function:function bounce()
-3
for velocity in the y direction. This will make the ball move in an upward motion:vy = -3
paddle
and ball
objects and close the function:if((ball.x + ball.width * 0.5) < paddle.x) then vx = -vx elseif((ball.x + ball.width * 0.5) >= paddle.x) then vx = vx end end
When the ball collides with the paddle, the motion follows through, depending on what side of the paddle is touched by the ball. In the first part of the if
statement, the ball travels toward 0 in the x direction. The last part of the if
statement shows the ball travelling toward the opposite side of the screen in the x direction.