Geometry: add FMath class & convenient accessors for AABB corners.

The FMath class is like the System.Math class, but returns floats instead of
doubles so that you don't have to redundantly cast things.

GitOrigin-RevId: 0e1d7f46c7a5c08c1bc2d2c8776ffa56eab697d8
This commit is contained in:
Colin McMillen 2020-01-31 14:13:35 -05:00
parent 141a0660c4
commit 554578968d

View File

@ -5,6 +5,23 @@ using System;
namespace SemiColinGames { namespace SemiColinGames {
// Math functions that return floats rather than doubles, for convenience.
public static class FMath {
public const float PI = (float) Math.PI;
public static float DegToRad(float degrees) {
return PI / 180 * degrees;
}
public static float Sin(double degrees) {
return (float) Math.Sin(degrees);
}
public static float Cos(double degrees) {
return (float) Math.Cos(degrees);
}
}
public readonly struct Hit { public readonly struct Hit {
public readonly AABB Collider; public readonly AABB Collider;
public readonly Vector2 Position; public readonly Vector2 Position;
@ -56,6 +73,38 @@ namespace SemiColinGames {
HalfSize = halfSize; HalfSize = halfSize;
} }
public float Top {
get { return Position.Y - HalfSize.Y; }
}
public float Bottom {
get { return Position.Y + HalfSize.Y; }
}
public float Left {
get { return Position.X - HalfSize.X; }
}
public float Right {
get { return Position.X + HalfSize.X; }
}
public Vector2 TopLeft {
get { return new Vector2(Left, Top); }
}
public Vector2 TopRight {
get { return new Vector2(Right, Top); }
}
public Vector2 BottomLeft {
get { return new Vector2(Left, Bottom); }
}
public Vector2 BottomRight {
get { return new Vector2(Right, Bottom); }
}
public Hit? Intersect(AABB box) { public Hit? Intersect(AABB box) {
float dx = box.Position.X - Position.X; float dx = box.Position.X - Position.X;
float px = box.HalfSize.X + HalfSize.X - Math.Abs(dx); float px = box.HalfSize.X + HalfSize.X - Math.Abs(dx);