rectangles now define top-left-width-height so that we dont have subpixel bounds

multishape
Evan Hemsley 2020-01-06 20:06:21 -08:00
parent 20df172027
commit 1fb768e7b5
7 changed files with 60 additions and 60 deletions

View File

@ -23,15 +23,15 @@ namespace MoonTools.Core.Bonk
public Vector2 Min { get; } public Vector2 Min { get; }
public Vector2 Max { get; } public Vector2 Max { get; }
public Rectangle(int width, int height) public Rectangle(int left, int top, int width, int height)
{ {
Width = width; Width = width;
Height = height; Height = height;
AABB = new AABB(-width / 2f, -height / 2f, width / 2f, height / 2f); Left = left;
Right = AABB.Right; Right = left + width;
Left = AABB.Left; Top = top;
Top = AABB.Top; Bottom = top + height;
Bottom = AABB.Bottom; AABB = new AABB(left, top, Right, Bottom);
BottomLeft = new Vector2(Left, Bottom); BottomLeft = new Vector2(Left, Bottom);
TopRight = new Vector2(Top, Right); TopRight = new Vector2(Top, Right);
Min = AABB.Min; Min = AABB.Min;

View File

@ -118,7 +118,7 @@ namespace MoonTools.Core.Bonk
public static SweepResult<T> Test<T>(SpatialHash<T> spatialHash, Point point, Transform2D transform, Vector2 ray) where T : IEquatable<T> 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), transform, ray); return Test(spatialHash, new Rectangle(0, 0, 0, 0), transform, ray);
} }
private static AABB SweepBox(AABB aabb, Vector2 ray) private static AABB SweepBox(AABB aabb, Vector2 ray)

View File

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

View File

@ -140,8 +140,8 @@ namespace Tests
[Test] [Test]
public void RectangleEqual() public void RectangleEqual()
{ {
var a = new Rectangle(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(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(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(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(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(6, 6); 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(3, 3); var a = new Rectangle(-1, -1, 3, 3);
var b = new Rectangle(6, 6); var b = new Rectangle(-3, -3, 6, 6);
(a != b).Should().BeTrue(); (a != b).Should().BeTrue();
} }

View File

@ -55,7 +55,7 @@ namespace Tests
public void PointRectangleOverlapping() public void PointRectangleOverlapping()
{ {
var point = new Point(); var point = new Point();
var rectangle = new Rectangle(4, 4); 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(4, 4); 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 Rectangle(12, 12); 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 Rectangle(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,10 +366,10 @@ namespace Tests
[Test] [Test]
public void RotatedRectanglesOverlapping() public void RotatedRectanglesOverlapping()
{ {
var rectangleA = new Rectangle(3, 6); var rectangleA = new Rectangle(-1, -3, 3, 6);
var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2); var transformA = new Transform2D(new Vector2(4f, 0), (float)System.Math.PI / 2);
var rectangleB = new Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(0, 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 Rectangle(2, 2); 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 Rectangle(2, 2); 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 Rectangle(2, 2); 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 Rectangle(2, 2); 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 Rectangle(2, 2); 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 Rectangle(2, 2); 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,10 +414,10 @@ namespace Tests
[Test] [Test]
public void RectanglesOverlappingOverlap() public void RectanglesOverlappingOverlap()
{ {
var rectangleA = new Rectangle(2, 2); 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 Rectangle(2, 2); var rectangleB = new Rectangle(-1, -1, 2, 2);
var transformB = new Transform2D(new Vector2(1, 0), 0, new Vector2(-1, 1)); 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();
@ -428,18 +428,18 @@ namespace Tests
{ {
var multiRectangleA = new MultiShape( var multiRectangleA = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2)))
) )
); );
var transformA = new Transform2D(new Position2D(5, 0)); var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiShape( var multiRectangleB = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 1)))
) )
); );
var transformB = new Transform2D(new Position2D(0, 3)); var transformB = new Transform2D(new Position2D(0, 3));
@ -452,18 +452,18 @@ namespace Tests
{ {
var multiRectangleA = new MultiShape( var multiRectangleA = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-5, 2))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-5, 2)))
) )
); );
var transformA = new Transform2D(new Position2D(5, 0)); var transformA = new Transform2D(new Position2D(5, 0));
var multiRectangleB = new MultiShape( var multiRectangleB = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(4, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 0))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 0))),
(new Rectangle(4, 1), new Transform2D(new Position2D(4, 1))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(4, 1)))
) )
); );
var transformB = new Transform2D(new Position2D(0, -3)); var transformB = new Transform2D(new Position2D(0, -3));

View File

@ -14,16 +14,16 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); 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(4, 4); 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(4, 4); 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(4, 4); 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);
@ -40,9 +40,9 @@ namespace Tests
var multiRectangle = new MultiShape( var multiRectangle = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>( ImmutableArray.Create<(IShape2D, Transform2D)>(
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -2))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -2))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -1))), (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, 0))) (new Rectangle(-2, 0, 4, 1), new Transform2D(new Position2D(-2, 0)))
) )
); );
var multiRectangleTransform = new Transform2D(new Position2D(8, 8)); var multiRectangleTransform = new Transform2D(new Position2D(8, 8));
@ -78,13 +78,13 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); 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(4, 4); 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); 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);
@ -99,10 +99,10 @@ namespace Tests
{ {
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);
var rectA = new Rectangle(4, 4); 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(4, 4); 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);

View File

@ -11,16 +11,16 @@ namespace Tests
[Test] [Test]
public void SweepsThrough() public void SweepsThrough()
{ {
var rectangle = new Rectangle(4, 4); var rectangle = new Rectangle(-2, -2, 4, 4);
var transform = new Transform2D(new Position2D(-6, 0)); var transform = new Transform2D(new Position2D(-6, 0));
var otherRectangle = new Rectangle(4, 4); var otherRectangle = new Rectangle(-2, -2, 4, 4);
var otherTransform = new Transform2D(new Position2D(6, 0)); var otherTransform = new Transform2D(new Position2D(6, 0));
var farthestRectangle = new Rectangle(4, 4); var farthestRectangle = new Rectangle(-2, -2, 4, 4);
var farthestTransform = new Transform2D(new Position2D(12, 0)); var farthestTransform = new Transform2D(new Position2D(12, 0));
var downRectangle = new Rectangle(12, 4); var downRectangle = new Rectangle(-6, -2, 12, 4);
var downTransform = new Transform2D(new Position2D(-6, 20)); var downTransform = new Transform2D(new Position2D(-6, 20));
var spatialHash = new SpatialHash<int>(16); var spatialHash = new SpatialHash<int>(16);