Skip to content

Commit f672232

Browse files
committed
fix: Vector3d.ClampMagnitude
1 parent eaee29b commit f672232

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/FixedMathSharp/Numerics/Vector3d.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,17 @@ public static Vector3d Clamp(Vector3d value, Vector3d min, Vector3d max)
643643
);
644644
}
645645

646+
/// <summary>
647+
/// Clamps the given Vector3d within the specified magnitude.
648+
/// </summary>
649+
/// <param name="value"></param>
650+
/// <param name="maxMagnitude"></param>
651+
/// <returns></returns>
646652
public static Vector3d ClampMagnitude(Vector3d value, Fixed64 maxMagnitude)
647653
{
648-
Fixed64 magnitudeSqr = value.SqrMagnitude;
649-
if (magnitudeSqr > maxMagnitude * maxMagnitude)
650-
{
651-
Fixed64 magnitude = FixedMath.Sqrt(magnitudeSqr); // Get actual magnitude
652-
return (value / magnitude) * maxMagnitude; // Scale vector to max magnitude
653-
}
654+
if (value.SqrMagnitude > maxMagnitude * maxMagnitude)
655+
return value.Normal * maxMagnitude; // Scale vector to max magnitude
656+
654657
return value;
655658
}
656659

0 commit comments

Comments
 (0)