From 8ecf6ed08e95445e80735859647764da7c242fa2 Mon Sep 17 00:00:00 2001 From: Robert Adams Date: Thu, 17 Jan 2013 14:44:54 -0800 Subject: [PATCH] Add utility function to clamp a vector to a maximum magnitude. --- OpenSim/Framework/Util.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 4fd8a2d288..7204279548 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -299,6 +299,18 @@ namespace OpenSim.Framework 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) public static bool InRange(T x, T min, T max) where T : IComparable