views:

475

answers:

3

Hello,

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  A: 

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).

Paul R
Bodyscanner
+2  A: 

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

alt text

duffymo
Thanks a lot Duffymo, from what I understand the velocity value is in the middle of the right hand side, when I want it to be alone on the left?To plug in my values - going from X pos 0 to 100 in 1 second, decelerating at 25%:100 = 0 + v * 1 - 0.5 * -25 * (1*1)Apologies if I'm completely off track
Bodyscanner
+5  A: 

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;
}
fiirhok
Thanks, I created a simple HTML/jQuery demo:http://pastebin.com/7R01Jh1QIt seems to stop short of the required distance - am I doing something wrong?
Bodyscanner
Each iteration of your loop represents a period of time. You're calculating the position as if the velocity is constant over the whole period of time, but my calculation in the answer assumes the velocity is continuously changing. You can correct for it by using the average velocity over the time period to adjust the position.There also seems to be some error caused by retrieving an integer value from the position, instead of tracking it as a float.I've submitted a correction to pastebin.com/7R01Jh1Q that seems to work. I'll update the answer with more details later when I have time.
fiirhok
Thanks Fiirhok that is amazing! And very close to what I wanted - I couldn't see your pastebin so here's another demo with your averaging + corrections: http://pastebin.com/ekDwCYvj . Is there any way to do this with the acceleration value as a fraction/percentage like in my first code snippet? It's handy because it solves the problem of the simulation overshooting the target (can be stopped at a certain number of decimal places). If it's too complicated I will accept your solution though.
Bodyscanner
I probably messed up the pastebin, I've tried again at http://pastebin.com/S47AzRdN. The technique I used won't work if your acceleration is a function of velocity. You could try to solve the differential equation, but I don't think it would work. It seems like what you want is basically an implementation of Zeno's Paradox: http://en.wikipedia.org/wiki/Zeno's_paradoxes
fiirhok
Yes accelerating in this way will result in the object never reaching its destination, I'd found it slightly smoother to animate because it won't need to snap to the final point if it overshoots on a frame. But I've happily acccepted your solution - I am so grateful!
Bodyscanner