From 554578968d23c561ea88f5958969c49432a900ab Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 31 Jan 2020 14:13:35 -0500 Subject: [PATCH] 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 --- Shared/Geometry.cs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Shared/Geometry.cs b/Shared/Geometry.cs index ccb1545..678c174 100644 --- a/Shared/Geometry.cs +++ b/Shared/Geometry.cs @@ -5,6 +5,23 @@ using System; 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 AABB Collider; public readonly Vector2 Position; @@ -56,6 +73,38 @@ namespace SemiColinGames { 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) { float dx = box.Position.X - Position.X; float px = box.HalfSize.X + HalfSize.X - Math.Abs(dx);