From 2e723477218a85dc4a7bd25b783271711fb28f93 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Thu, 7 Dec 2023 11:14:18 +0300 Subject: [PATCH] Refactor 2 --- Game/Physics2D/PhysicsMath.cs | 13 ++++++------- Game/Physics2D/Primitives/AABB.cs | 7 +++++++ Game/Physics2D/Primitives/Circle.cs | 8 +++++--- Game/Physics2D/Primitives/Line.cs | 3 +++ Game/Physics2D/Primitives/LineEquation.cs | 2 ++ Game/Physics2D/Primitives/Shape.cs | 14 +++++++++++++- Game/Physics2D/Primitives/Triangle.cs | 7 +++++-- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Game/Physics2D/PhysicsMath.cs b/Game/Physics2D/PhysicsMath.cs index 3874330..b22d844 100644 --- a/Game/Physics2D/PhysicsMath.cs +++ b/Game/Physics2D/PhysicsMath.cs @@ -91,8 +91,12 @@ public static class PhysicsMath public static bool ApproximatelyEquals(this float a, float b) - => ApproximatelyEqualsEpsilon(a, b, float.Epsilon); - public static bool ApproximatelyEqualsEpsilon(this float a, float b, float epsilon) + => ApproximatelyEquals(a, b, float.Epsilon); + public static bool ApproximatelyEquals(this Vector2 a, Vector2 b) + => ApproximatelyEquals(a, b, float.Epsilon); + public static bool ApproximatelyEquals(this Vector2 a, Vector2 b, float epsilon) + => ApproximatelyEquals(a.X, b.X, epsilon) && ApproximatelyEquals(a.Y, b.Y, epsilon); + public static bool ApproximatelyEquals(this float a, float b, float epsilon) { if (a == b) return true; @@ -107,9 +111,4 @@ public static class PhysicsMath return diff / Math.Min(absA + absB, float.MaxValue) < epsilon; } - - public static bool ApproximatelyEquals(this Vector2 a, Vector2 b) - => ApproximatelyEqualEpsilon(a, b, float.Epsilon); - public static bool ApproximatelyEqualEpsilon(this Vector2 a, Vector2 b, float epsilon) - => ApproximatelyEqualsEpsilon(a.X, b.X, epsilon) && ApproximatelyEqualsEpsilon(a.Y, b.Y, epsilon); } diff --git a/Game/Physics2D/Primitives/AABB.cs b/Game/Physics2D/Primitives/AABB.cs index 94a4a65..e6d3c28 100644 --- a/Game/Physics2D/Primitives/AABB.cs +++ b/Game/Physics2D/Primitives/AABB.cs @@ -7,4 +7,11 @@ public record AABB(Vector2 LowerBoundary, Vector2 UpperBoundary) public bool Overlaps(Vector2 point) => point.X >= LowerBoundary.X && point.X <= UpperBoundary.X && point.Y >= LowerBoundary.Y && point.Y <= UpperBoundary.Y; + + public bool Overlaps(AABB other) + => LowerBoundary.X <= other.UpperBoundary.X && UpperBoundary.X >= other.LowerBoundary.X && + LowerBoundary.Y <= other.UpperBoundary.Y && UpperBoundary.Y >= other.LowerBoundary.Y; + + public bool ApproximatelyEquals(AABB other) + => LowerBoundary.ApproximatelyEquals(other.LowerBoundary) && UpperBoundary.ApproximatelyEquals(other.UpperBoundary); } diff --git a/Game/Physics2D/Primitives/Circle.cs b/Game/Physics2D/Primitives/Circle.cs index 734119e..386e651 100644 --- a/Game/Physics2D/Primitives/Circle.cs +++ b/Game/Physics2D/Primitives/Circle.cs @@ -4,13 +4,15 @@ namespace Syntriax.Engine.Physics2D.Primitives; public record Circle(Vector2 Position, float Radius) { - public bool Intersects(Circle circleOther) + public bool Intersects(Circle other) { - float distanceSquared = (Position - circleOther.Position).LengthSquared(); - float radiusSumSquared = Radius * Radius + circleOther.Radius * circleOther.Radius; + float distanceSquared = (Position - other.Position).LengthSquared(); + float radiusSumSquared = Radius * Radius + other.Radius * other.Radius; return distanceSquared < radiusSumSquared; } public bool Overlaps(Vector2 point) => (Position - point).LengthSquared() <= Radius * Radius; + public bool ApproximatelyEquals(Circle other) + => Position.ApproximatelyEquals(other.Position) && Radius.ApproximatelyEquals(other.Radius); } diff --git a/Game/Physics2D/Primitives/Line.cs b/Game/Physics2D/Primitives/Line.cs index 483c9ad..5553497 100644 --- a/Game/Physics2D/Primitives/Line.cs +++ b/Game/Physics2D/Primitives/Line.cs @@ -118,4 +118,7 @@ public record Line(Vector2 From, Vector2 To) return result; } + + public bool ApproximatelyEquals(Line other) + => From.ApproximatelyEquals(other.From) && To.ApproximatelyEquals(other.To); } diff --git a/Game/Physics2D/Primitives/LineEquation.cs b/Game/Physics2D/Primitives/LineEquation.cs index a85596c..a60977b 100644 --- a/Game/Physics2D/Primitives/LineEquation.cs +++ b/Game/Physics2D/Primitives/LineEquation.cs @@ -3,4 +3,6 @@ namespace Syntriax.Engine.Physics2D.Primitives; public record LineEquation(float Slope, float OffsetY) { public float Resolve(float x) => Slope * x + OffsetY; // y = mx + b + public bool ApproximatelyEquals(LineEquation other) + => Slope.ApproximatelyEquals(other.Slope) && OffsetY.ApproximatelyEquals(other.OffsetY); } diff --git a/Game/Physics2D/Primitives/Shape.cs b/Game/Physics2D/Primitives/Shape.cs index 8f827ce..bedc743 100644 --- a/Game/Physics2D/Primitives/Shape.cs +++ b/Game/Physics2D/Primitives/Shape.cs @@ -36,7 +36,7 @@ public record Shape(IList Vertices) } } - public IList Lines + public List Lines { get { @@ -53,4 +53,16 @@ public record Shape(IList Vertices) lines.Add(new(Vertices[i], Vertices[i + 1])); lines.Add(new(Vertices[^1], Vertices[0])); } + + public bool ApproximatelyEquals(Shape other) + { + if (Vertices.Count != other.Vertices.Count) + return false; + + for (int i = 0; i < Vertices.Count; i++) + if (!Vertices[i].ApproximatelyEquals(other.Vertices[i])) + return false; + + return true; + } } diff --git a/Game/Physics2D/Primitives/Triangle.cs b/Game/Physics2D/Primitives/Triangle.cs index 9d610ab..6417951 100644 --- a/Game/Physics2D/Primitives/Triangle.cs +++ b/Game/Physics2D/Primitives/Triangle.cs @@ -27,7 +27,7 @@ public record Triangle(Vector2 A, Vector2 B, Vector2 C) { float x = (slopeAB * slopeBC * (A.Y - C.Y) + slopeBC * (A.X + B.X) - slopeAB * (B.X + C.X)) / (2 * (slopeBC - slopeAB)); float y = -(x - (A.X + B.X) / 2) / slopeAB + (A.Y + B.Y) / 2; - center = new Vector2((float)x, (float)y); + center = new Vector2(x, y); } else center = (midAB + midBC) * .5f; @@ -46,6 +46,9 @@ public record Triangle(Vector2 A, Vector2 B, Vector2 C) float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3; - return originalTriangleArea >= pointTriangleAreasSum; + return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f); } + + public bool ApproximatelyEquals(Triangle other) + => A.ApproximatelyEquals(other.A) && B.ApproximatelyEquals(other.B) && C.ApproximatelyEquals(other.C); }