From 0d29ab066fed4310cbd97aa846bf6f500fae3b32 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 23 Jan 2024 19:18:31 +0300 Subject: [PATCH] refactor: Added Static Methods Back --- Engine.Physics2D/Physics2D.cs | 77 +++++++++++++++++++++++++++++ Engine.Physics2D/PhysicsMath.cs | 51 ------------------- Engine.Physics2D/Primitives/Math.cs | 9 ---- 3 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 Engine.Physics2D/Physics2D.cs delete mode 100644 Engine.Physics2D/Primitives/Math.cs diff --git a/Engine.Physics2D/Physics2D.cs b/Engine.Physics2D/Physics2D.cs new file mode 100644 index 0000000..2c994d7 --- /dev/null +++ b/Engine.Physics2D/Physics2D.cs @@ -0,0 +1,77 @@ +using System; + +using Syntriax.Engine.Core; +using Syntriax.Engine.Physics2D; +using Syntriax.Engine.Physics2D.Primitives; + +namespace Engine.Physics2D; + +public static partial class Physics2D +{ + public const float RadianToDegree = 57.29577866666166f; + public const float DegreeToRadian = 0.01745329277777778f; + + public static bool Overlaps(this Circle left, Circle right) + { + float distanceSquared = left.Position.FromTo(right.Position).LengthSquared(); + float radiusSumSquared = left.RadiusSquared + right.RadiusSquared; + + return distanceSquared < radiusSumSquared; + } + + public static bool Overlaps(this Circle left, Circle right, out Vector2D normal, out float depth) + { + Vector2D distanceVector = left.Position.FromTo(right.Position); + float distanceSquared = distanceVector.LengthSquared(); + float radiusSumSquared = left.RadiusSquared + right.RadiusSquared; + bool isOverlapping = distanceSquared < radiusSumSquared; + + depth = 0f; + normal = distanceVector.Normalized; + + if (isOverlapping) + depth = MathF.Sqrt(radiusSumSquared - distanceSquared); + + return isOverlapping; + } + + public static bool Overlaps(this Circle circle, Vector2D point) => circle.Position.FromTo(point).LengthSquared() <= circle.RadiusSquared; + public static bool Overlaps(this Circle circle, Vector2D point, out Vector2D normal, out float depth) + { + Vector2D distanceVector = circle.Position.FromTo(point); + float distanceSquared = distanceVector.LengthSquared(); + float radiusSquared = circle.RadiusSquared; + bool isOverlapping = distanceSquared < radiusSquared; + + depth = 0f; + normal = distanceVector.Normalized; + + if (isOverlapping) + depth = MathF.Sqrt(radiusSquared - distanceSquared); + + return isOverlapping; + } + + public static bool Overlaps(this AABB aabb, Vector2D point) + => point.X >= aabb.LowerBoundary.X && point.X <= aabb.UpperBoundary.X && + point.Y >= aabb.LowerBoundary.Y && point.Y <= aabb.UpperBoundary.Y; + + public static bool Overlaps(this AABB left, AABB right) + => left.LowerBoundary.X <= right.UpperBoundary.X && left.UpperBoundary.X >= right.LowerBoundary.X && + left.LowerBoundary.Y <= right.UpperBoundary.Y && left.UpperBoundary.Y >= right.LowerBoundary.Y; + + public static bool Overlaps(Triangle triangle, Vector2D point) + { + float originalTriangleArea = triangle.Area; + + float pointTriangleArea1 = new Triangle(point, triangle.B, triangle.C).Area; + float pointTriangleArea2 = new Triangle(triangle.A, point, triangle.C).Area; + float pointTriangleArea3 = new Triangle(triangle.A, triangle.B, point).Area; + + float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3; + + return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f); + } + + public static bool LaysOn(this Vector2D point, Line line) => Line.Intersects(line, point); +} diff --git a/Engine.Physics2D/PhysicsMath.cs b/Engine.Physics2D/PhysicsMath.cs index 7ccd28c..bef5107 100644 --- a/Engine.Physics2D/PhysicsMath.cs +++ b/Engine.Physics2D/PhysicsMath.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; -using Microsoft.Xna.Framework; using Syntriax.Engine.Core; using Syntriax.Engine.Physics2D.Primitives; @@ -9,52 +8,6 @@ namespace Syntriax.Engine.Physics2D; public static class PhysicsMath { - public static Vector2D Scale(this Vector2D original, Vector2D scale) - => new Vector2D(original.X * scale.X, original.Y * scale.Y); - - public static Triangle ToSuperTriangle(this IList vertices) - { - float minX = float.MaxValue, minY = float.MaxValue; - float maxX = float.MinValue, maxY = float.MinValue; - - foreach (Vector2D point in vertices) - { - minX = MathF.Min(minX, point.X); - minY = MathF.Min(minY, point.Y); - maxX = MathF.Max(maxX, point.X); - maxY = MathF.Max(maxY, point.Y); - } - - float dx = maxX - minX; - float dy = maxY - minY; - float deltaMax = MathF.Max(dx, dy); - float midX = (minX + maxX) / 2; - float midY = (minY + maxY) / 2; - - Vector2D p1 = new Vector2D((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax); - Vector2D p2 = new Vector2D((float)midX, (float)midY + 20 * (float)deltaMax); - Vector2D p3 = new Vector2D((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax); - - return new Triangle(p1, p2, p3); - } - - public static IList ToLines(this IList vertices) - { - List lines = new List(vertices.Count - 1); - ToLines(vertices, lines); - return lines; - } - - public static void ToLines(this IList vertices, IList lines) - { - lines.Clear(); - for (int i = 0; i < vertices.Count - 1; i++) - lines.Add(new(vertices[i], vertices[i + 1])); - lines.Add(new(vertices[^1], vertices[0])); - } - - public static bool LaysOn(this Vector2D point, Line line) - => line.Resolve(point.X).ApproximatelyEquals(point); // Given three collinear points p, q, r, the function checks if @@ -92,10 +45,6 @@ public static class PhysicsMath public static bool ApproximatelyEquals(this float a, float b) => 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 Vector2D a, Vector2D b) => ApproximatelyEquals(a, b, float.Epsilon); public static bool ApproximatelyEquals(this Vector2D a, Vector2D b, float epsilon) diff --git a/Engine.Physics2D/Primitives/Math.cs b/Engine.Physics2D/Primitives/Math.cs deleted file mode 100644 index 79875d4..0000000 --- a/Engine.Physics2D/Primitives/Math.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Syntriax.Engine.Physics2D.Primitives; - -public static class Math -{ - public const float RadianToDegree = 57.29577866666166f; - public const float DegreeToRadian = 0.01745329277777778f; - - public static float Clamp(float value, float min, float max) => (value < min) ? min : (value > max) ? max : value; -}