Merge remote-tracking branch 'github/multishape'
						commit
						bcc0d0abc2
					
				|  | @ -14,7 +14,13 @@ namespace MoonTools.Bonk | ||||||
|         private readonly int cellSize; |         private readonly int cellSize; | ||||||
| 
 | 
 | ||||||
|         private readonly Dictionary<long, HashSet<T>> hashDictionary = new Dictionary<long, HashSet<T>>(); |         private readonly Dictionary<long, HashSet<T>> hashDictionary = new Dictionary<long, HashSet<T>>(); | ||||||
|         private readonly Dictionary<T, (IShape2D, Transform2D)> IDLookup = new Dictionary<T, (IShape2D, Transform2D)>(); |         private readonly Dictionary<T, (IHasAABB2D, Transform2D)> IDLookup = new Dictionary<T, (IHasAABB2D, Transform2D)>(); | ||||||
|  | 
 | ||||||
|  |         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) |         public SpatialHash(int cellSize) | ||||||
|         { |         { | ||||||
|  | @ -32,7 +38,7 @@ namespace MoonTools.Bonk | ||||||
|         /// <param name="id">A unique ID for the shape-transform pair.</param> |         /// <param name="id">A unique ID for the shape-transform pair.</param> | ||||||
|         /// <param name="shape"></param> |         /// <param name="shape"></param> | ||||||
|         /// <param name="transform2D"></param> |         /// <param name="transform2D"></param> | ||||||
|         public void Insert(T id, IShape2D shape, Transform2D transform2D) |         public void Insert(T id, IHasAABB2D shape, Transform2D transform2D) | ||||||
|         { |         { | ||||||
|             var box = shape.TransformedAABB(transform2D); |             var box = shape.TransformedAABB(transform2D); | ||||||
|             var minHash = Hash(box.Min); |             var minHash = Hash(box.Min); | ||||||
|  | @ -52,20 +58,30 @@ namespace MoonTools.Bonk | ||||||
|                     IDLookup[id] = (shape, transform2D); |                     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); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID. |         /// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         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 box = shape.TransformedAABB(transform2D); | ||||||
|             var minHash = Hash(box.Min); |             var (minX, minY) = Hash(box.Min); | ||||||
|             var maxHash = Hash(box.Max); |             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); |                     var key = MakeLong(i, j); | ||||||
|                     if (hashDictionary.ContainsKey(key)) |                     if (hashDictionary.ContainsKey(key)) | ||||||
|  | @ -73,7 +89,43 @@ namespace MoonTools.Bonk | ||||||
|                         foreach (var t in hashDictionary[key]) |                         foreach (var t in hashDictionary[key]) | ||||||
|                         { |                         { | ||||||
|                             var (otherShape, otherTransform) = IDLookup[t]; |                             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); | ||||||
|  |                             } | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Retrieves objects based on a pre-transformed AABB. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="aabb">A transformed AABB.</param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         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); |                                 yield return (t, otherShape, otherTransform); | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,17 @@ | ||||||
|  | using System; | ||||||
|  | using MoonTools.Core.Structs; | ||||||
|  | 
 | ||||||
|  | namespace MoonTools.Core.Bonk | ||||||
|  | { | ||||||
|  |     public interface IHasAABB2D | ||||||
|  |     { | ||||||
|  |         AABB AABB { get; } | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Returns a bounding box based on the shape. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="transform">A Transform for transforming the shape vertices.</param> | ||||||
|  |         /// <returns>Returns a bounding box based on the shape.</returns> | ||||||
|  |         AABB TransformedAABB(Transform2D transform); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -4,10 +4,8 @@ using MoonTools.Structs; | ||||||
| 
 | 
 | ||||||
| namespace MoonTools.Bonk | namespace MoonTools.Bonk | ||||||
| { | { | ||||||
|     public interface IShape2D : IEquatable<IShape2D> |     public interface IShape2D : IHasAABB2D, IEquatable<IShape2D> | ||||||
|     { |     { | ||||||
|         AABB AABB { get; } |  | ||||||
| 
 |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// A Minkowski support function. Gives the farthest point on the edge of a shape along the given direction. |         /// A Minkowski support function. Gives the farthest point on the edge of a shape along the given direction. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  | @ -15,12 +13,5 @@ namespace MoonTools.Bonk | ||||||
|         /// <param name="transform">A Transform for transforming the shape vertices.</param> |         /// <param name="transform">A Transform for transforming the shape vertices.</param> | ||||||
|         /// <returns>The farthest point on the edge of the shape along the given direction.</returns> |         /// <returns>The farthest point on the edge of the shape along the given direction.</returns> | ||||||
|         Vector2 Support(Vector2 direction, Transform2D transform); |         Vector2 Support(Vector2 direction, Transform2D transform); | ||||||
| 
 |  | ||||||
|         /// <summary> |  | ||||||
|         /// Returns a bounding box based on the shape. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="transform">A Transform for transforming the shape vertices.</param> |  | ||||||
|         /// <returns>Returns a bounding box based on the shape.</returns> |  | ||||||
|         AABB TransformedAABB(Transform2D transform); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| using System; | using System; | ||||||
| using System.Numerics; | using System.Numerics; | ||||||
| using MoonTools.Structs; | using MoonTools.Structs; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -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); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// Moves the shapes by pivoting with an offset transform.  | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="offsetTransform"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         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<T>() where T : struct, IShape2D | ||||||
|  |         { | ||||||
|  |             return ShapeTransformPairs.Length == 1 && ShapeTransformPairs[0].Item1 is T; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public (T, Transform2D) ShapeTransformPair<T>() 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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -14,6 +14,30 @@ namespace MoonTools.Bonk | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Tests if two shape-transform pairs are overlapping. Automatically detects fast-path optimizations. |         /// Tests if two shape-transform pairs are overlapping. Automatically detects fast-path optimizations. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  |         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) |         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) |             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; |             return FindCollisionSimplex(shapeA, transformA, shapeB, transformB).Item1; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// 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. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="multiShape"></param> | ||||||
|  |         /// <param name="multiShapeTransform"></param> | ||||||
|  |         /// <param name="shape"></param> | ||||||
|  |         /// <param name="shapeTransform"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// 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. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="multiShape"></param> | ||||||
|  |         /// <param name="multiShapeTransform"></param> | ||||||
|  |         /// <param name="shape"></param> | ||||||
|  |         /// <param name="shapeTransform"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         /// <summary> | ||||||
|  |         /// 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. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="multiShapeA"></param> | ||||||
|  |         /// <param name="transformA"></param> | ||||||
|  |         /// <param name="multiShapeB"></param> | ||||||
|  |         /// <param name="transformB"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Fast path for axis-aligned rectangles. If the transforms have non-zero rotation this will be inaccurate. |         /// Fast path for axis-aligned rectangles. If the transforms have non-zero rotation this will be inaccurate. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  |  | ||||||
|  | @ -59,7 +59,7 @@ namespace MoonTools.Bonk | ||||||
| 
 | 
 | ||||||
|         public bool Equals(IShape2D other) |         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) |         public bool Equals(Polygon other) | ||||||
|  | @ -82,11 +82,6 @@ namespace MoonTools.Bonk | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool Equals(Rectangle rectangle) |  | ||||||
|         { |  | ||||||
|             return RectanglePolygonComparison.Equals(this, rectangle); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public override int GetHashCode() |         public override int GetHashCode() | ||||||
|         { |         { | ||||||
|             return HashCode.Combine(Vertices); |             return HashCode.Combine(Vertices); | ||||||
|  |  | ||||||
|  | @ -1,56 +1,41 @@ | ||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Numerics; | using System.Numerics; | ||||||
| using MoonTools.Structs; | using MoonTools.Structs; | ||||||
| 
 | 
 | ||||||
| namespace MoonTools.Bonk | namespace MoonTools.Bonk | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// 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. | ||||||
|     /// </summary> |     /// </summary> | ||||||
|     public struct Rectangle : IShape2D, IEquatable<Rectangle> |     public struct Rectangle : IShape2D, IEquatable<Rectangle> | ||||||
|     { |     { | ||||||
|         /// <summary> |  | ||||||
|         /// The minimum position of the rectangle. Note that we assume y-down coordinates. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <value></value> |  | ||||||
|         public Position2D Min { get; } |  | ||||||
|         /// <summary> |  | ||||||
|         /// The maximum position of the rectangle. Note that we assume y-down coordinates. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <value></value> |  | ||||||
|         public Position2D Max { get; } |  | ||||||
|         public AABB AABB { 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 Width { get; } | ||||||
|         public int Height { get; } |         public int Height { get; } | ||||||
| 
 | 
 | ||||||
|         public Position2D TopRight { get { return new Position2D(Right, Top); } } |         public float Right { get; } | ||||||
|         public Position2D BottomLeft { get { return new Position2D(Left, Bottom); } } |         public float Left { get; } | ||||||
|  |         public float Top { get; } | ||||||
|  |         public float Bottom { get; } | ||||||
|  |         public Vector2 BottomLeft { get; } | ||||||
|  |         public Vector2 TopRight { get; } | ||||||
| 
 | 
 | ||||||
|         public IEnumerable<Position2D> Vertices |         public Vector2 Min { get; } | ||||||
|         { |         public Vector2 Max { get; } | ||||||
|             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 Rectangle(int minX, int minY, int maxX, int maxY) |         public Rectangle(int left, int top, int width, int height) | ||||||
|         { |         { | ||||||
|             Min = new Position2D(minX, minY); |             Width = width; | ||||||
|             Max = new Position2D(maxX, maxY); |             Height = height; | ||||||
|             AABB = new AABB(minX, minY, maxX, maxY); |             Left = left; | ||||||
|             Width = Max.X - Min.X; |             Right = left + width; | ||||||
|             Height = Max.Y - Min.Y; |             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) |         private Vector2 Support(Vector2 direction) | ||||||
|  | @ -97,7 +82,7 @@ namespace MoonTools.Bonk | ||||||
| 
 | 
 | ||||||
|         public bool Equals(IShape2D other) |         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) |         public bool Equals(Rectangle other) | ||||||
|  | @ -105,11 +90,6 @@ namespace MoonTools.Bonk | ||||||
|             return Min == other.Min && Max == other.Max; |             return Min == other.Min && Max == other.Max; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool Equals(Polygon other) |  | ||||||
|         { |  | ||||||
|             return RectanglePolygonComparison.Equals(other, this); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public override int GetHashCode() |         public override int GetHashCode() | ||||||
|         { |         { | ||||||
|             return HashCode.Combine(Min, Max); |             return HashCode.Combine(Min, Max); | ||||||
|  |  | ||||||
|  | @ -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; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  | @ -0,0 +1,21 @@ | ||||||
|  | using System; | ||||||
|  | using System.Numerics; | ||||||
|  | 
 | ||||||
|  | namespace MoonTools.Core.Bonk | ||||||
|  | { | ||||||
|  |     public struct SweepResult<T> where T : IEquatable<T> | ||||||
|  |     { | ||||||
|  |         public static SweepResult<T> False = new SweepResult<T>(); | ||||||
|  | 
 | ||||||
|  |         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; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,134 @@ | ||||||
|  | using System; | ||||||
|  | using System.Numerics; | ||||||
|  | using MoonTools.Core.Structs; | ||||||
|  | 
 | ||||||
|  | namespace MoonTools.Core.Bonk | ||||||
|  | { | ||||||
|  |     public static class SweepTest | ||||||
|  |     { | ||||||
|  |         /// <summary> | ||||||
|  |         /// Performs a sweep test on and against rectangles. Returns the position 1 pixel before overlap occurs. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <typeparam name="T"></typeparam> | ||||||
|  |         /// <param name="spatialHash">A spatial hash.</param> | ||||||
|  |         /// <param name="rectangle"></param> | ||||||
|  |         /// <param name="transform">A transform by which to transform the IHasAABB2D.</param> | ||||||
|  |         /// <param name="ray">Given in world-space.</param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Rectangle rectangle, Transform2D transform, Vector2 ray) where T : IEquatable<T> | ||||||
|  |         { | ||||||
|  |             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<Rectangle>()) | ||||||
|  |                 { | ||||||
|  |                     Transform2D rectangleOffset; | ||||||
|  |                     (otherRectangle, rectangleOffset) = multiShape.ShapeTransformPair<Rectangle>(); | ||||||
|  |                     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<T>(true, new Position2D((int)overlapPosition.X + correctionX, (int)overlapPosition.Y + correctionY), nearestID); | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 return SweepResult<T>.False; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable<T> | ||||||
|  |         { | ||||||
|  |             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) | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -14,6 +14,11 @@ namespace Tests | ||||||
|             var b = new AABB(new Vector2(0, 0), new Vector2(2, 2)); |             var b = new AABB(new Vector2(0, 0), new Vector2(2, 2)); | ||||||
| 
 | 
 | ||||||
|             AABB.TestOverlap(a, b).Should().BeTrue(); |             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] |         [Test] | ||||||
|  |  | ||||||
|  | @ -13,9 +13,9 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectangleOverlap() |         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 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 transformB = new Transform2D(new Vector2(1.5f, 0)); | ||||||
| 
 | 
 | ||||||
|             var (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB); |             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 line = new Line(new Position2D(-4, -4), new Position2D(4, 4)); | ||||||
|             var transformA = Transform2D.DefaultTransform; |             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 transformB = Transform2D.DefaultTransform; | ||||||
| 
 | 
 | ||||||
|             var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB); |             var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB); | ||||||
|  |  | ||||||
|  | @ -140,8 +140,8 @@ namespace Tests | ||||||
|             [Test] |             [Test] | ||||||
|             public void RectangleEqual() |             public void RectangleEqual() | ||||||
|             { |             { | ||||||
|                 var a = new Rectangle(0, 0, 3, 3); |                 var a = new Rectangle(-1, -1, 3, 3); | ||||||
|                 var b = new Rectangle(0, 0, 3, 3); |                 var b = new Rectangle(-1, -1, 3, 3); | ||||||
| 
 | 
 | ||||||
|                 a.Equals(b).Should().BeTrue(); |                 a.Equals(b).Should().BeTrue(); | ||||||
|             } |             } | ||||||
|  | @ -149,8 +149,8 @@ namespace Tests | ||||||
|             [Test] |             [Test] | ||||||
|             public void RectangleEqualOperator() |             public void RectangleEqualOperator() | ||||||
|             { |             { | ||||||
|                 var a = new Rectangle(0, 0, 3, 3); |                 var a = new Rectangle(-1, -1, 3, 3); | ||||||
|                 var b = new Rectangle(0, 0, 3, 3); |                 var b = new Rectangle(-1, -1, 3, 3); | ||||||
| 
 | 
 | ||||||
|                 (a == b).Should().BeTrue(); |                 (a == b).Should().BeTrue(); | ||||||
|             } |             } | ||||||
|  | @ -158,8 +158,8 @@ namespace Tests | ||||||
|             [Test] |             [Test] | ||||||
|             public void RectangleNotEqual() |             public void RectangleNotEqual() | ||||||
|             { |             { | ||||||
|                 var a = new Rectangle(0, 0, 3, 3); |                 var a = new Rectangle(-1, -1, 3, 3); | ||||||
|                 var b = new Rectangle(-1, -1, 5, 5); |                 var b = new Rectangle(-3, -3, 6, 6); | ||||||
| 
 | 
 | ||||||
|                 a.Equals(b).Should().BeFalse(); |                 a.Equals(b).Should().BeFalse(); | ||||||
|             } |             } | ||||||
|  | @ -167,8 +167,8 @@ namespace Tests | ||||||
|             [Test] |             [Test] | ||||||
|             public void RectangleNotEqualOperator() |             public void RectangleNotEqualOperator() | ||||||
|             { |             { | ||||||
|                 var a = new Rectangle(0, 0, 3, 3); |                 var a = new Rectangle(-1, -1, 3, 3); | ||||||
|                 var b = new Rectangle(-1, -1, 5, 5); |                 var b = new Rectangle(-3, -3, 6, 6); | ||||||
| 
 | 
 | ||||||
|                 (a != b).Should().BeTrue(); |                 (a != b).Should().BeTrue(); | ||||||
|             } |             } | ||||||
|  | @ -283,70 +283,6 @@ namespace Tests | ||||||
| 
 | 
 | ||||||
|                 (a != b).Should().BeTrue(); |                 (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 |         public class SimplexTests | ||||||
|  |  | ||||||
|  | @ -7,7 +7,7 @@ using System.Collections.Immutable; | ||||||
| 
 | 
 | ||||||
| namespace Tests | namespace Tests | ||||||
| { | { | ||||||
|     public class GJK2DTest |     public class NarrowPhaseTest | ||||||
|     { |     { | ||||||
|         [Test] |         [Test] | ||||||
|         public void PointLineOverlapping() |         public void PointLineOverlapping() | ||||||
|  | @ -55,7 +55,7 @@ namespace Tests | ||||||
|         public void PointRectangleOverlapping() |         public void PointRectangleOverlapping() | ||||||
|         { |         { | ||||||
|             var point = new Point(); |             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(); |             NarrowPhase.TestCollision(point, Transform2D.DefaultTransform, rectangle, Transform2D.DefaultTransform).Should().BeTrue(); | ||||||
|         } |         } | ||||||
|  | @ -65,7 +65,7 @@ namespace Tests | ||||||
|         { |         { | ||||||
|             var point = new Point(); |             var point = new Point(); | ||||||
|             var pointTransform = new Transform2D(new Position2D(5, 5)); |             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(); |             NarrowPhase.TestCollision(point, pointTransform, rectangle, Transform2D.DefaultTransform).Should().BeFalse(); | ||||||
|         } |         } | ||||||
|  | @ -354,10 +354,10 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectanglesNotOverlapping() |         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 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)); |             var transformB = new Transform2D(new Position2D(16, 240)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse(); |             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse(); | ||||||
|  | @ -366,11 +366,11 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RotatedRectanglesOverlapping() |         public void RotatedRectanglesOverlapping() | ||||||
|         { |         { | ||||||
|             var rectangleA = new MoonTools.Bonk.Rectangle(-1, -1, 2, 2); |             var rectangleA = new Rectangle(-1, -3, 3, 6); | ||||||
|             var transformA = new Transform2D(new Vector2(-1, 0), -90f); |             var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2); | ||||||
| 
 | 
 | ||||||
|             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)); |             var transformB = new Transform2D(new Vector2(0, 0)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); |             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); | ||||||
|         } |         } | ||||||
|  | @ -378,10 +378,10 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectanglesTouchingGJK2D() |         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 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)); |             var transformB = new Transform2D(new Vector2(1, 0)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); |             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); | ||||||
|  | @ -390,10 +390,10 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectanglesOverlappingGJK2D() |         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 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)); |             var transformB = new Transform2D(new Vector2(1, 0)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); |             NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); | ||||||
|  | @ -402,10 +402,10 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectanglesTouchingOverlap() |         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 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)); |             var transformB = new Transform2D(new Vector2(1, 0)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); |             NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); | ||||||
|  | @ -414,13 +414,61 @@ namespace Tests | ||||||
|         [Test] |         [Test] | ||||||
|         public void RectanglesOverlappingOverlap() |         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 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)); |             var transformB = new Transform2D(new Vector2(1, 0), 0, new Vector2(-1, 1)); | ||||||
| 
 | 
 | ||||||
|             NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); |             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(); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -3,6 +3,7 @@ using NUnit.Framework; | ||||||
| using MoonTools.Structs; | using MoonTools.Structs; | ||||||
| using MoonTools.Bonk; | using MoonTools.Bonk; | ||||||
| using System.Numerics; | using System.Numerics; | ||||||
|  | using System.Collections.Immutable; | ||||||
| 
 | 
 | ||||||
| namespace Tests | namespace Tests | ||||||
| { | { | ||||||
|  | @ -13,16 +14,16 @@ namespace Tests | ||||||
|         { |         { | ||||||
|             var spatialHash = new SpatialHash<int>(16); |             var spatialHash = new SpatialHash<int>(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 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 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 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 rectDTransform = new Transform2D(new Vector2(24, 24)); | ||||||
| 
 | 
 | ||||||
|             var circleA = new Circle(2); |             var circleA = new Circle(2); | ||||||
|  | @ -37,25 +38,39 @@ namespace Tests | ||||||
|             var point = new Point(); |             var point = new Point(); | ||||||
|             var pointTransform = new Transform2D(new Position2D(8, 8)); |             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(0, rectA, rectATransform); | ||||||
|             spatialHash.Insert(1, rectB, rectBTransform); |             spatialHash.Insert(1, rectB, rectBTransform); | ||||||
|             spatialHash.Insert(2, rectC, rectCTransform); |             spatialHash.Insert(2, rectC, rectCTransform); | ||||||
|             spatialHash.Insert(3, rectD, rectDTransform); |             spatialHash.Insert(3, rectD, rectDTransform); | ||||||
|             spatialHash.Insert(4, circleA, circleATransform); |             spatialHash.Insert(4, circleA, circleATransform); | ||||||
|             spatialHash.Insert(1, circleB, circleBTransform); |             spatialHash.Insert(9, circleB, circleBTransform); | ||||||
|             spatialHash.Insert(6, line, lineTransform); |             spatialHash.Insert(6, line, lineTransform); | ||||||
|             spatialHash.Insert(7, point, pointTransform); |             spatialHash.Insert(7, point, pointTransform); | ||||||
|  |             spatialHash.Insert(8, multiRectangle, multiRectangleTransform); | ||||||
| 
 | 
 | ||||||
|             spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty(); |             spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty(); | ||||||
|             spatialHash.Retrieve(1, rectB, rectBTransform).Should().NotContain((1, circleB, circleBTransform)); |             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((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(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(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(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(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] |         [Test] | ||||||
|  | @ -63,13 +78,13 @@ namespace Tests | ||||||
|         { |         { | ||||||
|             var spatialHash = new SpatialHash<int>(16); |             var spatialHash = new SpatialHash<int>(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 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 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)); |             var rectCTransform = new Transform2D(new Vector2(-8, -8)); | ||||||
| 
 | 
 | ||||||
|             spatialHash.Insert(0, rectA, rectATransform); |             spatialHash.Insert(0, rectA, rectATransform); | ||||||
|  | @ -84,10 +99,10 @@ namespace Tests | ||||||
|         { |         { | ||||||
|             var spatialHash = new SpatialHash<int>(16); |             var spatialHash = new SpatialHash<int>(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 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 rectBTransform = new Transform2D(new Vector2(8, 8)); | ||||||
| 
 | 
 | ||||||
|             spatialHash.Insert(0, rectA, rectATransform); |             spatialHash.Insert(0, rectA, rectATransform); | ||||||
|  |  | ||||||
|  | @ -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<int>(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<int>(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<int>(true, new Vector2(0, 15), 3) | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue