1

I have no idea why the ball will glide through the paddle when the side of the paddle is hit. It is not the same as normal case when the ball hits the top of the paddle and it bounces back.

I have written a few lines to change the Y velocity vector in the case when the ball hits the top and bottom of the bricks. But how can I change the X vector when the sides of the bricks are hit ?

Here are pseudocodes for the case of bricks collision

if the ball touched a brick
    if brick x = ball x + ball width or ball x = brick x + brick width
        change the velocity of the ball on the x-axis
    else
        change the velocity of the ball on the y-axis

1 Answer 1

11

What really causes this problem is that the ball hits one of the paddle's sides (or corners) and the bottom of the ball actually passes the top of the paddle.

let's call the distance between the bottom of the ball and the top of the paddle x. at this point, a collision with the paddle is detected and the ball goes up with a velocity on the y-axis whose absolute value is < x (i.e., |yVelocity| < x).

so even though the ball went up, technically it's still colliding with the paddle (it may be hard to notice). in the next iteration another detection with the paddle is detected (since the ball is still colliding with the paddle) so the ball goes down again.

this process keeps repeating until the ball is not colliding with the paddle anymore. here are some visualizations:

enter image description here

enter image description here

enter image description here

enter image description here

To fix this, you may handle collision with the paddle in such a way that

if the ball is going down
    bounce the ball off

This will simply ensure that whenever the ball hits the paddle, it will never go down again.


Update: If you're trying to make the ball bounce off horizontally when it touches the left/right sides of a brick, I prefer separating the conditions. This probably makes code easier to read and keeps some logical problems away. Assuming the height of my ball > the height of my bricks, the pseudocode might be something like

// if the ball is adjacent to a brick horizontally
if ball y  < brick y AND ball bottom > brick y
    change the velocity of the ball on the x-axis
else if ballY < brick bottom AND ballBottom > brick bottom
    change the velocity of the ball on the x-axis
0

You must log in to answer this question.

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