Applying power by amount of time passed?

Hi, I’m struggling to figure out how you can set the speed/power of a moving object based on time. For example, When you press a key, a ball is thrown from one end to the other. the time taken to press the key is calculated and the amount of force applied on the ball depends on the time; e,g pressing the key after 2 seconds moves the ball slightly, pressing the key after 10 seconds makes the ball bounce much faster.

How could this be achieved? Should the time be measured with frameCount, or set the time as a variable counting by millis? I want to set the max amount of seconds at a set amount, so I’m wondering if the 2nd option would be more viable.

Do this. When the button is pressed, record the time with millis(). When it is released, record the time again. Then subtract, and you get the elapsed time between these two events.

You can also check, probably in draw(), if it has been more than a certain amount of time since the button was pressed (and not released). In this case, you can throw the thing at max power, or have it explode, or whatever.

1 Like

Hmm yeah I get that, having trouble trying to execute it as a code and would really appreciate an example. I don’t know how to actually make the ball launch depending on how much time has passed, and making the ball’s speed change per second passed wouldn’t work out

ok so let’s think about that :wink:

If you make a graph of the speed compared to the time you have :

Capture d’écran de 2021-05-26 22-35-07

here you have the time (t) on the x axis and speed on the y axis. The speed vary linearly compared to the time.

But that’s not what you want because you want the ball to move faster as the time increase. So you need a function that does not vary linearly and that is increasing over time.

What about f(x) = x * x ?

Capture d’écran de 2021-05-26 22-41-10

It’s looking nice! The speed is increasing faster as the time goes :yum:

You can also check other cool functions on this page :

3 Likes

thanks for the awesome information.

2 Likes

thanks my issue has been fixed.