views:

1822

answers:

6

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.

+5  A: 

The four constraints you give are one too many for a linear system (one with constant acceleration), where any three of the variables would suffice to compute the acceleration and thereby determine the fourth variables. However, the system is way under-specified for a completely general nonlinear system -- there may be uncountably infinite ways to change acceleration over time while satisfying all the constraints as given. Can you perhaps specify better along what kind of curve acceleration should change over time?

Using 0 index to mean "at the start", 1 to mean "at the end", and D for Delta to mean "variation", given a linearly changing acceleration

  a(t) = a0 + t * (a1-a0)/Dt

where a0 and a1 are the two parameters we want to compute to satisfy all the various constraints, I compute (if there's been no misstep, as I did it all by hand):

DV = Dt * (a0+a1)/2
Ds = Dt * (V0 + ((a1-a0)/6 + a0/2) * Dt)

Given DV, Dt and Ds are all given, this leaves 2 linear equations in the unknowns a0 and a1 so you can solve for these (but I'm leaving things in this form to make it easier to double check on my derivations!!!).

If you're applying the proper formulas at every step to compute changes in space and velocity, it should make no difference whether you compute a0 and a1 once and for all or recompute them at every step based on the remaining Dt, Ds and DV.

Alex Martelli
You're right - I probably should be using a linear system. As long as it looks natural, I don't care much about the why or the how, so keeping it simpler will be better. When you propose it as a curve and I really think about it, needing a curve is unlikely in any case. I can't think of an exponential graph that would match what I'm looking for, but I can think of several linear ones. If I use a linear deceleration, do I need to calculate it only once (when t==0) or will it still work if I recalculate every time step?
Eli
editing my answer to respond to these further questions in a well formatted way
Alex Martelli
A: 

Problem is either overconstrained or underconstrained (a is not constant? is there a maximum a?) or ambiguous.

Simplest formula would be a=(Vf-V0)/t

Edit: if time is not constrained, and distance s is constrained, and acceleration is constant, then the relevant formulae are s = (Vf+V0)/2 * t, t=(Vf-V0)/a which simplifies to a = (Vf2 - V02) / (2s).

Jason S
ooooookay. Please tell us the constraints that would help to determine acceleration, if not constant.
Jason S
F(t) = ma(t), where force and accelerations are both vector functions of time, of course.
duffymo
OP gave constraints on V0, Vf, d, t. If a is constant and V0, Vf, and t are independent variables, then a = (Vf-V0)/t. I don't care about the 2 point hit, but I do care about being downvoted w/o comment for making an honest effort towards an answer. Otherwise why spend the time trying to give answers on this site?
Jason S
+3  A: 

If you're trying to simulate a time-dependent acceleration in your equations, it just means that you should assume that. You have to integrate F = ma along with the acceleration equations, that's all. If acceleration isn't constant, you just have to solve a system of equations instead of just one.

So now it's really three vector equations that you have to integrate simultaneously: one for each component of displacement, velocity, and acceleration, or nine equations in total. The force as a function of time will be an input for your problem.

If you're assuming 1D motion you're down to three simultaneous equations. The ones for velocity and displacement are both pretty easy.

duffymo
Hey duffymo, thanks for the response. I was indeed referring to the way people typically drive - start with your foot on the brake slightly when you are far from the stop, then press it hard when you are about to reach it, causing a small "lurch" typically as the momentum of the car carries its weight forward. Force and mass are unimportant (I think) to my problem; I only care about acceleration as the change of velocity over time, m/s/s. You can assume every car has a mass of 1, so that the force is essentially just the velocity.
Eli
"Force and mass are unimportant (I think) to my problem; I only care about acceleration as the change of velocity over time, m/s/s. You can assume every car has a mass of 1, so that the force is essentially just the velocity." - I disagree. You might be arguing that you want to take an empirical approach that isn't 100% faithful to the physics, but let's acknowledge it for what it is. Acceleration is ONLY one thing - the rate of change of velocity with respect to time, a first order tensor. There is no other definition.
duffymo
+1  A: 

This is just a guess, but couldn't you use one of those "typical physics equations" that deals with constant acceleration and use a negative number to get deceleration?

Dennis Palmer
Nope, already doing that `velocity -= decelValueFound;`
Eli
+1  A: 

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);
kek444
you're off by a factor of 2
Jason S
Really, what a horrible typo. Thanks.
kek444
Formula, as well as few typos fixed, additional idea added.
kek444
+1  A: 

In real life, a car's stopping ability depends on the pressure on the brake pedal, any engine braking that's going on, surface conditions, and such: also, there's that "grab" at the end when the car really stops. Modeling that is complicated, and you're unlikely to find good answers on a programming website. Find some automotive engineers.

Aside from that, I don't know what you're asking for. Are you trying to determine a braking schedule? As in there's a certain amount of deceleration while coasting, and then applying the brake? In real driving, the time is not usually considered in these maneuvers, but rather the distance.

As far as I can tell, your problem is that you aren't asking for anything specific, which suggests that you really haven't figured out what you actually want. If you'd provide a sample use for this, we could probably help you. As it is, you've provided the bare bones of a problem that is either overdetermined or way underconstrained, and there's really nothing we can do with that.

David Thornley
There are lots of levels of faithfulness that you can embed into a model. If a 1D constant acceleration is one extreme in the spectrum, yours seems a bit too far in the other direction (although not even close to the most complex). I think the non-physics, empirical approach is what s/he's looking for here.
duffymo