6

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.

Linus Juhlin
  • 1,175
  • 10
  • 31
MachineElf
  • 1,231
  • 2
  • 15
  • 28
  • `Compiler error: Line #4: Missing ')'.` – kennytm Mar 21 '10 at 12:38
  • 1
    This equation is not correct in any way. Where did you come up with this? – duffymo Mar 21 '10 at 12:39
  • This is more a topic for http://mathoverflow.net/ in my opinion. To fix the compiler error, remove one of the opening parenthesis. You have two, but close only one. – senfo Mar 21 '10 at 12:48
  • @senfo: mathoverflow is for research-level maths. – kennytm Mar 21 '10 at 12:49
  • 1
    I'm on my phone and just realized the compiler error was a comment by @KennyTM. StackOverflow needs better mobile browser support! ;-) – senfo Mar 21 '10 at 12:55
  • @senfo, this isn't a math question, in spite of the mathematical symbols. It's undergraduate physics and Newton's laws of motion that are confused here. – duffymo Mar 21 '10 at 13:06
  • Hi, sorry I missed a bracket, and I've made the value names clearer. This was just to give a rough idea of how I'm handling the inertia - by decaying the value over a period of frames. – MachineElf Mar 21 '10 at 15:19

3 Answers3

8

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
  • 386
  • 3
  • 10
  • Thanks, I created a simple HTML/jQuery demo: http://pastebin.com/7R01Jh1Q It seems to stop short of the required distance - am I doing something wrong? – MachineElf Mar 21 '10 at 20:40
  • 1
    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 Mar 21 '10 at 21:55
  • 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. – MachineElf Mar 22 '10 at 22:19
  • 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 Mar 23 '10 at 01:40
  • 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! – MachineElf Mar 23 '10 at 10:57
3

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

alt text

Community
  • 1
  • 1
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 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 – MachineElf Mar 21 '10 at 16:05
1

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
  • 208,748
  • 37
  • 389
  • 560
  • Thanks Paul & apologies - I don't have a maths background (should have mentioned that first, edited post), I was hoping for a computer style formula I could plug my values into. – MachineElf Mar 21 '10 at 16:13