9
\$\begingroup\$

Ok so consider we have a vehicle like the one I described in my answer to this question, with a 2D position, velocity, angular velocity, and drag/friction. It can accelerate and break by a variable amount, but not stop dead instantly or reach top speed instantly. How can I make it move to a particular point and stop - in the fastest time possible - without overshooting or oscillating?

I was thinking something like:

  • turn towards target position.
  • accelerate
  • look 1 tick ahead, asking "If I applied the breaks next tick, would I overshoot the target?"
  • If it would overshoot, apply the breaks now by some amount.

I'm sure this is a solved problem and there is a probably a better method than the one I described.

\$\endgroup\$

4 Answers 4

4
\$\begingroup\$

Using calculus, if you know your velocity and maximum deceleration rate then you can know your minimum stopping distance. With that in mind, you can know exactly when you need to apply the brakes (rounding/quantisation errors aside) to stop at a certain point.

Now, hopefully someone better at mathematics than me will post the actual formula...

\$\endgroup\$
2
  • 2
    \$\begingroup\$ finalPositionDelta = -0.5 * sqr(currentSpeed) / maximumDeceleration However this assumes that your deceleration is a constant, which depending on the depth of your simulation may not be the case. I'd go with Ranieri's solution. (whoops sorry it's DIV deceleration, not mul) \$\endgroup\$
    – tenpn
    Aug 20, 2010 at 11:18
  • \$\begingroup\$ Yeah, this method is still useful even if you frame it as a control systems problem, because understanding the gradients involved should help avoid instability or oscillation in the error measurement. \$\endgroup\$
    – Kylotan
    Aug 23, 2010 at 14:56
4
\$\begingroup\$

Steering Behaviors For Autonomous Characters. Check out seek and arrival.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Great for boids or space ships, but to apply them to vehicle physics you have to interpret the resulting vector, and you're back to the original problem again. \$\endgroup\$
    – tenpn
    Aug 20, 2010 at 15:23
3
\$\begingroup\$

It's a control problem. You need to vary your inputs proportionally to some kind of "error" measure, like the distance to the target. See also the answers to this question.

Target Tracking: When to accelerate and decelerate a rotating turret?

\$\endgroup\$
1
\$\begingroup\$

Sometimes, in game development, you need to take shortcuts. The shortcut here is called an Impulse, which, physically speaking is a very large force applied in a very short time. Instead of integrating it over time, you just apply it to the parameter as a direct change: set velocity to 0.

The question is: do you want to use the shortcut or not?

\$\endgroup\$
2
  • 2
    \$\begingroup\$ I'm not worried about taking shortcuts if it's invisible to the player, but if a vehicle could stop dead on the spot it would look very strange. \$\endgroup\$
    – Iain
    Aug 20, 2010 at 18:37
  • 1
    \$\begingroup\$ That is certainly true, however it helps to stop precisely at the point you want it to after a decelaration, thus avoiding complex calculation and having the divide your time step too many times. \$\endgroup\$
    – ADB
    Aug 21, 2010 at 11:27

You must log in to answer this question.

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