diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2024-08-16 23:45:34 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-08-16 23:45:34 +0200 |
commit | 4bd33df11e28e619555c3ba7a8e8c49a9a6497d8 (patch) | |
tree | 70b99771adfeef8af76ad265f0c47ca23d781efc | |
parent | 0d0eb71694d3ea6bf6b5b171053891044046cbfc (diff) | |
parent | 0d6e9de0b904aa5acdfcc56607a3aded431c2f6c (diff) | |
download | redot-engine-4bd33df11e28e619555c3ba7a8e8c49a9a6497d8.tar.gz |
Merge pull request #94766 from Z0rb14n/fix-vector3-slerp
C#: Fix Vector3 `Slerp` normalization error
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs index 27f2713efa..ef66d5bbe0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs @@ -692,10 +692,18 @@ namespace Godot // Zero length vectors have no angle, so the best we can do is either lerp or throw an error. return Lerp(to, weight); } + Vector3 axis = Cross(to); + real_t axisLengthSquared = axis.LengthSquared(); + if (axisLengthSquared == 0.0) + { + // Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp. + return Lerp(to, weight); + } + axis /= Mathf.Sqrt(axisLengthSquared); real_t startLength = Mathf.Sqrt(startLengthSquared); real_t resultLength = Mathf.Lerp(startLength, Mathf.Sqrt(endLengthSquared), weight); real_t angle = AngleTo(to); - return Rotated(Cross(to).Normalized(), angle * weight) * (resultLength / startLength); + return Rotated(axis, angle * weight) * (resultLength / startLength); } /// <summary> |