The simpler approach, but still somewhat smooth
By approximation, we could create such a braking curve and this would work for a kind of "smooth" braking:

$$
f(t) =
\begin{cases}
2t-2, &t \ge \frac{1}{2} \\
-2t, &t < \frac{1}{2}
\end{cases}
$$
The disadvantage here is that at the kinks \$(0, 0.5, 1)\$ there will be some noticeable not-"smooth"-ness and braking will not be really "smooth"... but it will be almost perfect.
This would be the acceleration with the velocity plotted:

The area of the acceleration for \$t \in [0,1]\$ is easily calculated with the following expression:
$$
v(t) = \frac{cos(\pi t)}{2} - \frac{1}{2}
$$
But for the purposes of calculating the maximum deceleration, we know that the "area" (velocity \$v\$) here is equal to \$-1\$ (this function will "remove" this much velocity).
This means that we can just input the velocity as the amplitude when we brake in stop time \$t_{stop}\$.
When the physics engine works by simulating acceleration/deceleration (probably?)
$$
f(t, t_{stop}, v_0) =
\begin{cases}
k \cdot v_0 \cdot \left(2 \frac{t}{t_{stop}}-2 \right), &\frac{t}{t_{stop}} \ge \frac{1}{2} \\
k \cdot v_0 \cdot \left(-2 \frac{t}{t_{stop}} \right), &\frac{t}{t_{stop}} < \frac{1}{2}
\end{cases}
$$
where \$v_0\$ is the initial velocity and \$k\$ is some factor.
When the physics engine works by simulating the velocity, not the change in speed
$$
v(t, t_{stop}, v_0) = v_0 + v_0 \cdot \left(\frac{cos \left(\pi \frac{t}{t_{stop}} \right)}{2} - \frac{1}{2} \right)
$$
But this will result in jerk, as the "kink" in the graph means the derivative is discontinuous:

The difficult approach, but smooth
If we want smooth braking, I would advise such an acceleration/deceleration curve:

It's not important to know, but this curve consists of three parts (left, middle, right):
$$
f(t) =
\begin{cases}
\frac{-cos(\pi t)}{\pi} + \frac{1}{\pi}, &t \in [0,1) \qquad(1) \\
\frac{-2cos(\pi t)}{\pi}, &t \in [1,2) \qquad(2) \\
\frac{-cos\pi}{\pi} - \frac{1}{\pi}, &t \in [2,3) \qquad(3) \\
\end{cases}
$$

The identified requirements from these three parts start with the first and last parts and consist of:
- The requirement is a smooth jerkless start.
- Any kinks in this acceleration/deceleration pattern would introduce jerk.
- The function must keep decreasing the slope when nearing the zero points.
The middle part is there to just connect the motion into one whole jerkless movement. This would require another chapter in this answer (brake from positive acceleration). For now, I will focus on braking (decelerating) when the acceleration is zero already.
To make this go smoothly, the wanted deceleration curve would approximate this:
It's not important, but the formula used here is
$$f(t) = \frac{-cos(\pi (2t-1))}{\pi} - \frac{1}{\pi}$$
as a basis, to normalize the braking from \$0\$ to \$1\$.

The information we have is:
- The distance \$d\$.
- The current speed \$v\$.
And the question that we have is: what are the parameters (time, amplitude) of the deceleration function, so that the interpolation of that function, equals the speed?
To make this question easier, let's assume that the deceleration curve is calculated, and saved, at one point in time, and is not recalculated.
To make this even easier, let's assign one variable already: time (in how much time we want to stop, stop_time).
Let us just calculate the time from:
$$t_{stop} = \frac{d}{v}$$
We already normalized the brake function for \$t \in [0,1]\$, so we need to divide \$t\$ by the stop time \$t_{stop}\$ to get the curve that fits within the wanted stop time:
$$
f(t, t_{stop}) = \frac{-cos\left(\pi \left(2\left(\frac{t}{stop\_time}\right)-1\right)\right)}{\pi}-\frac{1}{\pi}
$$
A car that is going faster obviously needs a higher braking force to stop the same distance (i.e. remove "more" velocity).
To calculate the velocity this function would "remove" we take the integral (from Wolfram Alpha):
$$
\int f(t, t_{stop}) =
-\frac{2\left(\frac{t}{2}-\frac{t_{stop} \cdot sin\left(\frac{2 \pi t}{t_{stop}}\right)}{4 \pi}\right)}{\pi}
$$
Now, how much velocity does this function remove in total for \$\frac{t}{t_{stop}} = 1\$?

We can see it's around \$\frac{1}{3}\$. Solving it numerically yields \$-\frac{1}{\pi}\$
That's how much velocity this deceleration function is able to remove, so this is our factor for the initial velocity.
For an engine that is velocity-based (already simplified)
$$
v(t, t_{stop}, v_0) =
v_0 + v_0 \cdot \left(-2\left(\frac{t}{2}-\frac{t_{stop} \cdot sin\left(\frac{2 \pi t}{t_{stop}}\right)}{4 \pi}\right)\right)
$$
Resulting in:

for a stop time \$t_{stop}\$ of 1.0
and initial velocity 10.0
.
In all formulas, \$t\$ would then be supplied as the time since braking started, and the result can be split into the corresponding x
, y
, and z
components according to the normalized speed vectors of your moving object.
x = 1/2 at^2 + Vt
Rearrange to solve fora
and you geta = 2(x-Vt) / (t^2)
– Mars Mar 08 '24 at 04:50