From 540681e59d5e51e85f1af0e462fa85da44a2a36a Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 31 Aug 2023 09:23:16 +0200 Subject: [PATCH] Add `MoveTowards` to vector3 --- Source/Engine/Core/Math/Double3.cs | 17 +++++++++++++++++ Source/Engine/Core/Math/Float3.cs | 17 +++++++++++++++++ Source/Engine/Core/Math/Vector3.cs | 17 +++++++++++++++++ Source/Engine/Core/Math/Vector3.h | 17 +++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/Source/Engine/Core/Math/Double3.cs b/Source/Engine/Core/Math/Double3.cs index eb41ad400..ca5eb9cbd 100644 --- a/Source/Engine/Core/Math/Double3.cs +++ b/Source/Engine/Core/Math/Double3.cs @@ -1054,6 +1054,23 @@ namespace FlaxEngine return result; } + /// + /// Moves a value current towards target. + /// + /// The position to move from. + /// The position to move towards. + /// The maximum distance that can be applied to the value. + /// The new position. + 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); + } + /// /// Performs a Hermite spline interpolation. /// diff --git a/Source/Engine/Core/Math/Float3.cs b/Source/Engine/Core/Math/Float3.cs index 1831a3b99..3ea95f0d7 100644 --- a/Source/Engine/Core/Math/Float3.cs +++ b/Source/Engine/Core/Math/Float3.cs @@ -1038,6 +1038,23 @@ namespace FlaxEngine return result; } + /// + /// Moves a value current towards target. + /// + /// The position to move from. + /// The position to move towards. + /// The maximum distance that can be applied to the value. + /// The new position. + public static Float3 MoveTowards(Float3 current, Float3 target, float maxDistanceDelta) + { + var to = target - current; + var distanceSq = to.LengthSquared; + if (distanceSq == 0 || (maxDistanceDelta >= 0 && distanceSq <= maxDistanceDelta * maxDistanceDelta)) + return target; + var scale = maxDistanceDelta / Mathf.Sqrt(distanceSq); + return new Float3(current.X + to.X * scale, current.Y + to.Y * scale, current.Z + to.Z * scale); + } + /// /// Performs a Hermite spline interpolation. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 4442d3334..72f538269 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1068,6 +1068,23 @@ namespace FlaxEngine return result; } + /// + /// Moves a value current towards target. + /// + /// The position to move from. + /// The position to move towards. + /// The maximum distance that can be applied to the value. + /// The new position. + public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta) + { + var to = target - current; + var distanceSq = to.LengthSquared; + if (distanceSq == 0 || (maxDistanceDelta >= 0 && distanceSq <= maxDistanceDelta * maxDistanceDelta)) + return target; + var scale = maxDistanceDelta / Mathr.Sqrt(distanceSq); + return new Vector3(current.X + to.X * scale, current.Y + to.Y * scale, current.Z + to.Z * scale); + } + /// /// Performs a Hermite spline interpolation. /// diff --git a/Source/Engine/Core/Math/Vector3.h b/Source/Engine/Core/Math/Vector3.h index cad5250b7..c0ebdf01d 100644 --- a/Source/Engine/Core/Math/Vector3.h +++ b/Source/Engine/Core/Math/Vector3.h @@ -727,6 +727,23 @@ public: Lerp(start, end, amount, result); } + /// + /// Moves a value current towards target. + /// + /// The position to move from. + /// The position to move towards. + /// The maximum distance that can be applied to the value. + /// The new position. + static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta) + { + const Vector3Base to = target - current; + const T distanceSq = to.LengthSquared(); + if (distanceSq == 0 || (maxDistanceDelta >= 0 && distanceSq <= maxDistanceDelta * maxDistanceDelta)) + return target; + const T scale = maxDistanceDelta / Math::Sqrt(distanceSq); + return Vector3Base(current.X + to.X * scale, current.Y + to.Y * scale, current.Z + to.Z * scale); + } + // Performs a Hermite spline interpolation. // @param value1 First source position vector // @param tangent1 First source tangent vector