more fixed math stuff

pull/19/head
cosmonaut 2022-05-10 19:48:55 -07:00
parent 41d1c593cf
commit 85f21b7859
4 changed files with 32 additions and 12 deletions

View File

@ -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));
}
/// <summary>

View File

@ -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
/// </summary>
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;
}
/// <summary>
@ -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

View File

@ -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)

View File

@ -218,6 +218,14 @@ namespace MoonWorks.Math.Fixed
return Fix64.Atan2(Y, X);
}
/// <summary>
/// Returns this Vector2 with the fractional components cut off.
/// </summary>
public Vector2 Truncated()
{
return new Vector2((int) X, (int) Y);
}
/// <summary>
/// Returns a <see cref="String"/> representation of this <see cref="Vector2"/> in the format:
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>]}