Calculate initial velocity to move a set distance

2020-02-26 13:05发布

问题:

I want to move something a set distance. However in my system there is inertia/drag/negative accelaration. I'm using a simple calculation like this for it:

v = oldV + ((targetV - oldV) * inertia)

Applying that over a number of frames makes the movement 'ramp up' or decay, eg:

v = 10 + ((0 - 10) * 0.25) = 7.5 // velocity changes from 10 to 7.5 this frame

So I know the distance I want to travel and the acceleration, but not the initial velocity that will get me there. Maybe a better explanation is I want to know how hard to hit a billiard ball so that it stops on a certain point.

I've been looking at Equations of motion (http://en.wikipedia.org/wiki/Equations_of_motion) but can't work out what the correct one for my problem is...

Any ideas? Thanks - I am from a design not science background.

Update: Fiirhok has a solution with a fixed acceleration value; HTML+jQuery demo:
http://pastebin.com/ekDwCYvj
Is there any way to do this with a fractional value or an easing function? The benefit of that in my experience is that fixed acceleration and frame based animation sometimes overshoots the final point and needs to be forced, creating a slight snapping glitch.

回答1:

This is a simple kinematics problem.

At some time t, the velocity (v) of an object under constant acceleration is described by:

v = v0 + at

Where v0 is the initial velocity and a is the acceleration. In your case, the final velocity is zero (the object is stopped) so we can solve for t:

t = -v0/a

To find the total difference traveled, we take the integral of the velocity (the first equation) over time. I haven't done an integral in years, but I'm pretty sure this one works out to:

d = v0t + 1/2 * at^2

We can substitute in the equation for t we developed ealier:

d = v0^2/a + 1/2 * v0^2 / a

And the solve for v0:

v0 = sqrt(-2ad)

Or, in a more programming-language format:

initialVelocity = sqrt( -2 * acceleration * distance );

The acceleration in this case is negative (the object is slowing down), and I'm assuming that it's constant, otherwise this gets more complicated.

If you want to use this inside a loop with a finite number of steps, you'll need to be a little careful. Each iteration of the loop represents a period of time. The object will move an amount equal to the average velocity times the length of time. A sample loop with the length of time of an iteration equal to 1 would look something like this:

position = 0;
currentVelocity = initialVelocity;
while( currentVelocity > 0 )
{
    averageVelocity = currentVelocity + (acceleration / 2);
    position = position + averageVelocity;
    currentVelocity += acceleration;
}


回答2:

If you want to move a set distance, use the following:



回答3:

Distance travelled is just the integral of velocity with respect to time. You need to integrate your expression with respect to time with limits [v, 0] and this will give you an expression for distance in terms of v (initial velocity).