Add utility function to clamp a vector to a maximum magnitude.

0.7.5-pf-bulletsim
Robert Adams 2013-01-17 14:44:54 -08:00 committed by Justin Clark-Casey (justincc)
parent 09450448b0
commit 8ecf6ed08e
1 changed files with 12 additions and 0 deletions

View File

@ -299,6 +299,18 @@ namespace OpenSim.Framework
x; x;
} }
// Clamp the maximum magnitude of a vector
public static Vector3 ClampV(Vector3 x, float max)
{
Vector3 ret = x;
float lenSq = x.LengthSquared();
if (lenSq > (max * max))
{
x = x / x.Length() * max;
}
return x;
}
// Inclusive, within range test (true if equal to the endpoints) // Inclusive, within range test (true if equal to the endpoints)
public static bool InRange<T>(T x, T min, T max) public static bool InRange<T>(T x, T min, T max)
where T : IComparable<T> where T : IComparable<T>