# Pong collision when ball hits paddle

**URL:** <https://discourse.processing.org/t/pong-collision-when-ball-hits-paddle/15281>\
**Category:** Coding Questions\
**Created:** [November 6, 2019, 8:54pm UTC](https://discourse.processing.org/t/pong-collision-when-ball-hits-paddle/15281 "2019-11-06T20:54:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FilDomInteractive](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fildominteractive/32/6678_2.png) [@FilDomInteractive](https://discourse.processing.org/u/FilDomInteractive)\
**Post date:** [November 6, 2019, 8:54pm UTC](https://discourse.processing.org/t/pong-collision-when-ball-hits-paddle/15281/1 "2019-11-06T20:54:39Z")

</div>

Hello, can you help me how to do collision so when the ball hits the paddle, it will go back?

The code \>

```auto
float x = 400;
float y = 300;
float k = width - 20;
float e = height / 2;
float c = 715;
float h = height / 2;
float xSpeed = 3.3;
float ySpeed = 3.3;

void setup() {
  size(800, 600);
}

void draw() {
  fill(0, 10);
  noStroke();
  smooth();
  rect(0, 0, width, height);
  
  x = x + xSpeed;
  
  fill(255);
  ellipse(x, y, 20, 20);
  
  rect(k, e, 10, 100);
  rect(c, h, 10, 100);
  
  if(e > height - 110) {
    e = height - 110;
  }
  if(e < 0) {
    e = 0;
  }
    if(h > height - 110) {
    h = height - 110;
  }
  if(h < 0) {
    h = 0;
  }
  if(x > width) {
    xSpeed = xSpeed * -1;
  }
  if(x < 0) {
    xSpeed = xSpeed * -1;
  }
}

void keyPressed() {
  if(keyPressed) {
    if(key == 'w') {
      e = e - 11;
    }
  }
  if(key == 's') {
    e = e + 11;
}
    if(keyCode == UP) {
      h = h - 11;
    }
  if(keyCode == DOWN) {
    h = h + 11;
}
}

```

---

<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:** [November 6, 2019, 9:01pm UTC](https://discourse.processing.org/t/pong-collision-when-ball-hits-paddle/15281/2 "2019-11-06T21:01:20Z")

</div>

Sure

first check if the y pos of the ball is inside the paddle (y\>paddle1y && y\<paddle1y+100)

And x is near paddle surface

- if so, say xSpeed = xSpeed \* -1;

- if not, restart game / ball

AND delete

```auto
  if (x > width) {
    xSpeed = xSpeed * -1;
  }
  if (x < 0) {
    xSpeed = xSpeed * -1;
  }

```

because it lets the ball return when NOT inside the paddle

**Remark 1**

Add `y = y + ySpeed;`

**Remark 2**

And please

```auto
  rect(k, e, 10, 100);
  rect(c, h, 10, 100);

```

make better names for your variables such as

- paddle1X, paddle1Y
- paddle2X, paddle2Y

Chrisir

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 7, 2019, 11:46am UTC](https://discourse.processing.org/t/pong-collision-when-ball-hits-paddle/15281/3 "2019-11-07T11:46:57Z")

</div>

You might want to initialize some of your global variables (such as k, e, and h) inside your setup instead. To understand why, add this line of code under `size(800, 600);` :

```auto
println(k);

```
