diff --git a/Source/Engine/Core/Math/Math.h b/Source/Engine/Core/Math/Math.h index 0c9a07841..276f64884 100644 --- a/Source/Engine/Core/Math/Math.h +++ b/Source/Engine/Core/Math/Math.h @@ -393,7 +393,7 @@ namespace Math /// The value to move towards. /// The maximum change that should be applied to the value. template - float MoveTowards(const T current, const T target, const T maxDelta) + static T MoveTowards(const T current, const T target, const T maxDelta) { if (Abs(target - current) <= maxDelta) return target; @@ -407,13 +407,38 @@ namespace Math /// /// template - float MoveTowardsAngle(const T current, const T target, const T maxDelta) + static T MoveTowardsAngle(const T current, const T target, const T maxDelta) { - float delta = DeltaAngle(current, target); + T delta = DeltaAngle(current, target); if ((-maxDelta < delta) && (delta < maxDelta)) return target; - target = current + delta; - return MoveTowards(current, target, maxDelta); + T deltaTarget = current + delta; + return MoveTowards(current, deltaTarget, maxDelta); + } + + /// + /// Calculates the shortest difference between two given angles given in degrees. + /// + /// + /// + template + static T DeltaAngle(const T current, const T target) + { + T t = Repeat(target - current, (T)360); + if (t > (T)180) + t -= (T)360; + return t; + } + + /// + /// Loops the value t, so that it is never larger than length and never smaller than 0. + /// + /// + /// + template + static T Repeat(const T t, const T length) + { + return t - Floor(t / length) * length; } // Multiply value by itself