Limit speed of an object

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.

  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?

You can use the constrain method to clamp your speed values : https://processing.org/reference/constrain_.html

xpseed = constrain(minSpeed, maxSpeed);
2 Likes

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:

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

Yes, my bad I forgot the first param :confused:
Using a condition is great too :wink: