make Clamp() generic across comparables and move it into FMath.

GitOrigin-RevId: 4ed26cc24dd204813540bde36889a17c20ad007b
This commit is contained in:
Colin McMillen 2020-02-03 17:52:57 -05:00
parent bf2f33f698
commit 0f9d546398

View File

@ -20,6 +20,16 @@ namespace SemiColinGames {
public static float Cos(double degrees) {
return (float) Math.Cos(degrees);
}
public static T Clamp<T>(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);