From f64b84551423b02ce2f66092769f3ab3bfbb7740 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Sat, 27 Aug 2022 10:28:00 +0200 Subject: [PATCH] C#: Add `CubicInterpolateAngle` --- .../glue/GodotSharp/GodotSharp/Core/Mathf.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs index 124410a1c2d..b30012d2145 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs @@ -193,6 +193,33 @@ namespace Godot (-pre + 3.0f * from - 3.0f * to + post) * (weight * weight * weight)); } + /// + /// Cubic interpolates between two rotation values with shortest path + /// by the factor defined in with pre and post values. + /// See also . + /// + /// The start value for interpolation. + /// The destination value for interpolation. + /// The value which before "from" value for interpolation. + /// The value which after "to" value for interpolation. + /// A value on the range of 0.0 to 1.0, representing the amount of interpolation. + /// The resulting value of the interpolation. + public static real_t CubicInterpolateAngle(real_t from, real_t to, real_t pre, real_t post, real_t weight) + { + real_t fromRot = from % Mathf.Tau; + + real_t preDiff = (pre - fromRot) % Mathf.Tau; + real_t preRot = fromRot + (2.0f * preDiff) % Mathf.Tau - preDiff; + + real_t toDiff = (to - fromRot) % Mathf.Tau; + real_t toRot = fromRot + (2.0f * toDiff) % Mathf.Tau - toDiff; + + real_t postDiff = (post - toRot) % Mathf.Tau; + real_t postRot = toRot + (2.0f * postDiff) % Mathf.Tau - postDiff; + + return CubicInterpolate(fromRot, toRot, preRot, postRot, weight); + } + /// /// Cubic interpolates between two values by the factor defined in /// with pre and post values. @@ -220,6 +247,39 @@ namespace Godot return Lerp(b1, b2, toT == 0 ? 0.5f : t / toT); } + /// + /// Cubic interpolates between two rotation values with shortest path + /// by the factor defined in with pre and post values. + /// See also . + /// It can perform smoother interpolation than + /// by the time values. + /// + /// The start value for interpolation. + /// The destination value for interpolation. + /// The value which before "from" value for interpolation. + /// The value which after "to" value for interpolation. + /// A value on the range of 0.0 to 1.0, representing the amount of interpolation. + /// + /// + /// + /// The resulting value of the interpolation. + public static real_t CubicInterpolateAngleInTime(real_t from, real_t to, real_t pre, real_t post, real_t weight, + real_t toT, real_t preT, real_t postT) + { + real_t fromRot = from % Mathf.Tau; + + real_t preDiff = (pre - fromRot) % Mathf.Tau; + real_t preRot = fromRot + (2.0f * preDiff) % Mathf.Tau - preDiff; + + real_t toDiff = (to - fromRot) % Mathf.Tau; + real_t toRot = fromRot + (2.0f * toDiff) % Mathf.Tau - toDiff; + + real_t postDiff = (post - toRot) % Mathf.Tau; + real_t postRot = toRot + (2.0f * postDiff) % Mathf.Tau - postDiff; + + return CubicInterpolateInTime(fromRot, toRot, preRot, postRot, weight, toT, preT, postT); + } + /// /// Returns the point at the given on a one-dimensional Bezier curve defined by /// the given , and points.