new multishape pattern

generics
Evan Hemsley 2020-01-02 01:40:03 -08:00
parent 9cbb1fdf31
commit b28196605e
15 changed files with 281 additions and 188 deletions

View File

@ -14,7 +14,7 @@ namespace MoonTools.Core.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 SpatialHash(int cellSize) public SpatialHash(int cellSize)
{ {
@ -32,7 +32,7 @@ namespace MoonTools.Core.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);
@ -57,7 +57,7 @@ namespace MoonTools.Core.Bonk
/// <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 minHash = Hash(box.Min);

17
Bonk/IHasAABB2D.cs Normal file
View File

@ -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);
}
}

10
Bonk/IMultiShape2D.cs Normal file
View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
public interface IMultiShape2D : IHasAABB2D
{
IEnumerable<(IShape2D, Transform2D)> ShapeTransformPairs { get; }
}
}

View File

@ -4,10 +4,8 @@ using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk namespace MoonTools.Core.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.Core.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);
} }
} }

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Core.Structs;

View File

@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
public struct MultiRectangle : IMultiShape2D
{
private ImmutableArray<(Rectangle, Transform2D)> Rectangles { get; }
public IEnumerable<(IShape2D, Transform2D)> ShapeTransformPairs
{
get
{
foreach (var rectangle in Rectangles) { yield return rectangle; }
}
}
public AABB AABB { get; }
public MultiRectangle(ImmutableArray<(Rectangle, Transform2D)> rectangles)
{
Rectangles = rectangles;
AABB = AABBFromRectangles(rectangles);
}
public AABB TransformedAABB(Transform2D transform)
{
return AABB.Transformed(AABB, transform);
}
private static AABB AABBFromRectangles(IEnumerable<(Rectangle, Transform2D)> rectangles)
{
var minX = float.MaxValue;
var minY = float.MaxValue;
var maxX = float.MinValue;
var maxY = float.MinValue;
foreach (var (rectangle, transform) in rectangles)
{
var transformedAABB = rectangle.TransformedAABB(transform);
if (transformedAABB.Min.X < minX)
{
minX = transformedAABB.Min.X;
}
if (transformedAABB.Min.Y < minY)
{
minY = transformedAABB.Min.Y;
}
if (transformedAABB.Max.X > maxX)
{
maxX = transformedAABB.Max.X;
}
if (transformedAABB.Max.Y > maxY)
{
maxY = transformedAABB.Max.Y;
}
}
return new AABB(minX, minY, maxX, maxY);
}
}
}

View File

@ -35,6 +35,63 @@ namespace MoonTools.Core.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(IMultiShape2D multiShape, Transform2D multiShapeTransform, IShape2D shape, Transform2D shapeTransform)
{
foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs)
{
if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(otherTransform))) { return true; }
}
return false;
}
/// <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, IMultiShape2D multiShape, Transform2D multiShapeTransform)
{
foreach (var (otherShape, otherTransform) in multiShape.ShapeTransformPairs)
{
if (TestCollision(shape, shapeTransform, otherShape, multiShapeTransform.Compose(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(IMultiShape2D multiShapeA, Transform2D transformA, IMultiShape2D multiShapeB, Transform2D transformB)
{
foreach (var (shapeA, shapeTransformA) in multiShapeA.ShapeTransformPairs)
{
foreach (var (shapeB, shapeTransformB) in multiShapeB.ShapeTransformPairs)
{
if (TestCollision(shapeA, transformA.Compose(shapeTransformA), shapeB, transformB.Compose(shapeTransformB))) { return true; }
}
}
return false;
}
/// <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>

View File

@ -59,7 +59,7 @@ namespace MoonTools.Core.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.Core.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);

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using MoonTools.Core.Structs; using MoonTools.Core.Structs;
@ -10,47 +9,33 @@ namespace MoonTools.Core.Bonk
/// </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 width, int height)
{ {
Min = new Position2D(minX, minY); Width = width;
Max = new Position2D(maxX, maxY); Height = height;
AABB = new AABB(minX, minY, maxX, maxY); AABB = new AABB(-width / 2f, -height / 2f, width / 2f, height / 2f);
Width = Max.X - Min.X; Right = AABB.Right;
Height = Max.Y - Min.Y; Left = AABB.Left;
Top = AABB.Top;
Bottom = AABB.Bottom;
BottomLeft = new Vector2(Left, Bottom);
TopRight = new Vector2(Top, Right);
Min = AABB.Min;
Max = AABB.Max;
} }
private Vector2 Support(Vector2 direction) private Vector2 Support(Vector2 direction)
@ -97,7 +82,7 @@ namespace MoonTools.Core.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.Core.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);

View File

@ -1,25 +0,0 @@
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
internal static class RectanglePolygonComparison
{
public static bool Equals(Polygon polygon, Rectangle rectangle)
{
if (polygon.VertexCount != 4) { return false; }
int? minIndex = null;
for (var i = 0; i < 4; i++)
{
if (polygon.Vertices[i] == rectangle.Min) { minIndex = i; break; }
}
if (!minIndex.HasValue) { return false; }
return
polygon.Vertices[(minIndex.Value + 1) % 4] == rectangle.TopRight &&
polygon.Vertices[(minIndex.Value + 2) % 4] == rectangle.Max &&
polygon.Vertices[(minIndex.Value + 3) % 4] == rectangle.BottomLeft;
}
}
}

View File

@ -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]

View File

@ -13,9 +13,9 @@ namespace Tests
[Test] [Test]
public void RectangleOverlap() public void RectangleOverlap()
{ {
var squareA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var squareA = new Rectangle(2, 2);
var transformA = Transform2D.DefaultTransform; var transformA = Transform2D.DefaultTransform;
var squareB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var squareB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1.5f, 0)); var 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.Core.Bonk.Rectangle(-1, -1, 1, 1); var square = new Rectangle(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);

View File

@ -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(3, 3);
var b = new Rectangle(0, 0, 3, 3); var b = new Rectangle(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(3, 3);
var b = new Rectangle(0, 0, 3, 3); var b = new Rectangle(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(3, 3);
var b = new Rectangle(-1, -1, 5, 5); var b = new Rectangle(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(3, 3);
var b = new Rectangle(-1, -1, 5, 5); var b = new Rectangle(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

View File

@ -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(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(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.Core.Bonk.Rectangle(-6, -6, 6, 6); var rectangleA = new Rectangle(12, 12);
var transformA = new Transform2D(new Position2D(39, 249)); var transformA = new Transform2D(new Position2D(39, 249));
var rectangleB = new MoonTools.Core.Bonk.Rectangle(0, 0, 16, 16); var rectangleB = new Rectangle(16, 16);
var transformB = new Transform2D(new Position2D(16, 240)); 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,10 +366,10 @@ namespace Tests
[Test] [Test]
public void RotatedRectanglesOverlapping() public void RotatedRectanglesOverlapping()
{ {
var rectangleA = new MoonTools.Core.Bonk.Rectangle(-1, -1, 2, 2); var rectangleA = new Rectangle(3, 3);
var transformA = new Transform2D(new Vector2(-1, 0), -90f); var transformA = new Transform2D(new Vector2(-1, 0), -90f);
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 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.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleA = new Rectangle(2, 2);
var transformA = new Transform2D(new Position2D(-1, 0)); var transformA = new Transform2D(new Position2D(-1, 0));
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); 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.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleA = new Rectangle(2, 2);
var transformA = new Transform2D(new Position2D(0, 0)); var transformA = new Transform2D(new Position2D(0, 0));
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); 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.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleA = new Rectangle(2, 2);
var transformA = new Transform2D(new Position2D(-1, 0)); var transformA = new Transform2D(new Position2D(-1, 0));
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); 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.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleA = new Rectangle(2, 2);
var transformA = new Transform2D(new Position2D(0, 0)); var transformA = new Transform2D(new Position2D(0, 0));
var rectangleB = new MoonTools.Core.Bonk.Rectangle(-1, -1, 1, 1); var rectangleB = new Rectangle(2, 2);
var transformB = new Transform2D(new Vector2(1, 0)); var transformB = new Transform2D(new Vector2(1, 0));
NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue(); NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
} }
[Test]
public void MultiRectanglesOverlapping()
{
var multiRectangleA = new MultiRectangle(
ImmutableArray.Create(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2)))
)
);
var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiRectangle(
ImmutableArray.Create(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1)))
)
);
var transformB = new Transform2D(new Position2D(0, 3));
NarrowPhase.TestCollision(multiRectangleA, transformA, multiRectangleB, transformB).Should().BeTrue();
}
[Test]
public void MultiRectanglesNotOverlapping()
{
var multiRectangleA = new MultiRectangle(
ImmutableArray.Create(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2)))
)
);
var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiRectangle(
ImmutableArray.Create(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1)))
)
);
var transformB = new Transform2D(new Position2D(0, -3));
NarrowPhase.TestCollision(multiRectangleA, transformA, multiRectangleB, transformB).Should().BeFalse();
}
} }
} }

View File

@ -3,6 +3,7 @@ using NUnit.Framework;
using MoonTools.Core.Structs; using MoonTools.Core.Structs;
using MoonTools.Core.Bonk; using MoonTools.Core.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(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(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(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(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,38 @@ 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 MultiRectangle(
ImmutableArray.Create(
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -2))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, 0)))
)
);
var multiRectangleTransform = new Transform2D(new Position2D(8, 8));
spatialHash.Insert(0, rectA, rectATransform); spatialHash.Insert(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));
} }
[Test] [Test]
@ -63,13 +77,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(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(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);
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 +98,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(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(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);