From d2f731471621ddd153b4943a9a885c2dff084e4c Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Thu, 24 Nov 2022 17:51:34 +0100 Subject: C#: Implement BezierDerivative Adds `BezierDerivative` method to Mathf, Vector2 and Vector3 (already exposed in Core). --- .../mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs | 23 +++++++++++++++++++++- .../glue/GodotSharp/GodotSharp/Core/Vector2.cs | 19 +++++++++++++++++- .../glue/GodotSharp/GodotSharp/Core/Vector3.cs | 20 ++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs index f2667c6807..3f9e986f62 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Mathf.cs @@ -282,7 +282,7 @@ namespace Godot /// /// Returns the point at the given on a one-dimensional Bezier curve defined by - /// the given , and points. + /// the given , , and points. /// /// The start value for the interpolation. /// Control point that defines the bezier curve. @@ -302,6 +302,27 @@ namespace Godot return start * omt3 + control1 * omt2 * t * 3 + control2 * omt * t2 * 3 + end * t3; } + /// + /// Returns the derivative at the given on a one dimensional Bezier curve defined by + /// the given , , and points. + /// + /// The start value for the interpolation. + /// Control point that defines the bezier curve. + /// Control point that defines the bezier curve. + /// The destination value for the 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 BezierDerivative(real_t start, real_t control1, real_t control2, real_t end, real_t t) + { + // Formula from Wikipedia article on Bezier curves + real_t omt = 1 - t; + real_t omt2 = omt * omt; + real_t t2 = t * t; + + real_t d = (control1 - start) * 3 * omt2 + (control2 - control1) * 6 * omt * t + (end - control2) * 3 * t2; + return d; + } + /// /// Converts an angle expressed in degrees to radians. /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs index 535391f447..b11778194a 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs @@ -240,7 +240,7 @@ namespace Godot /// /// Returns the point at the given on a one-dimensional Bezier curve defined by this vector - /// and the given , and points. + /// and the given , , and points. /// /// Control point that defines the bezier curve. /// Control point that defines the bezier curve. @@ -259,6 +259,23 @@ namespace Godot return this * omt3 + control1 * omt2 * t * 3 + control2 * omt * t2 * 3 + end * t3; } + /// + /// Returns the derivative at the given on the Bezier curve defined by this vector + /// and the given , , and points. + /// + /// Control point that defines the bezier curve. + /// Control point that defines the bezier curve. + /// The destination value for the interpolation. + /// A value on the range of 0.0 to 1.0, representing the amount of interpolation. + /// The resulting value of the interpolation. + public readonly Vector2 BezierDerivative(Vector2 control1, Vector2 control2, Vector2 end, real_t t) + { + return new Vector2( + Mathf.BezierDerivative(x, control1.x, control2.x, end.x, t), + Mathf.BezierDerivative(y, control1.y, control2.y, end.y, t) + ); + } + /// /// Returns the normalized vector pointing from this vector to . /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs index 53bd0b0908..7e31822d50 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs @@ -234,7 +234,7 @@ namespace Godot /// /// Returns the point at the given on a one-dimensional Bezier curve defined by this vector - /// and the given , and points. + /// and the given , , and points. /// /// Control point that defines the bezier curve. /// Control point that defines the bezier curve. @@ -253,6 +253,24 @@ namespace Godot return this * omt3 + control1 * omt2 * t * 3 + control2 * omt * t2 * 3 + end * t3; } + /// + /// Returns the derivative at the given on the Bezier curve defined by this vector + /// and the given , , and points. + /// + /// Control point that defines the bezier curve. + /// Control point that defines the bezier curve. + /// The destination value for the interpolation. + /// A value on the range of 0.0 to 1.0, representing the amount of interpolation. + /// The resulting value of the interpolation. + public readonly Vector3 BezierDerivative(Vector3 control1, Vector3 control2, Vector3 end, real_t t) + { + return new Vector3( + Mathf.BezierDerivative(x, control1.x, control2.x, end.x, t), + Mathf.BezierDerivative(y, control1.y, control2.y, end.y, t), + Mathf.BezierDerivative(z, control1.z, control2.z, end.y, t) + ); + } + /// /// Returns the normalized vector pointing from this vector to . /// -- cgit v1.2.3