Skip to content

Commit 723c22a

Browse files
committed
feat: add method to clamp Vector3d by magnitude
1 parent 6d2ea43 commit 723c22a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/FixedMathSharp/Extensions/Vector3d.Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public static Vector3d ClampOneInPlace(this Vector3d v)
2020
return v;
2121
}
2222

23+
public static Vector3d ClampMagnitude(this Vector3d value, Fixed64 maxMagnitude)
24+
{
25+
return Vector3d.ClampMagnitude(value, maxMagnitude);
26+
}
27+
2328
/// <summary>
2429
/// Checks if the distance between two vectors is less than or equal to a specified factor.
2530
/// </summary>

src/FixedMathSharp/Numerics/Vector3d.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,17 @@ public static Vector3d Clamp(Vector3d value, Vector3d min, Vector3d max)
622622
);
623623
}
624624

625+
public static Vector3d ClampMagnitude(Vector3d value, Fixed64 maxMagnitude)
626+
{
627+
Fixed64 magnitudeSqr = value.SqrMagnitude;
628+
if (magnitudeSqr > maxMagnitude * maxMagnitude)
629+
{
630+
Fixed64 magnitude = FixedMath.Sqrt(magnitudeSqr); // Get actual magnitude
631+
return (value / magnitude) * maxMagnitude; // Scale vector to max magnitude
632+
}
633+
return value;
634+
}
635+
625636
/// <summary>
626637
/// Determines if two vectors are exactly parallel by checking if their cross product is zero.
627638
/// </summary>

0 commit comments

Comments
 (0)