# Slow down object's speed

**URL:** <https://discourse.processing.org/t/slow-down-objects-speed/29937>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [May 7, 2021, 12:30pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937 "2021-05-07T12:30:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![akira](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@akira](https://discourse.processing.org/u/akira)\
**Post date:** [May 7, 2021, 12:30pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/1 "2021-05-07T12:30:58Z")

</div>

Hello! I’m a bit stuck with my conditional statement for this basic program I’m working on. Two objects at different y coordinates start moving towards the width of the screen at varying speeds. When they approach each other at a specific distance, both objects need to slow down and once they get past that distance, their original speed also returns.  
Here’s the function I created to compute the distance between the objects and adjust the speed accordingly:

```auto
void closingDist(Bubble other) { 
    float d = dist(xpos, 0, other.xpos, 0); //declared a float d which computes the distance between the xpos of the two objects
    if (d <= 4) { //made a conditional statement dependent on the value of d
      xspeed--; //if d<=4 (ie if the objects are within 4 pixels horizontally), speed will slow down
      //having a problem since both bubbles start of at the place and the bubble just starts going backwards or just maintains that speed 
      stroke(255, 0, 0);
      line(xpos, ypos, other.xpos, other.ypos); 
    } 
  }

```

I also tried multiplying the speed by a percentage but it never returns to the original speed even when the distance is more than 4 pixels. I believe my condition works since the part of the code that connects the two bubbles when in the same range works but I’m just stuck with how I can adjust the speed.  
Any clarifications would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![CodingIsLife](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@CodingIsLife](https://discourse.processing.org/u/CodingIsLife)\
**Post date:** [May 7, 2021, 3:15pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/2 "2021-05-07T15:15:02Z")

</div>

> [@akira](#):
>
> Two objects at different y coordinates

Okay… I don’t get it. If 2 objects are at different y coordinates then why are you calculating the distance like this?

> [@akira](#):
>
> `float d = dist(xpos, 0, other.xpos, 0);`

Instead you should calculate the distance like this  
`float d = dist(xpos, ypos, other.xpos, other.ypos);`

---

<div class="post-metadata">

**Author:** ![akira](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@akira](https://discourse.processing.org/u/akira)\
**Post date:** [May 7, 2021, 4:14pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/3 "2021-05-07T16:14:07Z")

</div>

Oh sorry! I didn’t think it was necessary at first because I thought I only need to compute the distance between the x coordinates so I set it to 0. Noob mistake.

---

<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:** [May 7, 2021, 4:20pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/4 "2021-05-07T16:20:06Z")

</div>

I think that’s okay, you can use 0 instead of y when you’re just interested in x

The problem might be elsewhere

---

<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:** [May 7, 2021, 4:33pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/5 "2021-05-07T16:33:04Z")

</div>

> [@akira](#):
>
> it never returns to the original speed

Do you have a code line that says speed=4; ? It’s not in the code section you posted.

Also 4 pixels is a very small amount. When your xspeed is 4 then this distance might never occur

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2021, 4:59pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/6 "2021-05-07T16:59:37Z")

</div>

Hello @akira,  
Could adding an **else - statement** to the function to switch back to the original speed be a possible solution?  
🤓

---

<div class="post-metadata">

**Author:** ![akira](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@akira](https://discourse.processing.org/u/akira)\
**Post date:** [May 7, 2021, 5:27pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/7 "2021-05-07T17:27:23Z")

</div>

Not in the class code! However, it’s part of the arguments for my constructor which looks like this:

```auto
Bubble(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

```

Unfortunately, the instructions for the project require that the objects must slow down once they’re within 4 pixels of each other so I don’t really know how I should work around that.

Apologies if I used the wrong terms–I’m still familiarizing myself with the concepts!

---

<div class="post-metadata">

**Author:** ![akira](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@akira](https://discourse.processing.org/u/akira)\
**Post date:** [May 7, 2021, 5:38pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/8 "2021-05-07T17:38:51Z")

</div>

Thank you for your reply! I tried doing that but I don’t know how I can return the original value when it uses a value that was put in my main line of code (this function was written in my class). xspeed = xspeed as an else - statement, for example, shows the error that it has no effect. I’ve written in a previous reply the arguments for the constructor that I made if that helps.

---

<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:** [May 7, 2021, 6:11pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/9 "2021-05-07T18:11:15Z")

</div>

> [@akira](#):
>
> ```auto
> xspeed = tempXspeed;
>  
> 
> ```

To have this in your constructor won’t help, since it is called only once and not throughout. You need to repeat this line, eg in an else-part of your if-clause

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 7, 2021, 6:28pm UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/10 "2021-05-07T18:28:54Z")

</div>

Ah!! I had a feeling my suggestion was probably missing something… 🙂

---

<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:** [May 8, 2021, 5:56am UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/11 "2021-05-08T05:56:06Z")

</div>

@debxyz : I had just overlooked yours - was on my phone

I am sorry!!

---

<div class="post-metadata">

**Author:** ![akira](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@akira](https://discourse.processing.org/u/akira)\
**Post date:** [May 8, 2021, 7:13am UTC](https://discourse.processing.org/t/slow-down-objects-speed/29937/12 "2021-05-08T07:13:38Z")

</div>

It’s alright!! I tried doing your earlier advice about changing the condition from 4 since it’s too small to actually see the difference and you’re right! I also added a new variable that replaces xspeed (slows it down to half its value). Thank you so much for helping me!
