Browse Source

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: 0e1d7f46c7
master
Colin McMillen 4 years ago
parent
commit
554578968d
  1. 49
      Shared/Geometry.cs

49
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);

Loading…
Cancel
Save