
Since we are only applying forces to the ship without imposing its position, controlling the ship’s speed is necessary. We can limit the ship’s speed to not exceed an amount called maxSpeed.
Lerp Function
The Lerp function is a linear interpolation function with a control variable t and a range of values [a, b], which has the following form:
ret = Lerp(a, b, t)
Where:
t lies in the interval [0, 1]
When t is 0, ret is equal to a
When t is 1, ret is equal to b
In general, ret = a + (b – a) * t
InverseLerp Function
The InverseLerp function is the inverse version of the Lerp function, which returns a value in the range [0, 1]. It has the form:
ret = InverseLerp(left, right, value)
When value is left, ret is 0
When value is right, ret is 1
In general, ret = (value – left) / (right – left)
First, we use the InverseLerp function to calculate the speedFactor, which represents how fast the ship moves under the influence of forces in the physical environment compared to the desired speed limit, it has a value in the range [0, 1]
speedFactor = InverseLerp(0, maxSpeed, Velocity)
When Velocity is 0, speedFactor is 0
When Velocity is maxSpeed, speedFactor is 1
We get the desired thrust force as
thrust = Lerp(maxSpeed, 0, speedFactor) * k
Where k is a coefficient in the program.
When the ship is stationary, maxSpeed is 0, thrust force is the largest.
When the ship speed is close to maxSpeed, speedFactor is close to 1 and thrust force is close to 0, ensuring that the ship speed never exceeds maxSpeed.