Skip to content

Commit

Permalink
Fix Vector3 Slerp Normalizing Zero Vectors
Browse files Browse the repository at this point in the history
Ported the existing zero length check in C++ into C#.
  • Loading branch information
Z0rb14n committed Jul 26, 2024
1 parent e343dbb commit 0d6e9de
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,18 @@ public readonly Vector3 Slerp(Vector3 to, real_t weight)
// 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>
Expand Down

0 comments on commit 0d6e9de

Please sign in to comment.