I've tried the typical physics equations for this but none of them really work because the equations deal with constant acceleration and mine will need to change to work correctly. Basically I have a car that can be going at a large range of speeds and needs to slow down and stop over a given distance and time as it reaches the end of its path.
So, I have:
V0
, or the current speed
Vf
, or the speed I want to reach (typically 0)
t
, or the amount of time I want to take to reach the end of my path
d
, or the distance I want to go as I change from V0 to Vf
I want to calculate
a
, or the acceleration needed to go from V0 to Vf
The reason this becomes a programming-specific question is because a
needs to be recalculated every single timestep as the car keeps stopping. So, V0
constantly is changed to be V0
from last timestep plus
the a
that was calculated last timestep. So essentially it will start stopping slowly then will eventually stop more abruptly, sort of like a car in real life.
EDITS:
All right, thanks for the great responses. A lot of what I needed was just some help thinking about this. Let me be more specific now that I've got some more ideas from you all:
I have a car c
that is 64 pixels
from its destination, so d=64
. It is driving at 2 pixels per timestep
, where a timestep is 1/60 of a second
. I want to find the acceleration a
that will bring it to a speed of 0.2 pixels per timestep
by the time it has traveled d
.
d = 64 //distance
V0 = 2 //initial velocity (in ppt)
Vf = 0.2 //final velocity (in ppt)
Also because this happens in a game loop, a variable delta
is passed through to each action, which is the multiple of 1/60s that the last timestep took
. In other words, if it took 1/60s, then delta
is 1.0, if it took 1/30s, then delta
is 0.5. Before acceleration is actually applied, it is multiplied by this delta value. Similarly, before the car moves again its velocity is multiplied by the delta value. This is pretty standard stuff, but it might be what is causing problems with my calculations.
Linear acceleration a
for a distance d
going from a starting speed Vi
to a final speed Vf
:
a = (Vf*Vf - Vi*Vi)/(2 * d)
EDIT:
After your edit, let me try and gauge what you need...
If you take this formula and insert your numbers, you get a constant acceleration of -0,0309375. Now, let's keep calling this result 'a'.
What you need between timestamps (frames?) is not actually the acceleration, but new location of the vehicle, right? So you use the following formula:
Sd = Vi * t + 0.5 * t * t * a
where Sd is the current distance from the start position at current frame/moment/sum_of_deltas, Vi is the starting speed, and t is the time since the start.
With this, your decceleration is constant, but even if it is linear, your speed will accomodate to your constraints.
If you want a non-linear decceleration, you could find some non-linear interpolation method, and interpolate not acceleration, but simply position between two points.
location = non_linear_function(time);