# Applying power by amount of time passed?

**URL:** <https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 26, 2021, 3:15am UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295 "2021-05-26T03:15:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![int](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@int](https://discourse.processing.org/u/int)\
**Post date:** [May 26, 2021, 3:15am UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/1 "2021-05-26T03:15:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [May 26, 2021, 3:19am UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/2 "2021-05-26T03:19:12Z")

</div>

> [@int](#):
>
> set the time as a variable counting by millis

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.

---

<div class="post-metadata">

**Author:** ![int](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@int](https://discourse.processing.org/u/int)\
**Post date:** [May 26, 2021, 5:11am UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/3 "2021-05-26T05:11:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 26, 2021, 8:43pm UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/4 "2021-05-26T20:43:18Z")

</div>

ok so let’s think about that 😉

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

![Capture d’écran de 2021-05-26 22-35-07](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fa29b9cd5c0613dc979ad9c5390fbe03c0cf36f6.png)

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/186ce4a31d5514f95ffa48f0cba98d9a9096c8d9.png)

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

You can also check other cool functions on this page :

> **[Easing Functions Cheat Sheet](https://easings.net/)**
>
> Easing functions specify the speed of animation to make the movement more natural. Real objects don’t just move at a constant speed, and do not start and stop in an instant. This page helps you choose the right...

---

<div class="post-metadata">

**Author:** ![alexsunny123](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@alexsunny123](https://discourse.processing.org/u/alexsunny123)\
**Post date:** [June 2, 2021, 8:03am UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/5 "2021-06-02T08:03:34Z")

</div>

> [@int](#):
>
> 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

thanks for the awesome information.

---

<div class="post-metadata">

**Author:** ![alexsunny123](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@alexsunny123](https://discourse.processing.org/u/alexsunny123)\
**Post date:** [August 16, 2021, 12:35pm UTC](https://discourse.processing.org/t/applying-power-by-amount-of-time-passed/30295/6 "2021-08-16T12:35:15Z")

</div>

> [@alexsunny123](#):
>
> > [@int](#):
> >
> > 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. [https://ometv.onl](https://ometv.onl) [https://chatroulette.top](https://chatroulette.top)
> 
> thanks for the awesome information.

thanks my issue has been fixed.
