views:

146

answers:

3

I'm trying to get my simulation to stop on a specific point. I have my starting position, an ending position, my current velocity and the time I'd like to take to get there. Since:

d = vt + (at^2)/2

I was figuring that

d = (end - start)
a = 2(d - vt) / t^2

but my end point is way off when I run it. I've tried using two simple updates, first:

v += a * dt
d += v * dt

and second:

d += v * dt + a * dt * dt / 2;
v += a * dt;

if that matters. Position in this case is 1d, so no need for crazy vector stuff. Any help would be greatly appreciated :) Thanks!

(Edit: formatting) (Edit2: corrected update #2) (Edit3: updates now show dt instead of t)

+2  A: 

We start at x_start (and t=0), with speed v_start, and we want to end at x_end, with velocity zero.

Since we have a constant acceleration, the average speed will be v_start/2, which means we'll reach x_end at t_end = (x_end - x_start) / (v_start / 2).

Okay, so then we can use x(t) = x_start + v_start * t + at^2/2. As a sanity check, plug in t=0 and make sure you get x = x_start.

Then plug in t_end and you can solve for a. I get -v^2/(2D) where D is x_end - x_start. The negative sign just means you're slowing down instead of speeding up.

If you plug this into the original function you get:

x(t) = x_start + v_start * t - (v^2/4D) * t^2

If you have a counter that tells you the value of t (which will vary between 0 and t_end), you can simply move the object to the correct position at each moment.

Or (and this might make more sense, depending on the language, environment, etc.), you can calculate the instantaneous velocity each timestep by, and then the instantaneous position, following CoderTao's answer.

MatrixFrog
So t_end is completely dependent on v_start? What if I want this deceleration to take more/less time?
Sean
The way you've stated it, I assume you want a constant acceleration. In that case, there is only one solution, yes.
MatrixFrog
That is exactly what I was doing wrong. I was trying to plug in my own duration.
Sean
A: 

One possible problem; which isn't clear from what you've written so far, is that the equations:

v+=a*t
d+=v*t
...
d+=v*t + a*t*t/2
v+=a*t

Should be:

v+=a*dt
d+=v*dt
...
d+=v*dt + a*dt*dt/2
v+=a*dt

Where dt is the difference in time since the last update. It would be useful to have a bit more information- the surrounding code, example inputs/outputs, etc.

CoderTao
Not quite. `a*dt*dt/2` doesn't make much sense. Once you know the instantaneous velocity, you just want `d+=v*dt`
MatrixFrog
If so, the error is mine, not his. I was trying an update that does position before velocity.
Sean
Which one you do first is not so important. (Doing velocity first *feels* more correct to me but I don't think it matters much.) An equation for x which depends on t^2 is perfectly valid and correct. But adding (dt)^2 directly to your position doesn't make sense.
MatrixFrog
@MatrixFrog the a*dt^2/2 comes from the equation x=x0+v*t+a*t^2/2 with t=dt; it's basically accounting for the fact that the velocity is changing continuously over the timestep; which is also part of why velocity is incremented after the distance update in the second form of the equations. Without it, you would expect to accrue a small amount of error over the simulation, probably ending with an x that falls too short from the target if using a positive acceleration.
CoderTao
Okay, I see now. Personally, I would prefer to leave that out, and if there are accuracy issues, try to solve them by shrinking dt. Whether that is possible/feasible depends on the language being used, etc. But that's just me, anyway.
MatrixFrog
A: 

Why are you changing your physics formula? Just change the acceleration so the object stops where you want.

The required acceleration is what you have given:

a = 2(d - vt) / t^2

Just assign a in your code when you want to begin stopping it.

strager
The question is less "what's wrong with the formula" and more "what am I doing wrong with it"
Sean
@Sean, Oh, so you are having problem with the "fundamental" engine?
strager