Pong - Ball Bouncing Bug

Hi,

in school we are making a Pong Game. I only have a problem with the bouncing when the ball touches the paddle on one of the edges the ball gets stuck in the paddle.

Bounce Method:

void testCollision(Ball ball) {
if (x < ball.getX() + ball.getRadius() &&
x + playerWidth > ball.getX() - ball.getRadius() &&
y < ball.getY()&&
y + playerHeight > ball.getY()) {
ball.bounceX();
} else if (y < ball.getY() - ball.getY()&&
y + playerHeight > ball.getY() + ball.getY()&&
x < ball.getX() + ball.getRadius() &&
x + playerWidth > ball.getX() - ball.getRadius()) {
ball.bounceY();
}
}

Sorry for the format i couldn't make it cleaner looking. I would take advices for it.

If someone needs more code from the game i could give it to you

can you show the code for bounceX?

*-1 ?

Sure

void bounceX() {
this.angle = -this.angle;
}

void bounceY() {
this.angle = PI - this.angle;
}

try check left and right side separately

then you can say

void bounceXLeft() {
this.angle =   abs(this.angle);
}


void bounceXRight() {
this.angle =  - abs(this.angle);
}

With the functions implemented the ball sometimes flies through the paddle when I don't move it.

The code:

if(x < ball.getX() + ball.getRadius()){
ball.bounceXRight();
}else if(x + playerWidth > ball.getX() - ball.getRadius()){
ball.bounceXLeft();
}

can’t hep you with that

you need to debug your code

1 Like

Thanks anyway

1 Like

Read this article by @quark