From b28196605e0e1de536452ec5843d36e033f9e991 Mon Sep 17 00:00:00 2001 From: Evan Hemsley <2342303+ehemsley@users.noreply.github.com> Date: Thu, 2 Jan 2020 01:40:03 -0800 Subject: [PATCH] new multishape pattern --- Bonk/BroadPhase/SpatialHash.cs | 6 +- Bonk/IHasAABB2D.cs | 17 +++++ Bonk/IMultiShape2D.cs | 10 +++ Bonk/IShape2D.cs | 11 +--- Bonk/MinkowskiDifference.cs | 4 +- Bonk/MultiShapes/MultiRectangle.cs | 65 ++++++++++++++++++ Bonk/NarrowPhase/NarrowPhase.cs | 57 ++++++++++++++++ Bonk/Shapes/Polygon.cs | 7 +- Bonk/Shapes/Rectangle.cs | 62 ++++++------------ Bonk/Shapes/RectanglePolygonComparison.cs | 25 ------- Test/AABBTest.cs | 5 ++ Test/EPA2DTest.cs | 6 +- Test/Equality.cs | 80 +++-------------------- Test/{GJK2DTest.cs => NarrowPhaseTest.cs} | 78 +++++++++++++++++----- Test/SpatialHashTest.cs | 36 ++++++---- 15 files changed, 281 insertions(+), 188 deletions(-) create mode 100644 Bonk/IHasAABB2D.cs create mode 100644 Bonk/IMultiShape2D.cs create mode 100644 Bonk/MultiShapes/MultiRectangle.cs delete mode 100644 Bonk/Shapes/RectanglePolygonComparison.cs rename Test/{GJK2DTest.cs => NarrowPhaseTest.cs} (83%) diff --git a/Bonk/BroadPhase/SpatialHash.cs b/Bonk/BroadPhase/SpatialHash.cs index 72dc32c..8f54856 100644 --- a/Bonk/BroadPhase/SpatialHash.cs +++ b/Bonk/BroadPhase/SpatialHash.cs @@ -14,7 +14,7 @@ namespace MoonTools.Core.Bonk private readonly int cellSize; private readonly Dictionary> hashDictionary = new Dictionary>(); - private readonly Dictionary IDLookup = new Dictionary(); + private readonly Dictionary IDLookup = new Dictionary(); public SpatialHash(int cellSize) { @@ -32,7 +32,7 @@ namespace MoonTools.Core.Bonk /// A unique ID for the shape-transform pair. /// /// - public void Insert(T id, IShape2D shape, Transform2D transform2D) + public void Insert(T id, IHasAABB2D shape, Transform2D transform2D) { var box = shape.TransformedAABB(transform2D); var minHash = Hash(box.Min); @@ -57,7 +57,7 @@ namespace MoonTools.Core.Bonk /// /// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID. /// - public IEnumerable<(T, IShape2D, Transform2D)> Retrieve(T id, IShape2D shape, Transform2D transform2D) + public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D) { var box = shape.TransformedAABB(transform2D); var minHash = Hash(box.Min); diff --git a/Bonk/IHasAABB2D.cs b/Bonk/IHasAABB2D.cs new file mode 100644 index 0000000..5acd583 --- /dev/null +++ b/Bonk/IHasAABB2D.cs @@ -0,0 +1,17 @@ +using System; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public interface IHasAABB2D + { + AABB AABB { get; } + + /// + /// Returns a bounding box based on the shape. + /// + /// A Transform for transforming the shape vertices. + /// Returns a bounding box based on the shape. + AABB TransformedAABB(Transform2D transform); + } +} diff --git a/Bonk/IMultiShape2D.cs b/Bonk/IMultiShape2D.cs new file mode 100644 index 0000000..390d03c --- /dev/null +++ b/Bonk/IMultiShape2D.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public interface IMultiShape2D : IHasAABB2D + { + IEnumerable<(IShape2D, Transform2D)> ShapeTransformPairs { get; } + } +} diff --git a/Bonk/IShape2D.cs b/Bonk/IShape2D.cs index 05d7616..1394b43 100644 --- a/Bonk/IShape2D.cs +++ b/Bonk/IShape2D.cs @@ -4,10 +4,8 @@ using MoonTools.Core.Structs; namespace MoonTools.Core.Bonk { - public interface IShape2D : IEquatable + public interface IShape2D : IHasAABB2D, IEquatable { - AABB AABB { get; } - /// /// A Minkowski support function. Gives the farthest point on the edge of a shape along the given direction. /// @@ -15,12 +13,5 @@ namespace MoonTools.Core.Bonk /// A Transform for transforming the shape vertices. /// The farthest point on the edge of the shape along the given direction. Vector2 Support(Vector2 direction, Transform2D transform); - - /// - /// Returns a bounding box based on the shape. - /// - /// A Transform for transforming the shape vertices. - /// Returns a bounding box based on the shape. - AABB TransformedAABB(Transform2D transform); } } diff --git a/Bonk/MinkowskiDifference.cs b/Bonk/MinkowskiDifference.cs index 89d7c0d..cc45fce 100644 --- a/Bonk/MinkowskiDifference.cs +++ b/Bonk/MinkowskiDifference.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Numerics; using MoonTools.Core.Structs; @@ -56,4 +56,4 @@ namespace MoonTools.Core.Bonk return !(a == b); } } -} \ No newline at end of file +} diff --git a/Bonk/MultiShapes/MultiRectangle.cs b/Bonk/MultiShapes/MultiRectangle.cs new file mode 100644 index 0000000..310af84 --- /dev/null +++ b/Bonk/MultiShapes/MultiRectangle.cs @@ -0,0 +1,65 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public struct MultiRectangle : IMultiShape2D + { + private ImmutableArray<(Rectangle, Transform2D)> Rectangles { get; } + + public IEnumerable<(IShape2D, Transform2D)> ShapeTransformPairs + { + get + { + foreach (var rectangle in Rectangles) { yield return rectangle; } + } + } + + public AABB AABB { get; } + + public MultiRectangle(ImmutableArray<(Rectangle, Transform2D)> rectangles) + { + Rectangles = rectangles; + + AABB = AABBFromRectangles(rectangles); + } + + public AABB TransformedAABB(Transform2D transform) + { + return AABB.Transformed(AABB, transform); + } + + private static AABB AABBFromRectangles(IEnumerable<(Rectangle, Transform2D)> rectangles) + { + var minX = float.MaxValue; + var minY = float.MaxValue; + var maxX = float.MinValue; + var maxY = float.MinValue; + + foreach (var (rectangle, transform) in rectangles) + { + var transformedAABB = rectangle.TransformedAABB(transform); + + if (transformedAABB.Min.X < minX) + { + minX = transformedAABB.Min.X; + } + if (transformedAABB.Min.Y < minY) + { + minY = transformedAABB.Min.Y; + } + if (transformedAABB.Max.X > maxX) + { + maxX = transformedAABB.Max.X; + } + if (transformedAABB.Max.Y > maxY) + { + maxY = transformedAABB.Max.Y; + } + } + + return new AABB(minX, minY, maxX, maxY); + } + } +} diff --git a/Bonk/NarrowPhase/NarrowPhase.cs b/Bonk/NarrowPhase/NarrowPhase.cs index 337e7ce..7cd731b 100644 --- a/Bonk/NarrowPhase/NarrowPhase.cs +++ b/Bonk/NarrowPhase/NarrowPhase.cs @@ -35,6 +35,63 @@ namespace MoonTools.Core.Bonk return FindCollisionSimplex(shapeA, transformA, shapeB, transformB).Item1; } + /// + /// Tests if a multishape-transform and shape-transform pair are overlapping. + /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape. + /// + /// + /// + /// + /// + /// + public static bool TestCollision(IMultiShape2D multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform) + { + foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs) + { + if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(otherTransform))) { return true; } + } + return false; + } + + /// + /// Tests if a multishape-transform and shape-transform pair are overlapping. + /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishape. + /// + /// + /// + /// + /// + /// + public static bool TestCollision(IShape2D shape, Transform2D shapeTransform, IMultiShape2D multiShape, Transform2D multiShapeTransform) + { + foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs) + { + if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(otherTransform))) { return true; } + } + return false; + } + + /// + /// Tests if two multishape-transform pairs are overlapping. + /// Note that this must perform pairwise comparison so the worst-case performance of this method will vary inversely with the amount of shapes in the multishapes. + /// + /// + /// + /// + /// + /// + public static bool TestCollision(IMultiShape2D multiShapeA, Transform2D transformA, IMultiShape2D multiShapeB, Transform2D transformB) + { + foreach (var (shapeA, shapeTransformA) in multiShapeA.ShapeTransformPairs) + { + foreach (var (shapeB, shapeTransformB) in multiShapeB.ShapeTransformPairs) + { + if (TestCollision(shapeA, transformA.Compose(shapeTransformA), shapeB, transformB.Compose(shapeTransformB))) { return true; } + } + } + return false; + } + /// /// Fast path for axis-aligned rectangles. If the transforms have non-zero rotation this will be inaccurate. /// diff --git a/Bonk/Shapes/Polygon.cs b/Bonk/Shapes/Polygon.cs index 90dc44c..bb7a1b4 100644 --- a/Bonk/Shapes/Polygon.cs +++ b/Bonk/Shapes/Polygon.cs @@ -59,7 +59,7 @@ namespace MoonTools.Core.Bonk public bool Equals(IShape2D other) { - return (other is Polygon otherPolygon && Equals(otherPolygon)) || (other is Rectangle rectangle && Equals(rectangle)); + return (other is Polygon otherPolygon && Equals(otherPolygon)); } public bool Equals(Polygon other) @@ -82,11 +82,6 @@ namespace MoonTools.Core.Bonk return true; } - public bool Equals(Rectangle rectangle) - { - return RectanglePolygonComparison.Equals(this, rectangle); - } - public override int GetHashCode() { return HashCode.Combine(Vertices); diff --git a/Bonk/Shapes/Rectangle.cs b/Bonk/Shapes/Rectangle.cs index fc6db8e..5095d02 100644 --- a/Bonk/Shapes/Rectangle.cs +++ b/Bonk/Shapes/Rectangle.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Numerics; using MoonTools.Core.Structs; @@ -10,47 +9,33 @@ namespace MoonTools.Core.Bonk /// public struct Rectangle : IShape2D, IEquatable { - /// - /// The minimum position of the rectangle. Note that we assume y-down coordinates. - /// - /// - public Position2D Min { get; } - /// - /// The maximum position of the rectangle. Note that we assume y-down coordinates. - /// - /// - public Position2D Max { get; } public AABB AABB { get; } - - public int Left { get { return Min.X; } } - public int Right { get { return Max.X; } } - public int Top { get { return Min.Y; } } - public int Bottom { get { return Max.Y; } } - public int Width { get; } public int Height { get; } - public Position2D TopRight { get { return new Position2D(Right, Top); } } - public Position2D BottomLeft { get { return new Position2D(Left, Bottom); } } + public float Right { get; } + public float Left { get; } + public float Top { get; } + public float Bottom { get; } + public Vector2 BottomLeft { get; } + public Vector2 TopRight { get; } - public IEnumerable Vertices - { - get - { - yield return new Position2D(Min.X, Min.Y); - yield return new Position2D(Min.X, Max.Y); - yield return new Position2D(Max.X, Min.Y); - yield return new Position2D(Max.X, Max.Y); - } - } + public Vector2 Min { get; } + public Vector2 Max { get; } - public Rectangle(int minX, int minY, int maxX, int maxY) + public Rectangle(int width, int height) { - Min = new Position2D(minX, minY); - Max = new Position2D(maxX, maxY); - AABB = new AABB(minX, minY, maxX, maxY); - Width = Max.X - Min.X; - Height = Max.Y - Min.Y; + Width = width; + Height = height; + AABB = new AABB(-width / 2f, -height / 2f, width / 2f, height / 2f); + Right = AABB.Right; + Left = AABB.Left; + Top = AABB.Top; + Bottom = AABB.Bottom; + BottomLeft = new Vector2(Left, Bottom); + TopRight = new Vector2(Top, Right); + Min = AABB.Min; + Max = AABB.Max; } private Vector2 Support(Vector2 direction) @@ -97,7 +82,7 @@ namespace MoonTools.Core.Bonk public bool Equals(IShape2D other) { - return (other is Rectangle rectangle && Equals(rectangle)) || (other is Polygon polygon && Equals(polygon)); + return (other is Rectangle rectangle && Equals(rectangle)); } public bool Equals(Rectangle other) @@ -105,11 +90,6 @@ namespace MoonTools.Core.Bonk return Min == other.Min && Max == other.Max; } - public bool Equals(Polygon other) - { - return RectanglePolygonComparison.Equals(other, this); - } - public override int GetHashCode() { return HashCode.Combine(Min, Max); diff --git a/Bonk/Shapes/RectanglePolygonComparison.cs b/Bonk/Shapes/RectanglePolygonComparison.cs deleted file mode 100644 index 36821d7..0000000 --- a/Bonk/Shapes/RectanglePolygonComparison.cs +++ /dev/null @@ -1,25 +0,0 @@ -using MoonTools.Core.Structs; - -namespace MoonTools.Core.Bonk -{ - internal static class RectanglePolygonComparison - { - public static bool Equals(Polygon polygon, Rectangle rectangle) - { - if (polygon.VertexCount != 4) { return false; } - - int? minIndex = null; - for (var i = 0; i < 4; i++) - { - if (polygon.Vertices[i] == rectangle.Min) { minIndex = i; break; } - } - - if (!minIndex.HasValue) { return false; } - - return - polygon.Vertices[(minIndex.Value + 1) % 4] == rectangle.TopRight && - polygon.Vertices[(minIndex.Value + 2) % 4] == rectangle.Max && - polygon.Vertices[(minIndex.Value + 3) % 4] == rectangle.BottomLeft; - } - } -} diff --git a/Test/AABBTest.cs b/Test/AABBTest.cs index be842f3..b283766 100644 --- a/Test/AABBTest.cs +++ b/Test/AABBTest.cs @@ -14,6 +14,11 @@ namespace Tests var b = new AABB(new Vector2(0, 0), new Vector2(2, 2)); AABB.TestOverlap(a, b).Should().BeTrue(); + + var c = new AABB(-2, -2, 2, 1); + var d = new AABB(-2, -2, 2, 2); + + AABB.TestOverlap(c, d).Should().BeTrue(); } [Test] diff --git a/Test/EPA2DTest.cs b/Test/EPA2DTest.cs index 6ffc348..87bef7c 100644 --- a/Test/EPA2DTest.cs +++ b/Test/EPA2DTest.cs @@ -13,9 +13,9 @@ namespace Tests [Test] public void RectangleOverlap() { - var squareA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var squareA = new Rectangle(2, 2); var transformA = Transform2D.DefaultTransform; - var squareB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var squareB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1.5f, 0)); var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB); @@ -62,7 +62,7 @@ namespace Tests { var line = new Line(new Position2D(-4, -4), new Position2D(4, 4)); var transformA = Transform2D.DefaultTransform; - var square = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var square = new Rectangle(2, 2); var transformB = Transform2D.DefaultTransform; var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB); diff --git a/Test/Equality.cs b/Test/Equality.cs index d441c83..9f6cb65 100644 --- a/Test/Equality.cs +++ b/Test/Equality.cs @@ -140,8 +140,8 @@ namespace Tests [Test] public void RectangleEqual() { - var a = new Rectangle(0, 0, 3, 3); - var b = new Rectangle(0, 0, 3, 3); + var a = new Rectangle(3, 3); + var b = new Rectangle(3, 3); a.Equals(b).Should().BeTrue(); } @@ -149,8 +149,8 @@ namespace Tests [Test] public void RectangleEqualOperator() { - var a = new Rectangle(0, 0, 3, 3); - var b = new Rectangle(0, 0, 3, 3); + var a = new Rectangle(3, 3); + var b = new Rectangle(3, 3); (a == b).Should().BeTrue(); } @@ -158,8 +158,8 @@ namespace Tests [Test] public void RectangleNotEqual() { - var a = new Rectangle(0, 0, 3, 3); - var b = new Rectangle(-1, -1, 5, 5); + var a = new Rectangle(3, 3); + var b = new Rectangle(6, 6); a.Equals(b).Should().BeFalse(); } @@ -167,8 +167,8 @@ namespace Tests [Test] public void RectangleNotEqualOperator() { - var a = new Rectangle(0, 0, 3, 3); - var b = new Rectangle(-1, -1, 5, 5); + var a = new Rectangle(3, 3); + var b = new Rectangle(6, 6); (a != b).Should().BeTrue(); } @@ -283,70 +283,6 @@ namespace Tests (a != b).Should().BeTrue(); } - - [Test] - public void PolygonRectangleEqual() - { - var a = new Polygon(ImmutableArray.Create( - new Position2D(-1, -1), - new Position2D(1, -1), - new Position2D(1, 1), - new Position2D(-1, 1) - )); - - var b = new Rectangle(-1, -1, 1, 1); - - a.Equals(b).Should().BeTrue(); - b.Equals(a).Should().BeTrue(); - } - - [Test] - public void PolygonRectangleNotEqual() - { - var a = new Polygon(ImmutableArray.Create( - new Position2D(-2, -1), - new Position2D(1, -1), - new Position2D(1, 1), - new Position2D(-2, 1) - )); - - var b = new Rectangle(-1, -1, 1, 1); - - a.Equals(b).Should().BeFalse(); - b.Equals(a).Should().BeFalse(); - } - - [Test] - public void PolygonRectangleEqualOperator() - { - var a = new Polygon(ImmutableArray.Create( - new Position2D(-1, -1), - new Position2D(1, -1), - new Position2D(1, 1), - new Position2D(-1, 1) - )); - - var b = new Rectangle(-1, -1, 1, 1); - - (a == b).Should().BeTrue(); - (b == a).Should().BeTrue(); - } - - [Test] - public void PolygonRectangleNotEqualOperator() - { - var a = new Polygon(ImmutableArray.Create( - new Position2D(2, 1), - new Position2D(1, -1), - new Position2D(-1, -1), - new Position2D(-2, 1) - )); - - var b = new Rectangle(-1, -1, 1, 1); - - (a != b).Should().BeTrue(); - (b != a).Should().BeTrue(); - } } public class SimplexTests diff --git a/Test/GJK2DTest.cs b/Test/NarrowPhaseTest.cs similarity index 83% rename from Test/GJK2DTest.cs rename to Test/NarrowPhaseTest.cs index 540d08b..1b3755a 100644 --- a/Test/GJK2DTest.cs +++ b/Test/NarrowPhaseTest.cs @@ -7,7 +7,7 @@ using System.Collections.Immutable; namespace Tests { - public class GJK2DTest + public class NarrowPhaseTest { [Test] public void PointLineOverlapping() @@ -55,7 +55,7 @@ namespace Tests public void PointRectangleOverlapping() { var point = new Point(); - var rectangle = new Rectangle(-2, -2, 2, 2); + var rectangle = new Rectangle(4, 4); NarrowPhase.TestCollision(point, Transform2D.DefaultTransform, rectangle, Transform2D.DefaultTransform).Should().BeTrue(); } @@ -65,7 +65,7 @@ namespace Tests { var point = new Point(); var pointTransform = new Transform2D(new Position2D(5, 5)); - var rectangle = new Rectangle(-2, -2, 2, 2); + var rectangle = new Rectangle(4, 4); NarrowPhase.TestCollision(point, pointTransform, rectangle, Transform2D.DefaultTransform).Should().BeFalse(); } @@ -354,10 +354,10 @@ namespace Tests [Test] public void RectanglesNotOverlapping() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-6, -6, 6, 6); + var rectangleA = new Rectangle(12, 12); var transformA = new Transform2D(new Position2D(39, 249)); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(0, 0, 16, 16); + var rectangleB = new Rectangle(16, 16); var transformB = new Transform2D(new Position2D(16, 240)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse(); @@ -366,10 +366,10 @@ namespace Tests [Test] public void RotatedRectanglesOverlapping() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 2, 2); + var rectangleA = new Rectangle(3, 3); var transformA = new Transform2D(new Vector2(-1, 0), -90f); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1, 0)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); @@ -378,10 +378,10 @@ namespace Tests [Test] public void RectanglesTouchingGJK2D() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(2, 2); var transformA = new Transform2D(new Position2D(-1, 0)); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1, 0)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); @@ -390,10 +390,10 @@ namespace Tests [Test] public void RectanglesOverlappingGJK2D() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(2, 2); var transformA = new Transform2D(new Position2D(0, 0)); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1, 0)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); @@ -402,10 +402,10 @@ namespace Tests [Test] public void RectanglesTouchingOverlap() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(2, 2); var transformA = new Transform2D(new Position2D(-1, 0)); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1, 0)); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); @@ -414,13 +414,61 @@ namespace Tests [Test] public void RectanglesOverlappingOverlap() { - var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(2, 2); var transformA = new Transform2D(new Position2D(0, 0)); - var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(2, 2); var transformB = new Transform2D(new Vector2(1, 0)); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); } + + [Test] + public void MultiRectanglesOverlapping() + { + var multiRectangleA = new MultiRectangle( + ImmutableArray.Create( + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) + ) + ); + var transformA = new Transform2D(new Position2D(5, 0)); + + var multiRectangleB = new MultiRectangle( + ImmutableArray.Create( + (new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), + (new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), + (new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) + ) + ); + var transformB = new Transform2D(new Position2D(0, 3)); + + NarrowPhase.TestCollision(multiRectangleA, transformA, multiRectangleB, transformB).Should().BeTrue(); + } + + [Test] + public void MultiRectanglesNotOverlapping() + { + var multiRectangleA = new MultiRectangle( + ImmutableArray.Create( + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) + ) + ); + var transformA = new Transform2D(new Position2D(5, 0)); + + var multiRectangleB = new MultiRectangle( + ImmutableArray.Create( + (new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), + (new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), + (new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) + ) + ); + var transformB = new Transform2D(new Position2D(0, -3)); + + NarrowPhase.TestCollision(multiRectangleA, transformA, multiRectangleB, transformB).Should().BeFalse(); + } } } diff --git a/Test/SpatialHashTest.cs b/Test/SpatialHashTest.cs index 2d4b153..da75900 100644 --- a/Test/SpatialHashTest.cs +++ b/Test/SpatialHashTest.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using MoonTools.Core.Structs; using MoonTools.Core.Bonk; using System.Numerics; +using System.Collections.Immutable; namespace Tests { @@ -13,16 +14,16 @@ namespace Tests { var spatialHash = new SpatialHash(16); - var rectA = new Rectangle(-2, -2, 2, 2); + var rectA = new Rectangle(4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(4, 4); var rectBTransform = new Transform2D(new Vector2(8, 8)); - var rectC = new Rectangle(-2, -2, 2, 2); + var rectC = new Rectangle(4, 4); var rectCTransform = new Transform2D(new Vector2(24, -4)); - var rectD = new Rectangle(-2, -2, 2, 2); + var rectD = new Rectangle(4, 4); var rectDTransform = new Transform2D(new Vector2(24, 24)); var circleA = new Circle(2); @@ -37,25 +38,38 @@ namespace Tests var point = new Point(); var pointTransform = new Transform2D(new Position2D(8, 8)); + var multiRectangle = new MultiRectangle( + ImmutableArray.Create( + (new Rectangle(4, 1), new Transform2D(new Position2D(-2, -2))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-2, -1))), + (new Rectangle(4, 1), new Transform2D(new Position2D(-2, 0))) + ) + ); + var multiRectangleTransform = new Transform2D(new Position2D(8, 8)); + spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(1, rectB, rectBTransform); spatialHash.Insert(2, rectC, rectCTransform); spatialHash.Insert(3, rectD, rectDTransform); spatialHash.Insert(4, circleA, circleATransform); - spatialHash.Insert(1, circleB, circleBTransform); + spatialHash.Insert(9, circleB, circleBTransform); spatialHash.Insert(6, line, lineTransform); spatialHash.Insert(7, point, pointTransform); + spatialHash.Insert(8, multiRectangle, multiRectangleTransform); spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty(); spatialHash.Retrieve(1, rectB, rectBTransform).Should().NotContain((1, circleB, circleBTransform)); spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((7, point, pointTransform)); + spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((8, multiRectangle, multiRectangleTransform)); spatialHash.Retrieve(2, rectC, rectCTransform).Should().Contain((6, line, lineTransform)).And.Contain((4, circleA, circleATransform)); - spatialHash.Retrieve(3, rectD, rectDTransform).Should().Contain((1, circleB, circleBTransform)); + spatialHash.Retrieve(3, rectD, rectDTransform).Should().Contain((9, circleB, circleBTransform)); spatialHash.Retrieve(4, circleA, circleATransform).Should().Contain((6, line, lineTransform)).And.Contain((2, rectC, rectCTransform)); spatialHash.Retrieve(1, circleB, circleBTransform).Should().NotContain((1, rectB, rectBTransform)).And.Contain((3, rectD, rectDTransform)); spatialHash.Retrieve(6, line, lineTransform).Should().Contain((4, circleA, circleATransform)).And.Contain((2, rectC, rectCTransform)); + + spatialHash.Retrieve(8, multiRectangle, multiRectangleTransform).Should().Contain((1, rectB, rectBTransform)); } [Test] @@ -63,13 +77,13 @@ namespace Tests { var spatialHash = new SpatialHash(16); - var rectA = new Rectangle(-2, -2, 2, 2); + var rectA = new Rectangle(4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(4, 4); var rectBTransform = new Transform2D(new Vector2(-8, -8)); - var rectC = new Rectangle(-1, -1, 1, 1); + var rectC = new Rectangle(2, 2); var rectCTransform = new Transform2D(new Vector2(-8, -8)); spatialHash.Insert(0, rectA, rectATransform); @@ -84,10 +98,10 @@ namespace Tests { var spatialHash = new SpatialHash(16); - var rectA = new Rectangle(-2, -2, 2, 2); + var rectA = new Rectangle(4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(4, 4); var rectBTransform = new Transform2D(new Vector2(8, 8)); spatialHash.Insert(0, rectA, rectATransform);