12
\$\begingroup\$

Many years ago I coded some AI for a budget pseudo-3d game. There was one calculation which I never really figured out the best way to do, and that was calculating where the enemy would end up if it stopped now. For example:

  • The enemy is currently at X = 540.0.
  • The enemy is moving right at 10 pixels per frame.
  • When the enemy wants to stop, it's speed will drop by 1 pixel per frame until it reaches zero.

Is there a simple formula that would get me the position where the enemy ends up when he is fully stopped? I ended up precalculating and hardcoding the offset which worked for my needs but would have to be calculated separately for enemies with different speeds.

\$\endgroup\$

4 Answers 4

7
\$\begingroup\$

I generally agree with John's answer. I'm going to offer a slightly modified formula (which adds an extra V/2 onto his value):

D = V / A * (V + A) / 2 

With V = 10 and A = 1, that gets D = 55. This is exactly the result of

10 + 9 + 8 + 7 + .... + 3 + 2 + 1

which is the frame-by-frame motion of the enemy.

Here's kind of how you go about getting to that step.

  1. V: Current Velocity = 10 pixels/frame, A: Current Acceleration = 1 pixels/frame^2
  2. T: Time To Come to a Stop = V/A = 10 frames.
  3. Distance Traveled in 10 frames = frame1 + frame2 + frame3 + ... = V +(V-A)+(V-2*A) + ...
  4. This is equal to T * V - A/2 * T * (T-1), which simplifies to the above equation.
\$\endgroup\$
3
  • \$\begingroup\$ +1 I can't see any logic flaws in your solution, and I really can't remember how I came across my solution. So my expression must be wrong and I probably didn't notice due to the small difference and lack of detailed testing? Would my expression become correct if I simply added half of the velocity? \$\endgroup\$ Oct 26, 2011 at 20:17
  • \$\begingroup\$ I answered my own question using power calc. Taking my expression + (v/2) appears to always equal your expression for any given v and a. \$\endgroup\$ Oct 26, 2011 at 20:25
  • \$\begingroup\$ Thanks to both @John and you, this would have saved me a lot of effort back in the days! I'm accepting this answer, since it's simpler and more efficient but John's answer is highly appreciated as well. Cheers guys! \$\endgroup\$ Oct 27, 2011 at 6:40
6
\$\begingroup\$

I had the exact same issue when working on my game, and it took me forever to get the math right (bleh). So here it is:

minDistanceToStop = 0.5 * acceleration * Math.Pow(velocityLinear() / acceleration, 2.0);

Re-written into regular math:

(Acceleration / 2) * (linearVelocity / Acceleration)^2

Where acceleration in your case is 1, and linearVelocity is 10:

(1 / 2) * (10 / 1)^2
= 50 units to stop

EDIT

Jimmy's result and explanation are both correct. My formula requires that you also add half of the velocity.

minDistanceToStop = (0.5 * acceleration * Math.Pow(velocityLinear() / acceleration, 2.0)) + (velocityLinear() / 2);

or

((Acceleration / 2) * (linearVelocity / Acceleration)^2) + (linearVelocity / 2)
((1 / 2) * (10 / 1)^2) + (10 / 2)
= 55
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Just for the record, Math.Pow() is a terrible, terrible idea here. It may special-case the '2.0' exponent if it's smart enough, but any way you slice it, rewriting that expression as '0.5*linearVelocity*linearVelocity/Acceleration' should be a huge win. \$\endgroup\$ Oct 26, 2011 at 23:26
4
\$\begingroup\$

Calculations about changing velocities is the entire point of calculus. I haven't done it in a while so I don't remember off the top of my head, but I think your situation is simply taking the integral of -1 (ie. the deceleration).

\$\endgroup\$
0
\$\begingroup\$

Isn't this constant acceleration motion?

X = Xi + V*t + (1/2) * a * (t^2)

Where:
X: Last position
Xi: Initial position
V: Velocity
t: Time
a: Acceleration

The only tricky part here is how to determine "t", since we slow down with an acceleration of -1, then we can calculate t = V/a , then t is 10.

so,
Xi: 540
V: 10
t: 10
a: -1

Put everything on:

X = 540 + 10*10 + (1/2) * (-1) * (10 ^ 2) 
X = 540 + 100 + (-50) 
X = 540 + 50 
X = 590

The formula comes from by taking integration of acceleration: Check here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .