# Limit speed of an object

**URL:** <https://discourse.processing.org/t/limit-speed-of-an-object/2044>\
**Category:** Coding Questions\
**Created:** [July 24, 2018, 12:00pm UTC](https://discourse.processing.org/t/limit-speed-of-an-object/2044 "2018-07-24T12:00:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattwonderwall](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mattwonderwall](https://discourse.processing.org/u/mattwonderwall)\
**Post date:** [July 24, 2018, 12:00pm UTC](https://discourse.processing.org/t/limit-speed-of-an-object/2044/1 "2018-07-24T12:00:30Z")

</div>

I’ve been breaking my head over how I can limit the speed of a bouncing ball. I’ve chosen that for every time it hits, to increase the speed by 5%, using the following code.

```auto
  if (xpos > width-rad || xpos < rad || ypos > height-rad || ypos < rad) {
    xspeed = xspeed *1.05;
    yspeed = yspeed *1.05;
  }

```

At a certain point the speed obv gets too fast. How can I limit it ( i tried several options) or alternatively; have it decreased back to the original speed and then start the loop again?

---

<div class="post-metadata">

**Author:** ![alexr4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexr4/32/576_2.png) [@alexr4](https://discourse.processing.org/u/alexr4)\
**Post date:** [July 24, 2018, 12:03pm UTC](https://discourse.processing.org/t/limit-speed-of-an-object/2044/2 "2018-07-24T12:03:08Z")

</div>

You can use the constrain method to clamp your speed values : [https://processing.org/reference/constrain\_.html](https://processing.org/reference/constrain_.html)

```auto
xpseed = constrain(minSpeed, maxSpeed);

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 24, 2018, 12:19pm UTC](https://discourse.processing.org/t/limit-speed-of-an-object/2044/3 "2018-07-24T12:19:04Z")

</div>

> [@alexr4](#):
>
> You can use the constrain method to clamp your speed values : [https://processing.org/reference/constrain\_.html](https://processing.org/reference/constrain_.html)
> 
> ```auto
> xpseed = constrain(minSpeed, maxSpeed);
> 
> ```

It should be  
`xspeed = constrain(xspeed, minspeed, maxspeed);`  
no?

Also what you can do is simply check if your speed is over a certain value:

```auto
if (xpos > width-rad || xpos < rad || ypos > height-rad || ypos < rad) {
    xspeed = xspeed *1.05;
    yspeed = yspeed *1.05;

    if (xspeed > maxspeed) {
        xspeed = maxspeed;
    }

    if (yspeed > maxspeed) {
        yspeed = maxspeed;
    }
}

```

---

<div class="post-metadata">

**Author:** ![alexr4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexr4/32/576_2.png) [@alexr4](https://discourse.processing.org/u/alexr4)\
**Post date:** [July 24, 2018, 12:22pm UTC](https://discourse.processing.org/t/limit-speed-of-an-object/2044/4 "2018-07-24T12:22:01Z")

</div>

Yes, my bad I forgot the first param 😕  
Using a condition is great too 😉
