Skip to content
Advertisement

Formula to calculate time needed for a ball to reach destination where the ball’s x y is updated in a time loop

I am coding a simulation of ball movement. I have an updateBall function which runs every 100 miliseconds to update the location of the ball.

How is the formula to find out the time in miliseconds needed to reach a given target coordinate? For example, given target x=100 y=200 the time needed to reach is approximately 5300ms.

Below is the relevant code snippet,

JavaScript
JavaScript

Advertisement

Answer

My math is a bit rusty so if I am not mistaken it should be like this:

JavaScript

where t is time and dist is the distance from start to target.

so you got 2 solutions for t so use the one that makes sense (non negative). If there is no such one it means your ball never gets to your target.

Btw once you want to add stuff like gravity and other force fields or obstacles then you should change your math/physics to Newton D’Alembert integration instead of using direction vector to convert your problem to 1D as that is rather limiting.

[Edit2]

Do not forget to use compatible units as you use 0.1 sec interval for update and also you multiply speed by 100 so:

JavaScript

so you reach the target in 4.47 sec … Here values of your simulation ported to C++:

JavaScript

As you can see your simulation reaches the target slightly before 4.5 sec However yours 5.3 sec result is way too far off so there still must be something fishy.

Also Real friction behaves differently and it scales up actual speed instead so it would by applied like this:

JavaScript

where dt is the interval you update with so dt=0.1 however then the above equations will no longer work as it changes the v(t) function too.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement