From 0f9d5463983eeae11af901a5e7b0db9ce14b9525 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Mon, 3 Feb 2020 17:52:57 -0500 Subject: [PATCH] make Clamp() generic across comparables and move it into FMath. GitOrigin-RevId: 4ed26cc24dd204813540bde36889a17c20ad007b --- Shared/Geometry.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Shared/Geometry.cs b/Shared/Geometry.cs index 678c174..fb9bc94 100644 --- a/Shared/Geometry.cs +++ b/Shared/Geometry.cs @@ -20,6 +20,16 @@ namespace SemiColinGames { public static float Cos(double degrees) { return (float) Math.Cos(degrees); } + + public static T Clamp(T value, T min, T max) where T : IComparable { + if (value.CompareTo(min) == -1) { + return min; + } else if (value.CompareTo(max) == 1) { + return max; + } else { + return value; + } + } } public readonly struct Hit { @@ -58,16 +68,6 @@ namespace SemiColinGames { public readonly Vector2 Position; // centroid public readonly Vector2 HalfSize; - static float Clamp(float value, float min, float max) { - if (value < min) { - return min; - } else if (value > max) { - return max; - } else { - return value; - } - } - public AABB(Vector2 position, Vector2 halfSize) { Position = position; HalfSize = halfSize; @@ -188,10 +188,10 @@ namespace SemiColinGames { Vector2 direction = Vector2.Normalize(delta); // TODO: why is this calculation made, and then thrown away? Vector2 sweepHitPos = new Vector2( - Clamp(hit.Position.X + direction.X * box.HalfSize.X, + FMath.Clamp(hit.Position.X + direction.X * box.HalfSize.X, Position.X - HalfSize.X, Position.X + HalfSize.X), - Clamp(hit.Position.Y + direction.Y * box.HalfSize.Y, + FMath.Clamp(hit.Position.Y + direction.Y * box.HalfSize.Y, Position.Y - HalfSize.Y, Position.Y + HalfSize.Y)); return new Sweep(hit, hitPos, hit.Time);