diff --git a/Bonk/BroadPhase/SpatialHash.cs b/Bonk/BroadPhase/SpatialHash.cs index fc6d7f7..354a009 100644 --- a/Bonk/BroadPhase/SpatialHash.cs +++ b/Bonk/BroadPhase/SpatialHash.cs @@ -14,7 +14,13 @@ namespace MoonTools.Bonk private readonly int cellSize; private readonly Dictionary> hashDictionary = new Dictionary>(); - private readonly Dictionary IDLookup = new Dictionary(); + private readonly Dictionary IDLookup = new Dictionary(); + + public int MinX { get; private set; } = 0; + public int MaxX { get; private set; } = 0; + public int MinY { get; private set; } = 0; + public int MaxY { get; private set; } = 0; + public SpatialHash(int cellSize) { @@ -32,7 +38,7 @@ namespace MoonTools.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); @@ -52,20 +58,30 @@ namespace MoonTools.Bonk IDLookup[id] = (shape, transform2D); } } + + MinX = Math.Min(MinX, minHash.Item1); + MinY = Math.Min(MinY, minHash.Item2); + MaxX = Math.Max(MaxX, maxHash.Item1); + MaxY = Math.Max(MaxY, maxHash.Item2); } /// /// 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); - var maxHash = Hash(box.Max); + var (minX, minY) = Hash(box.Min); + var (maxX, maxY) = Hash(box.Max); - for (var i = minHash.Item1; i <= maxHash.Item1; i++) + if (minX < MinX) { minX = MinX; } + if (maxX > MaxX) { maxX = MaxX; } + if (minY < MinY) { minY = MinY; } + if (maxY > MaxY) { maxY = MaxY; } + + for (var i = minX; i <= maxX; i++) { - for (var j = minHash.Item2; j <= maxHash.Item2; j++) + for (var j = minY; j <= maxY; j++) { var key = MakeLong(i, j); if (hashDictionary.ContainsKey(key)) @@ -73,7 +89,43 @@ namespace MoonTools.Bonk foreach (var t in hashDictionary[key]) { var (otherShape, otherTransform) = IDLookup[t]; - if (!id.Equals(t) && AABB.TestOverlap(shape.TransformedAABB(transform2D), otherShape.TransformedAABB(otherTransform))) + if (!id.Equals(t) && AABB.TestOverlap(box, otherShape.TransformedAABB(otherTransform))) + { + yield return (t, otherShape, otherTransform); + } + } + } + } + } + } + + + /// + /// Retrieves objects based on a pre-transformed AABB. + /// + /// A transformed AABB. + /// + public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB aabb) + { + var (minX, minY) = Hash(aabb.Min); + var (maxX, maxY) = Hash(aabb.Max); + + if (minX < MinX) { minX = MinX; } + if (maxX > MaxX) { maxX = MaxX; } + if (minY < MinY) { minY = MinY; } + if (maxY > MaxY) { maxY = MaxY; } + + for (var i = minX; i <= maxX; i++) + { + for (var j = minY; j <= maxY; j++) + { + var key = MakeLong(i, j); + if (hashDictionary.ContainsKey(key)) + { + foreach (var t in hashDictionary[key]) + { + var (otherShape, otherTransform) = IDLookup[t]; + if (AABB.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform))) { yield return (t, otherShape, otherTransform); } 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/IShape2D.cs b/Bonk/IShape2D.cs index df8da4f..f8ecb2e 100644 --- a/Bonk/IShape2D.cs +++ b/Bonk/IShape2D.cs @@ -4,10 +4,8 @@ using MoonTools.Structs; namespace MoonTools.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.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 4dea095..6768463 100644 --- a/Bonk/MinkowskiDifference.cs +++ b/Bonk/MinkowskiDifference.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Numerics; using MoonTools.Structs; diff --git a/Bonk/MultiShape.cs b/Bonk/MultiShape.cs new file mode 100644 index 0000000..202120a --- /dev/null +++ b/Bonk/MultiShape.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Numerics; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public struct MultiShape : IHasAABB2D + { + public ImmutableArray<(IShape2D, Transform2D)> ShapeTransformPairs { get; } + + public AABB AABB { get; } + + public MultiShape(ImmutableArray<(IShape2D, Transform2D)> shapeTransformPairs) + { + ShapeTransformPairs = shapeTransformPairs; + + AABB = AABBFromShapes(shapeTransformPairs); + } + + public AABB TransformedAABB(Transform2D transform) + { + return AABB.Transformed(AABB, transform); + } + + /// + /// Moves the shapes by pivoting with an offset transform. + /// + /// + /// + public IEnumerable<(IShape2D, Transform2D)> TransformShapesUsingOffset(Transform2D offsetTransform) + { + foreach (var (shape, shapeTransform) in ShapeTransformPairs) + { + var newTransform = new Transform2D(Vector2.Transform(shapeTransform.Position, offsetTransform.TransformMatrix), offsetTransform.Rotation, offsetTransform.Scale); + yield return (shape, newTransform); + } + } + + public bool IsSingleShape() where T : struct, IShape2D + { + return ShapeTransformPairs.Length == 1 && ShapeTransformPairs[0].Item1 is T; + } + + public (T, Transform2D) ShapeTransformPair() where T : struct, IShape2D + { + return ((T, Transform2D))ShapeTransformPairs[0]; + } + + private static AABB AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms) + { + var minX = float.MaxValue; + var minY = float.MaxValue; + var maxX = float.MinValue; + var maxY = float.MinValue; + + foreach (var (shape, transform) in shapeTransforms) + { + var aabb = shape.TransformedAABB(transform); + + if (aabb.Min.X < minX) + { + minX = aabb.Min.X; + } + if (aabb.Min.Y < minY) + { + minY = aabb.Min.Y; + } + if (aabb.Max.X > maxX) + { + maxX = aabb.Max.X; + } + if (aabb.Max.Y > maxY) + { + maxY = aabb.Max.Y; + } + } + + return new AABB(minX, minY, maxX, maxY); + } + } +} diff --git a/Bonk/NarrowPhase/NarrowPhase.cs b/Bonk/NarrowPhase/NarrowPhase.cs index 8609947..4881955 100644 --- a/Bonk/NarrowPhase/NarrowPhase.cs +++ b/Bonk/NarrowPhase/NarrowPhase.cs @@ -14,6 +14,30 @@ namespace MoonTools.Bonk /// /// Tests if two shape-transform pairs are overlapping. Automatically detects fast-path optimizations. /// + public static bool TestCollision(IHasAABB2D hasBoundingBoxA, Transform2D transformA, IHasAABB2D hasBoundingBoxB, Transform2D transformB) + { + if (hasBoundingBoxA is MultiShape && hasBoundingBoxB is MultiShape) + { + return TestCollision((MultiShape)hasBoundingBoxA, transformA, (MultiShape)hasBoundingBoxB, transformB); + } + else if (hasBoundingBoxA is MultiShape && hasBoundingBoxB is IShape2D) + { + return TestCollision((MultiShape)hasBoundingBoxA, transformA, (IShape2D)hasBoundingBoxB, transformB); + } + else if (hasBoundingBoxA is IShape2D && hasBoundingBoxB is MultiShape) + { + return TestCollision((IShape2D)hasBoundingBoxA, transformA, (MultiShape)hasBoundingBoxB, transformB); + } + else if (hasBoundingBoxA is IShape2D && hasBoundingBoxB is IShape2D) + { + return TestCollision((IShape2D)hasBoundingBoxA, transformA, (IShape2D)hasBoundingBoxB, transformB); + } + else + { + throw new System.ArgumentException("Collision testing requires MultiShapes or IShape2Ds."); + } + } + public static bool TestCollision(IShape2D shapeA, Transform2D transformA, IShape2D shapeB, Transform2D transformB) { if (shapeA is Rectangle rectangleA && shapeB is Rectangle rectangleB && transformA.Rotation == 0 && transformB.Rotation == 0) @@ -35,6 +59,63 @@ namespace MoonTools.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(MultiShape multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform) + { + foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform)) + { + if (TestCollision(shape, shapeTransform, otherShape, 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, MultiShape multiShape, Transform2D multiShapeTransform) + { + foreach (var (otherShape, otherTransform) in multiShape.TransformShapesUsingOffset(multiShapeTransform)) + { + if (TestCollision(shape, shapeTransform, otherShape, 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(MultiShape multiShapeA, Transform2D transformA, MultiShape multiShapeB, Transform2D transformB) + { + foreach (var (shapeA, shapeTransformA) in multiShapeA.TransformShapesUsingOffset(transformA)) + { + foreach (var (shapeB, shapeTransformB) in multiShapeB.TransformShapesUsingOffset(transformB)) + { + if (TestCollision(shapeA, shapeTransformA, shapeB, 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 684f778..be8b7a8 100644 --- a/Bonk/Shapes/Polygon.cs +++ b/Bonk/Shapes/Polygon.cs @@ -59,7 +59,7 @@ namespace MoonTools.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.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 c78c40b..0dedfec 100644 --- a/Bonk/Shapes/Rectangle.cs +++ b/Bonk/Shapes/Rectangle.cs @@ -1,56 +1,41 @@ using System; -using System.Collections.Generic; using System.Numerics; using MoonTools.Structs; namespace MoonTools.Bonk { /// - /// A rectangle is a shape defined by a minimum and maximum X value and a minimum and maximum Y value. + /// A rectangle is a shape defined by a width and height. The origin is the center of the rectangle. /// 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 left, int top, 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; + Left = left; + Right = left + width; + Top = top; + Bottom = top + height; + AABB = new AABB(left, top, Right, 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.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.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 449845a..0000000 --- a/Bonk/Shapes/RectanglePolygonComparison.cs +++ /dev/null @@ -1,25 +0,0 @@ -using MoonTools.Structs; - -namespace MoonTools.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/Bonk/SweepTest/SweepResult.cs b/Bonk/SweepTest/SweepResult.cs new file mode 100644 index 0000000..d6dc84d --- /dev/null +++ b/Bonk/SweepTest/SweepResult.cs @@ -0,0 +1,21 @@ +using System; +using System.Numerics; + +namespace MoonTools.Core.Bonk +{ + public struct SweepResult where T : IEquatable + { + public static SweepResult False = new SweepResult(); + + public bool Hit { get; } + public Vector2 Motion { get; } + public T ID { get; } + + public SweepResult(bool hit, Vector2 motion, T id) + { + Hit = hit; + Motion = motion; + ID = id; + } + } +} diff --git a/Bonk/SweepTest/SweepTest.cs b/Bonk/SweepTest/SweepTest.cs new file mode 100644 index 0000000..cf17d37 --- /dev/null +++ b/Bonk/SweepTest/SweepTest.cs @@ -0,0 +1,134 @@ +using System; +using System.Numerics; +using MoonTools.Core.Structs; + +namespace MoonTools.Core.Bonk +{ + public static class SweepTest + { + /// + /// Performs a sweep test on and against rectangles. Returns the position 1 pixel before overlap occurs. + /// + /// + /// A spatial hash. + /// + /// A transform by which to transform the IHasAABB2D. + /// Given in world-space. + /// + public static SweepResult Test(SpatialHash spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable + { + var transformedAABB = rectangle.TransformedAABB(transform); + var sweepBox = SweepBox(transformedAABB, ray); + + var shortestDistance = float.MaxValue; + var nearestID = default(T); + Rectangle? nearestRectangle = null; + Transform2D? nearestTransform = null; + + foreach (var (id, shape, shapeTransform) in spatialHash.Retrieve(sweepBox)) + { + Rectangle otherRectangle; + Transform2D otherTransform; + AABB otherTransformedAABB; + if (shape is Rectangle) + { + otherRectangle = (Rectangle)shape; + otherTransformedAABB = shape.TransformedAABB(shapeTransform); + otherTransform = shapeTransform; + } + else if (shape is MultiShape multiShape && multiShape.IsSingleShape()) + { + Transform2D rectangleOffset; + (otherRectangle, rectangleOffset) = multiShape.ShapeTransformPair(); + otherTransform = shapeTransform.Compose(rectangleOffset); + otherTransformedAABB = shape.TransformedAABB(otherTransform); + } + else + { + continue; + } + + float xInvEntry, yInvEntry; + + if (ray.X > 0) + { + xInvEntry = otherTransformedAABB.Left - (transformedAABB.Right); + } + else + { + xInvEntry = (otherTransformedAABB.Right) - transformedAABB.Left; + } + + if (ray.Y > 0) + { + yInvEntry = otherTransformedAABB.Top - (transformedAABB.Bottom); + } + else + { + yInvEntry = (otherTransformedAABB.Bottom) - transformedAABB.Top; + } + + float xEntry, yEntry; + + if (ray.X == 0) + { + xEntry = float.MinValue; + } + else + { + xEntry = xInvEntry / ray.X; + } + + if (ray.Y == 0) + { + yEntry = float.MinValue; + } + else + { + yEntry = yInvEntry / ray.Y; + } + + var entryTime = Math.Max(xEntry, yEntry); + + if (entryTime >= 0 && entryTime <= 1) + { + if (entryTime < shortestDistance) + { + shortestDistance = entryTime; + nearestID = id; + nearestRectangle = otherRectangle; + nearestTransform = shapeTransform; + } + } + + } + + if (nearestRectangle.HasValue) + { + var overlapPosition = ray * shortestDistance; + var correctionX = -Math.Sign(ray.X); + var correctionY = -Math.Sign(ray.Y); + return new SweepResult(true, new Position2D((int)overlapPosition.X + correctionX, (int)overlapPosition.Y + correctionY), nearestID); + } + else + { + return SweepResult.False; + } + } + + public static SweepResult Test(SpatialHash spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable + { + return Test(spatialHash, new Rectangle(0, 0, 0, 0), transform, ray); + } + + private static AABB SweepBox(AABB aabb, Vector2 ray) + { + return new AABB( + Math.Min(aabb.Min.X, aabb.Min.X + ray.X), + Math.Min(aabb.Min.Y, aabb.Min.Y + ray.Y), + Math.Max(aabb.Max.X, aabb.Max.X + ray.X), + Math.Max(aabb.Max.Y, aabb.Max.Y + ray.Y) + ); + } + } +} diff --git a/Test/AABBTest.cs b/Test/AABBTest.cs index 345f694..e841c15 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 38751c3..278d354 100644 --- a/Test/EPA2DTest.cs +++ b/Test/EPA2DTest.cs @@ -13,9 +13,9 @@ namespace Tests [Test] public void RectangleOverlap() { - var squareA = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var squareA = new Rectangle(-1, -1, 2, 2); var transformA = Transform2D.DefaultTransform; - var squareB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var squareB = new Rectangle(-1, -1, 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.Bonk.Rectangle(-1, -1, 1, 1); + var square = new Rectangle(-1, -1, 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 ac57b4f..62665b4 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(-1, -1, 3, 3); + var b = new Rectangle(-1, -1, 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(-1, -1, 3, 3); + var b = new Rectangle(-1, -1, 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(-1, -1, 3, 3); + var b = new Rectangle(-3, -3, 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(-1, -1, 3, 3); + var b = new Rectangle(-3, -3, 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 80% rename from Test/GJK2DTest.cs rename to Test/NarrowPhaseTest.cs index ab1d84f..ed90965 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(-2, -2, 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(-2, -2, 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.Bonk.Rectangle(-6, -6, 6, 6); + var rectangleA = new Rectangle(-6, -6, 12, 12); var transformA = new Transform2D(new Position2D(39, 249)); - var rectangleB = new MoonTools.Bonk.Rectangle(0, 0, 16, 16); + var rectangleB = new Rectangle(-8, -8, 16, 16); var transformB = new Transform2D(new Position2D(16, 240)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse(); @@ -366,11 +366,11 @@ namespace Tests [Test] public void RotatedRectanglesOverlapping() { - var rectangleA = new MoonTools.Bonk.Rectangle(-1, -1, 2, 2); - var transformA = new Transform2D(new Vector2(-1, 0), -90f); + var rectangleA = new Rectangle(-1, -3, 3, 6); + var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2); - var rectangleB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); - var transformB = new Transform2D(new Vector2(1, 0)); + var rectangleB = new Rectangle(-1, -1, 2, 2); + var transformB = new Transform2D(new Vector2(0, 0)); NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); } @@ -378,10 +378,10 @@ namespace Tests [Test] public void RectanglesTouchingGJK2D() { - var rectangleA = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(-1, -1, 2, 2); var transformA = new Transform2D(new Position2D(-1, 0)); - var rectangleB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(-1, -1, 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.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(-1, -1, 2, 2); var transformA = new Transform2D(new Position2D(0, 0)); - var rectangleB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(-1, -1, 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.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(-1, -1, 2, 2); var transformA = new Transform2D(new Position2D(-1, 0)); - var rectangleB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleB = new Rectangle(-1, -1, 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.Bonk.Rectangle(-1, -1, 1, 1); + var rectangleA = new Rectangle(-1, -1, 2, 2); var transformA = new Transform2D(new Position2D(0, 0)); - var rectangleB = new MoonTools.Bonk.Rectangle(-1, -1, 1, 1); - var transformB = new Transform2D(new Vector2(1, 0)); + var rectangleB = new Rectangle(-1, -1, 2, 2); + var transformB = new Transform2D(new Vector2(1, 0), 0, new Vector2(-1, 1)); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); } + + [Test] + public void MultiRectanglesOverlapping() + { + var multiRectangleA = new MultiShape( + ImmutableArray.Create<(IShape2D, Transform2D)>( + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2))) + ) + ); + var transformA = new Transform2D(new Position2D(5, 0)); + + var multiRectangleB = new MultiShape( + ImmutableArray.Create<(IShape2D, Transform2D)>( + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))), + (new Rectangle(-2, 0, 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 MultiShape( + ImmutableArray.Create<(IShape2D, Transform2D)>( + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2))) + ) + ); + var transformA = new Transform2D(new Position2D(5, 0)); + + var multiRectangleB = new MultiShape( + ImmutableArray.Create<(IShape2D, Transform2D)>( + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))), + (new Rectangle(-2, 0, 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 83ce7b9..11e9a98 100644 --- a/Test/SpatialHashTest.cs +++ b/Test/SpatialHashTest.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using MoonTools.Structs; using MoonTools.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(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(-2, -2, 4, 4); var rectBTransform = new Transform2D(new Vector2(8, 8)); - var rectC = new Rectangle(-2, -2, 2, 2); + var rectC = new Rectangle(-2, -2, 4, 4); var rectCTransform = new Transform2D(new Vector2(24, -4)); - var rectD = new Rectangle(-2, -2, 2, 2); + var rectD = new Rectangle(-2, -2, 4, 4); var rectDTransform = new Transform2D(new Vector2(24, 24)); var circleA = new Circle(2); @@ -37,25 +38,39 @@ namespace Tests var point = new Point(); var pointTransform = new Transform2D(new Position2D(8, 8)); + var multiRectangle = new MultiShape( + ImmutableArray.Create<(IShape2D, Transform2D)>( + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -2))), + (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -1))), + (new Rectangle(-2, 0, 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)); + spatialHash.Retrieve(8, multiRectangle, multiRectangleTransform).Should().NotContain((0, rectA, rectATransform)); } [Test] @@ -63,13 +78,13 @@ namespace Tests { var spatialHash = new SpatialHash(16); - var rectA = new Rectangle(-2, -2, 2, 2); + var rectA = new Rectangle(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(-2, -2, 4, 4); var rectBTransform = new Transform2D(new Vector2(-8, -8)); - var rectC = new Rectangle(-1, -1, 1, 1); + var rectC = new Rectangle(-2, -2, 2, 2); var rectCTransform = new Transform2D(new Vector2(-8, -8)); spatialHash.Insert(0, rectA, rectATransform); @@ -84,10 +99,10 @@ namespace Tests { var spatialHash = new SpatialHash(16); - var rectA = new Rectangle(-2, -2, 2, 2); + var rectA = new Rectangle(-2, -2, 4, 4); var rectATransform = new Transform2D(new Vector2(-8, -8)); - var rectB = new Rectangle(-2, -2, 2, 2); + var rectB = new Rectangle(-2, -2, 4, 4); var rectBTransform = new Transform2D(new Vector2(8, 8)); spatialHash.Insert(0, rectA, rectATransform); diff --git a/Test/SweepTestTest.cs b/Test/SweepTestTest.cs new file mode 100644 index 0000000..c94c47a --- /dev/null +++ b/Test/SweepTestTest.cs @@ -0,0 +1,42 @@ +using System.Numerics; +using FluentAssertions; +using MoonTools.Core.Bonk; +using MoonTools.Core.Structs; +using NUnit.Framework; + +namespace Tests +{ + class SweepTestTest + { + [Test] + public void SweepsThrough() + { + var rectangle = new Rectangle(-2, -2, 4, 4); + var transform = new Transform2D(new Position2D(-6, 0)); + + var otherRectangle = new Rectangle(-2, -2, 4, 4); + var otherTransform = new Transform2D(new Position2D(6, 0)); + + var farthestRectangle = new Rectangle(-2, -2, 4, 4); + var farthestTransform = new Transform2D(new Position2D(12, 0)); + + var downRectangle = new Rectangle(-6, -2, 12, 4); + var downTransform = new Transform2D(new Position2D(-6, 20)); + + var spatialHash = new SpatialHash(16); + spatialHash.Insert(1, otherRectangle, otherTransform); + spatialHash.Insert(2, farthestRectangle, farthestTransform); + spatialHash.Insert(3, downRectangle, downTransform); + + SweepTest.Test(spatialHash, rectangle, transform, new Vector2(12, 0)).Should().Be( + new SweepResult(true, new Vector2(7, 0), 1) + ); + + SweepTest.Test(spatialHash, rectangle, transform, new Vector2(-12, 0)).Hit.Should().BeFalse(); + + SweepTest.Test(spatialHash, rectangle, transform, new Vector2(0, 20)).Should().Be( + new SweepResult(true, new Vector2(0, 15), 3) + ); + } + } +}