C#: Add CubicInterpolateAngle

This commit is contained in:
Raul Santos 2022-08-27 10:28:00 +02:00
parent 623f4a52ca
commit f64b845514
No known key found for this signature in database
GPG key ID: B532473AE3A803E4

View file

@ -193,6 +193,33 @@ namespace Godot
(-pre + 3.0f * from - 3.0f * to + post) * (weight * weight * weight));
}
/// <summary>
/// Cubic interpolates between two rotation values with shortest path
/// by the factor defined in <paramref name="weight"/> with pre and post values.
/// See also <see cref="LerpAngle"/>.
/// </summary>
/// <param name="from">The start value for interpolation.</param>
/// <param name="to">The destination value for interpolation.</param>
/// <param name="pre">The value which before "from" value for interpolation.</param>
/// <param name="post">The value which after "to" value for interpolation.</param>
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
/// <returns>The resulting value of the interpolation.</returns>
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);
}
/// <summary>
/// Cubic interpolates between two values by the factor defined in <paramref name="weight"/>
/// with pre and post values.
@ -220,6 +247,39 @@ namespace Godot
return Lerp(b1, b2, toT == 0 ? 0.5f : t / toT);
}
/// <summary>
/// Cubic interpolates between two rotation values with shortest path
/// by the factor defined in <paramref name="weight"/> with pre and post values.
/// See also <see cref="LerpAngle"/>.
/// It can perform smoother interpolation than <see cref="CubicInterpolateAngle"/>
/// by the time values.
/// </summary>
/// <param name="from">The start value for interpolation.</param>
/// <param name="to">The destination value for interpolation.</param>
/// <param name="pre">The value which before "from" value for interpolation.</param>
/// <param name="post">The value which after "to" value for interpolation.</param>
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
/// <param name="toT"></param>
/// <param name="preT"></param>
/// <param name="postT"></param>
/// <returns>The resulting value of the interpolation.</returns>
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);
}
/// <summary>
/// Returns the point at the given <paramref name="t"/> on a one-dimensional Bezier curve defined by
/// the given <paramref name="control1"/>, <paramref name="control2"/> and <paramref name="end"/> points.