# How do I make a vehicle move to a point and stop without overshooting or oscillating?

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.

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

• 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) – tenpn Aug 20 '10 at 11:18
• 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. – Kylotan Aug 23 '10 at 14:56

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

• 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. – tenpn Aug 20 '10 at 15:23

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?

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?

• 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. – Iain Aug 20 '10 at 18:37
• 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. – ADB Aug 21 '10 at 11:27