diff --git a/src/Collision/Fixed/NarrowPhase.cs b/src/Collision/Fixed/NarrowPhase.cs index 3117a74..731e969 100644 --- a/src/Collision/Fixed/NarrowPhase.cs +++ b/src/Collision/Fixed/NarrowPhase.cs @@ -141,7 +141,7 @@ namespace MoonWorks.Collision.Fixed if (shapeB == null) { throw new System.ArgumentNullException(nameof(shapeB)); } if (!simplex.TwoSimplex) { throw new System.ArgumentException("Simplex must be a 2-Simplex.", nameof(simplex)); } - var epsilon = Fix64.One / new Fix64(10000); + var epsilon = Fix64.FromFraction(1, 10000); var a = simplex.A; var b = simplex.B.Value; diff --git a/src/Math/Fixed/Fix64.cs b/src/Math/Fixed/Fix64.cs index a0e9292..d7c6820 100644 --- a/src/Math/Fixed/Fix64.cs +++ b/src/Math/Fixed/Fix64.cs @@ -40,6 +40,14 @@ namespace MoonWorks.Math.Fixed RawValue = value * ONE; } + /// + /// Create a fractional Fix64 number of the value (numerator / denominator). + /// + public static Fix64 FromFraction(int numerator, int denominator) + { + return new Fix64(numerator) / new Fix64(denominator); + } + /// /// Returns an int indicating the sign of a Fix64 number. /// @@ -73,18 +81,28 @@ namespace MoonWorks.Math.Fixed return new Fix64((value.RawValue + mask) ^ mask); } + /// + /// Returns the largest integral value less than or equal to the specified number. + /// public static Fix64 Floor(Fix64 value) { // Zero out the fractional part. return new Fix64((long)((ulong)value.RawValue & 0xFFFFFFFF00000000)); } + /// + /// Returns the smallest integral value that is greater than or equal to the specified number. + /// public static Fix64 Ceiling(Fix64 value) { var hasFractionalPart = (value.RawValue & 0x00000000FFFFFFFF) != 0; return hasFractionalPart ? Floor(value) + One : value; } + /// + /// Rounds to the nearest integral value. + /// If the value is halfway between an even and an uneven value, returns the even value. + /// public static Fix64 Round(Fix64 value) { var fractionalPart = value.RawValue & 0x00000000FFFFFFFF; @@ -104,22 +122,35 @@ namespace MoonWorks.Math.Fixed : integralPart + One; } - //Formula taken from https://docs.microsoft.com/en-us/dotnet/api/system.math.ieeeremainder?view=net-6.0 + /// + /// Returns a remainder value as defined by the IEEE remainder method. + /// + /// public static Fix64 IEEERemainder(Fix64 dividend, Fix64 divisor) { + //Formula taken from https://docs.microsoft.com/en-us/dotnet/api/system.math.ieeeremainder?view=net-6.0 return dividend - (divisor * Round(dividend / divisor)); } + /// + /// Returns the minimum of two given Fix64 values. + /// public static Fix64 Min(Fix64 x, Fix64 y) { return (x < y) ? x : y; } + /// + /// Returns the maximum of two given Fix64 values. + /// public static Fix64 Max(Fix64 x, Fix64 y) { return (x > y) ? x : y; } + /// + /// Returns a value that is neither greater than nor less than a given min and max value. + /// public static Fix64 Clamp(Fix64 value, Fix64 min, Fix64 max) { return Fix64.Min(Fix64.Max(value, min), max); @@ -127,6 +158,10 @@ namespace MoonWorks.Math.Fixed // Trigonometry functions + /// + /// Returns the square root of the given Fix64 value. + /// + /// Throws if x is less than zero. public static Fix64 Sqrt(Fix64 x) { var xl = x.RawValue; @@ -238,6 +273,9 @@ namespace MoonWorks.Math.Fixed return clampedPiOver2; } + /// + /// Returns the sine of the specified angle. + /// public static Fix64 Sin(Fix64 x) { var clampedL = ClampSinValue(x.RawValue, out var flipHorizontal, out var flipVertical); @@ -262,6 +300,9 @@ namespace MoonWorks.Math.Fixed return new Fix64(finalValue); } + /// + /// Returns the cosine of the specified angle. + /// public static Fix64 Cos(Fix64 x) { var xl = x.RawValue; @@ -269,6 +310,9 @@ namespace MoonWorks.Math.Fixed return Sin(new Fix64(rawAngle)); } + /// + /// Returns the tangent of the specified angle. + /// public static Fix64 Tan(Fix64 x) { var clampedPi = x.RawValue % PI; @@ -300,6 +344,9 @@ namespace MoonWorks.Math.Fixed return new Fix64(finalValue); } + /// + /// Returns the angle whose tangent is the specified number. + /// public static Fix64 Atan(Fix64 z) { if (z.RawValue == 0) return Zero; @@ -354,6 +401,9 @@ namespace MoonWorks.Math.Fixed return result; } + /// + /// Returns the angle whose tangent is the quotient of two specified numbers. + /// public static Fix64 Atan2(Fix64 y, Fix64 x) { var yl = y.RawValue; diff --git a/src/Math/Fixed/Matrix3x2.cs b/src/Math/Fixed/Matrix3x2.cs index f234ce3..0187ec5 100644 --- a/src/Math/Fixed/Matrix3x2.cs +++ b/src/Math/Fixed/Matrix3x2.cs @@ -51,7 +51,7 @@ namespace MoonWorks.Math.Fixed 0, 0 ); - private static readonly Fix64 RotationEpsilon = (Fix64.One / new Fix64(1000)) * (Fix64.Pi / new Fix64(180)); + private static readonly Fix64 RotationEpsilon = Fix64.FromFraction(1, 1000) * (Fix64.Pi / new Fix64(180)); /// /// Returns the multiplicative identity matrix.