Add MoveTowards to vector3

This commit is contained in:
Wojtek Figat
2023-08-31 09:23:16 +02:00
parent b6c8a08b58
commit 540681e59d
4 changed files with 68 additions and 0 deletions

View File

@@ -1054,6 +1054,23 @@ namespace FlaxEngine
return result;
}
/// <summary>
/// Moves a value current towards target.
/// </summary>
/// <param name="current">The position to move from.</param>
/// <param name="target">The position to move towards.</param>
/// <param name="maxDistanceDelta">The maximum distance that can be applied to the value.</param>
/// <returns>The new position.</returns>
public static Double3 MoveTowards(Double3 current, Double3 target, double maxDistanceDelta)
{
var to = target - current;
var distanceSq = to.LengthSquared;
if (distanceSq == 0 || (maxDistanceDelta >= 0 && distanceSq <= maxDistanceDelta * maxDistanceDelta))
return target;
var scale = maxDistanceDelta / Mathd.Sqrt(distanceSq);
return new Double3(current.X + to.X * scale, current.Y + to.Y * scale, current.Z + to.Z * scale);
}
/// <summary>
/// Performs a Hermite spline interpolation.
/// </summary>