# Pong - Ball Bouncing Bug

**URL:** <https://discourse.processing.org/t/pong-ball-bouncing-bug/30599>\
**Category:** Coding Questions\
**Created:** [June 9, 2021, 9:17am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599 "2021-06-09T09:17:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaftigMelo](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SaftigMelo](https://discourse.processing.org/u/SaftigMelo)\
**Post date:** [June 9, 2021, 9:17am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/1 "2021-06-09T09:17:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 9, 2021, 9:18am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/2 "2021-06-09T09:18:37Z")

</div>

> [@SaftigMelo](#):
>
> bounceX

can you show the code for bounceX?

`*-1` ?

---

<div class="post-metadata">

**Author:** ![SaftigMelo](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SaftigMelo](https://discourse.processing.org/u/SaftigMelo)\
**Post date:** [June 9, 2021, 9:32am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/3 "2021-06-09T09:32:22Z")

</div>

Sure

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

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 9, 2021, 9:36am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/4 "2021-06-09T09:36:56Z")

</div>

try check left and right side separately

then you can say

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

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

```

---

<div class="post-metadata">

**Author:** ![SaftigMelo](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SaftigMelo](https://discourse.processing.org/u/SaftigMelo)\
**Post date:** [June 9, 2021, 9:53am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/5 "2021-06-09T09:53:46Z")

</div>

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();  
> }

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 9, 2021, 10:23am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/6 "2021-06-09T10:23:11Z")

</div>

can’t hep you with that

you need to debug your code

---

<div class="post-metadata">

**Author:** ![SaftigMelo](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SaftigMelo](https://discourse.processing.org/u/SaftigMelo)\
**Post date:** [June 9, 2021, 10:42am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/7 "2021-06-09T10:42:23Z")

</div>

Thanks anyway

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [June 9, 2021, 11:06am UTC](https://discourse.processing.org/t/pong-ball-bouncing-bug/30599/8 "2021-06-09T11:06:44Z")

</div>

Read this [article](http://lagers.org.uk/blog/?cat=10) by @quark
