forked from MoonsideGames/MoonTools.Bonk
rectangles now define top-left-width-height so that we dont have subpixel bounds
parent
20df172027
commit
1fb768e7b5
|
@ -23,15 +23,15 @@ namespace MoonTools.Core.Bonk
|
|||
public Vector2 Min { get; }
|
||||
public Vector2 Max { get; }
|
||||
|
||||
public Rectangle(int width, int height)
|
||||
public Rectangle(int left, int top, int width, int height)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
AABB = new AABB(-width / 2f, -height / 2f, width / 2f, height / 2f);
|
||||
Right = AABB.Right;
|
||||
Left = AABB.Left;
|
||||
Top = AABB.Top;
|
||||
Bottom = AABB.Bottom;
|
||||
Left = left;
|
||||
Right = left + width;
|
||||
Top = top;
|
||||
Bottom = top + height;
|
||||
AABB = new AABB(left, top, Right, Bottom);
|
||||
BottomLeft = new Vector2(Left, Bottom);
|
||||
TopRight = new Vector2(Top, Right);
|
||||
Min = AABB.Min;
|
||||
|
|
|
@ -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>
|
||||
{
|
||||
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)
|
||||
|
|
|
@ -13,9 +13,9 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectangleOverlap()
|
||||
{
|
||||
var squareA = new Rectangle(2, 2);
|
||||
var squareA = new Rectangle(-1, -1, 2, 2);
|
||||
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 (result, simplex) = NarrowPhase.FindCollisionSimplex(squareA, transformA, squareB, transformB);
|
||||
|
@ -62,7 +62,7 @@ namespace Tests
|
|||
{
|
||||
var line = new Line(new Position2D(-4, -4), new Position2D(4, 4));
|
||||
var transformA = Transform2D.DefaultTransform;
|
||||
var square = new Rectangle(2, 2);
|
||||
var square = new Rectangle(-1, -1, 2, 2);
|
||||
var transformB = Transform2D.DefaultTransform;
|
||||
|
||||
var (result, simplex) = NarrowPhase.FindCollisionSimplex(line, transformA, square, transformB);
|
||||
|
|
|
@ -140,8 +140,8 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectangleEqual()
|
||||
{
|
||||
var a = new Rectangle(3, 3);
|
||||
var b = new Rectangle(3, 3);
|
||||
var a = new Rectangle(-1, -1, 3, 3);
|
||||
var b = new Rectangle(-1, -1, 3, 3);
|
||||
|
||||
a.Equals(b).Should().BeTrue();
|
||||
}
|
||||
|
@ -149,8 +149,8 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectangleEqualOperator()
|
||||
{
|
||||
var a = new Rectangle(3, 3);
|
||||
var b = new Rectangle(3, 3);
|
||||
var a = new Rectangle(-1, -1, 3, 3);
|
||||
var b = new Rectangle(-1, -1, 3, 3);
|
||||
|
||||
(a == b).Should().BeTrue();
|
||||
}
|
||||
|
@ -158,8 +158,8 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectangleNotEqual()
|
||||
{
|
||||
var a = new Rectangle(3, 3);
|
||||
var b = new Rectangle(6, 6);
|
||||
var a = new Rectangle(-1, -1, 3, 3);
|
||||
var b = new Rectangle(-3, -3, 6, 6);
|
||||
|
||||
a.Equals(b).Should().BeFalse();
|
||||
}
|
||||
|
@ -167,8 +167,8 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectangleNotEqualOperator()
|
||||
{
|
||||
var a = new Rectangle(3, 3);
|
||||
var b = new Rectangle(6, 6);
|
||||
var a = new Rectangle(-1, -1, 3, 3);
|
||||
var b = new Rectangle(-3, -3, 6, 6);
|
||||
|
||||
(a != b).Should().BeTrue();
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ namespace Tests
|
|||
public void PointRectangleOverlapping()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ namespace Tests
|
|||
{
|
||||
var point = new Point();
|
||||
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();
|
||||
}
|
||||
|
@ -354,10 +354,10 @@ namespace Tests
|
|||
[Test]
|
||||
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 rectangleB = new Rectangle(16, 16);
|
||||
var rectangleB = new Rectangle(-8, -8, 16, 16);
|
||||
var transformB = new Transform2D(new Position2D(16, 240));
|
||||
|
||||
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeFalse();
|
||||
|
@ -366,10 +366,10 @@ namespace Tests
|
|||
[Test]
|
||||
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 rectangleB = new Rectangle(2, 2);
|
||||
var rectangleB = new Rectangle(-1, -1, 2, 2);
|
||||
var transformB = new Transform2D(new Vector2(0, 0));
|
||||
|
||||
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
|
||||
|
@ -378,10 +378,10 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectanglesTouchingGJK2D()
|
||||
{
|
||||
var rectangleA = new Rectangle(2, 2);
|
||||
var rectangleA = new Rectangle(-1, -1, 2, 2);
|
||||
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));
|
||||
|
||||
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
|
||||
|
@ -390,10 +390,10 @@ namespace Tests
|
|||
[Test]
|
||||
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 rectangleB = new Rectangle(2, 2);
|
||||
var rectangleB = new Rectangle(-1, -1, 2, 2);
|
||||
var transformB = new Transform2D(new Vector2(1, 0));
|
||||
|
||||
NarrowPhase.TestCollision(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
|
||||
|
@ -402,10 +402,10 @@ namespace Tests
|
|||
[Test]
|
||||
public void RectanglesTouchingOverlap()
|
||||
{
|
||||
var rectangleA = new Rectangle(2, 2);
|
||||
var rectangleA = new Rectangle(-1, -1, 2, 2);
|
||||
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));
|
||||
|
||||
NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
|
||||
|
@ -414,10 +414,10 @@ namespace Tests
|
|||
[Test]
|
||||
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 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));
|
||||
|
||||
NarrowPhase.TestRectangleOverlap(rectangleA, transformA, rectangleB, transformB).Should().BeTrue();
|
||||
|
@ -428,18 +428,18 @@ namespace Tests
|
|||
{
|
||||
var multiRectangleA = new MultiShape(
|
||||
ImmutableArray.Create<(IShape2D, Transform2D)>(
|
||||
(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)))
|
||||
(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(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)))
|
||||
(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));
|
||||
|
@ -452,18 +452,18 @@ namespace Tests
|
|||
{
|
||||
var multiRectangleA = new MultiShape(
|
||||
ImmutableArray.Create<(IShape2D, Transform2D)>(
|
||||
(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)))
|
||||
(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(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)))
|
||||
(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));
|
||||
|
|
|
@ -14,16 +14,16 @@ namespace Tests
|
|||
{
|
||||
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 rectB = new Rectangle(4, 4);
|
||||
var rectB = new Rectangle(-2, -2, 4, 4);
|
||||
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 rectD = new Rectangle(4, 4);
|
||||
var rectD = new Rectangle(-2, -2, 4, 4);
|
||||
var rectDTransform = new Transform2D(new Vector2(24, 24));
|
||||
|
||||
var circleA = new Circle(2);
|
||||
|
@ -40,9 +40,9 @@ namespace Tests
|
|||
|
||||
var multiRectangle = new MultiShape(
|
||||
ImmutableArray.Create<(IShape2D, Transform2D)>(
|
||||
(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)))
|
||||
(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));
|
||||
|
@ -78,13 +78,13 @@ namespace Tests
|
|||
{
|
||||
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 rectB = new Rectangle(4, 4);
|
||||
var rectB = new Rectangle(-2, -2, 4, 4);
|
||||
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));
|
||||
|
||||
spatialHash.Insert(0, rectA, rectATransform);
|
||||
|
@ -99,10 +99,10 @@ namespace Tests
|
|||
{
|
||||
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 rectB = new Rectangle(4, 4);
|
||||
var rectB = new Rectangle(-2, -2, 4, 4);
|
||||
var rectBTransform = new Transform2D(new Vector2(8, 8));
|
||||
|
||||
spatialHash.Insert(0, rectA, rectATransform);
|
||||
|
|
|
@ -11,16 +11,16 @@ namespace Tests
|
|||
[Test]
|
||||
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 otherRectangle = new Rectangle(4, 4);
|
||||
var otherRectangle = new Rectangle(-2, -2, 4, 4);
|
||||
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 downRectangle = new Rectangle(12, 4);
|
||||
var downRectangle = new Rectangle(-6, -2, 12, 4);
|
||||
var downTransform = new Transform2D(new Position2D(-6, 20));
|
||||
|
||||
var spatialHash = new SpatialHash<int>(16);
|
||||
|
|
Loading…
Reference in New Issue