开发者

Figure out acceleration to hit a target position with 0 velocity?

开发者 https://www.devze.com 2022-12-20 14:10 出处:网络
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:

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)


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消