# Trouble with bouncing ball

**URL:** <https://discourse.processing.org/t/trouble-with-bouncing-ball/14312>\
**Category:** Coding Questions\
**Created:** [September 30, 2019, 8:56am UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312 "2019-09-30T08:56:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [September 30, 2019, 8:56am UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/1 "2019-09-30T08:56:33Z")

</div>

Hi, so far I just have a ball bouncing on the screen, but the sketch stops working when I add the number 125 into the - if (y \<= 125), part of my code, although it works when I have 100 instead of 125.

Can anyone explain to this me?

Thanks in advance 🙂

```auto
int y = 100;
int ySpeed = 5;

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

void draw() {
  background(0);
  fill(255, 0, 0);
  ellipse(width/2, y, 50, 50);
  
  y = y + ySpeed;
  
  if (y >= height-25) {
    ySpeed = ySpeed * -1;
  }
  
  if (y <= 125) {
    ySpeed = ySpeed * -1;
  }
  
}``
```

---

<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:** [September 30, 2019, 10:13am UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/2 "2019-09-30T10:13:26Z")

</div>

> [@abigail](#):
>
> if (y \<= 125) {  
> ySpeed = ySpeed \* -1;

Try

`ySpeed = abs(ySpeed);`

That’s always positive and not going back and forth. (The “`abs`” means “absolute” and just cuts off the minus sign, so it’s always positive. A positive number stays positive. See reference for `abs()`)

---

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [September 30, 2019, 12:04pm UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/3 "2019-09-30T12:04:44Z")

</div>

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 30, 2019, 12:27pm UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/4 "2019-09-30T12:27:28Z")

</div>

Hi,

I found that 105 was the point where it did a song and dance; you can figure out why examining your code.

The abs() as @Chrisir suggested is the key; your code was toggling it and may not have been always in the expected state (but in the programmed state).

Then you define direction -1 or +1.

Also useful for troubleshooting to see state of variables in code:  
[https://processing.org/reference/print\_.html](https://processing.org/reference/print_.html)  
[https://processing.org/reference/println\_.html](https://processing.org/reference/println_.html)

🙂

---

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [September 30, 2019, 12:40pm UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/5 "2019-09-30T12:40:55Z")

</div>

Thanks for the reply! 🙂 I think I get it

---

<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:** [September 30, 2019, 2:04pm UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/6 "2019-09-30T14:04:25Z")

</div>

Yeah, we could say after the if-clause applied and speed changed its direction, the ball was still inside that area so the condition still applied and the direction was changed again and so on.

We see this as a stutter.

Now with abs() the direction is just set as we want it and can’t go back and forth

You might change your other if clause too

---

<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:** [September 30, 2019, 2:08pm UTC](https://discourse.processing.org/t/trouble-with-bouncing-ball/14312/7 "2019-09-30T14:08:17Z")

</div>

My first reply was wrong - I had -1 there

Corrected it

Apologies
