diff --git a/src/Collision/Fixed/SpatialHash2D.cs b/src/Collision/Fixed/SpatialHash2D.cs
index 2a3a33a..7b7ceda 100644
--- a/src/Collision/Fixed/SpatialHash2D.cs
+++ b/src/Collision/Fixed/SpatialHash2D.cs
@@ -28,7 +28,7 @@ namespace MoonWorks.Collision.Fixed
private (int, int) Hash(Vector2 position)
{
- return ((int) Fix64.Floor(position.X / cellSize), (int) Fix64.Floor(position.Y / cellSize));
+ return ((int) (position.X / cellSize), (int) (position.Y / cellSize));
}
///
diff --git a/src/Math/Fixed/Fix64.cs b/src/Math/Fixed/Fix64.cs
index d7c6820..c21d9ad 100644
--- a/src/Math/Fixed/Fix64.cs
+++ b/src/Math/Fixed/Fix64.cs
@@ -30,6 +30,9 @@ namespace MoonWorks.Math.Fixed
const int LUT_SIZE = (int)(PI_OVER_2 >> 15);
static readonly Fix64 LutInterval = (Fix64)(LUT_SIZE - 1) / PiOver2;
+ public bool IsFractional => (RawValue & 0x00000000FFFFFFFF) != 0;
+ public bool IsIntegral => (RawValue & 0x00000000FFFFFFFF) == 0;
+
private Fix64(long value)
{
RawValue = value;
@@ -95,8 +98,7 @@ namespace MoonWorks.Math.Fixed
///
public static Fix64 Ceiling(Fix64 value)
{
- var hasFractionalPart = (value.RawValue & 0x00000000FFFFFFFF) != 0;
- return hasFractionalPart ? Floor(value) + One : value;
+ return value.IsFractional ? Floor(value) + One : value;
}
///
@@ -697,12 +699,12 @@ namespace MoonWorks.Math.Fixed
public static bool operator >(Fix64 x, int y)
{
- return ((int) x) > y;
+ return x > ((Fix64) y);
}
public static bool operator <(Fix64 x, int y)
{
- return ((int) x) < y;
+ return x < ((Fix64) y);
}
public static bool operator >=(Fix64 x, Fix64 y)
@@ -717,12 +719,12 @@ namespace MoonWorks.Math.Fixed
public static bool operator >=(Fix64 x, int y)
{
- return ((int) x) >= y;
+ return x >= ((Fix64) y);
}
public static bool operator <=(Fix64 x, int y)
{
- return ((int) x) <= y;
+ return x <= ((Fix64) y);
}
// Casting
diff --git a/src/Math/Fixed/Transform2D.cs b/src/Math/Fixed/Transform2D.cs
index bbb9cfc..9a61c35 100644
--- a/src/Math/Fixed/Transform2D.cs
+++ b/src/Math/Fixed/Transform2D.cs
@@ -6,7 +6,21 @@ namespace MoonWorks.Math.Fixed
public Fix64 Rotation { get; }
public Vector2 Scale { get; }
- public Matrix3x2 TransformMatrix { get; }
+ private bool transformMatrixCalculated = false;
+ private Matrix3x2 transformMatrix = Matrix3x2.Identity;
+ public Matrix3x2 TransformMatrix
+ {
+ get
+ {
+ if (!transformMatrixCalculated)
+ {
+ transformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
+ transformMatrixCalculated = true;
+ }
+
+ return transformMatrix;
+ }
+ }
public bool IsAxisAligned => Rotation % Fix64.PiOver2 == Fix64.Zero;
public bool IsUniformScale => Scale.X == Scale.Y;
@@ -18,7 +32,6 @@ namespace MoonWorks.Math.Fixed
Position = Vector2.Zero;
Rotation = Fix64.Zero;
Scale = Vector2.One;
- TransformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
}
public Transform2D(Vector2 position)
@@ -26,7 +39,6 @@ namespace MoonWorks.Math.Fixed
Position = position;
Rotation = Fix64.Zero;
Scale = Vector2.One;
- TransformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
}
public Transform2D(Vector2 position, Fix64 rotation)
@@ -34,7 +46,6 @@ namespace MoonWorks.Math.Fixed
Position = position;
Rotation = rotation;
Scale = Vector2.One;
- TransformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
}
public Transform2D(Vector2 position, Fix64 rotation, Vector2 scale)
@@ -42,7 +53,6 @@ namespace MoonWorks.Math.Fixed
Position = position;
Rotation = rotation;
Scale = scale;
- TransformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
}
public Transform2D Compose(Transform2D other)
diff --git a/src/Math/Fixed/Vector2.cs b/src/Math/Fixed/Vector2.cs
index ced8b76..c261554 100644
--- a/src/Math/Fixed/Vector2.cs
+++ b/src/Math/Fixed/Vector2.cs
@@ -218,6 +218,14 @@ namespace MoonWorks.Math.Fixed
return Fix64.Atan2(Y, X);
}
+ ///
+ /// Returns this Vector2 with the fractional components cut off.
+ ///
+ public Vector2 Truncated()
+ {
+ return new Vector2((int) X, (int) Y);
+ }
+
///
/// Returns a representation of this in the format:
/// {X:[] Y:[]}