public void UnRotatedRectanglesCollideCorrectly() { var rect1 = new BoundingRectangle(10, 6) { Position = new Vector2d(5, 3) }; var rect2 = new BoundingRectangle(3, 3) { Position = new Vector2d(5, 3) }; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect2.Position += 3; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect2.Position++; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect2.Position++; Assert.False(rect1.Intersects(rect2)); Assert.False(rect2.Intersects(rect1)); }
public void UnrotatedRectangleCornersGetWorks() { var rect = new BoundingRectangle(6, 6) { Position = new Vector2d(3, 3) }; Assert.True(rect.TopLeft.Equivalent(Vector2d.Zero)); Assert.True(rect.TopRight.Equivalent(new Vector2d(6, 0))); Assert.True(rect.BotLeft.Equivalent(new Vector2d(0, 6))); Assert.True(rect.BotRight.Equivalent(new Vector2d(6, 6))); rect.Position -= 6; Assert.True(rect.TopLeft.Equivalent(new Vector2d(-6, -6))); Assert.True(rect.TopRight.Equivalent(new Vector2d(0, -6))); Assert.True(rect.BotLeft.Equivalent(new Vector2d(-6, 0))); Assert.True(rect.BotRight.Equivalent(Vector2d.Zero)); }
public override bool Intersects(BoundingRectangle rectangle) { Vector2d translated = rectangle.Rotation == 0 ? Position : Position.RotateAround(rectangle.Position, -rectangle.Rotation); var circleDistance = translated.Distance(rectangle.Position); if (circleDistance.X > (rectangle.Size.HalfWidth + this.Radius)) { return false; } if (circleDistance.Y > (rectangle.Size.HalfHeight + this.Radius)) { return false; } if (circleDistance.X <= (rectangle.Size.HalfWidth)) { return true; } if (circleDistance.Y <= (rectangle.Size.HalfHeight)) { return true; } var cornerDistance_sq = Math.Pow(circleDistance.X - rectangle.Size.HalfWidth, 2) + Math.Pow(circleDistance.Y - rectangle.Size.HalfHeight, 2); return (cornerDistance_sq <= (this.Radius * this.Radius)); }
public void IsCollidingWithRectangleWorks() { var rect = new BoundingRectangle(10, 6) { Position = new Vector2d(5, 3) }; var circle = new BoundingCircle(3) { Position = new Vector2d(14, 3) }; Assert.False(circle.Intersects(rect)); circle.Position.X--; Assert.True(circle.Intersects(rect)); rect.Rotation = Math.PI * .5; rect.Position.X = 3; rect.Position.Y = 5; circle.Position.X = 8; circle.Position.Y = 12; Assert.True(circle.Intersects(rect)); circle = new BoundingCircle(50) { Position = new Vector2d(156, 165) }; rect = new BoundingRectangle(new Size2d(200, 100)) { Position = new Vector2d(300, 200) }; Assert.True(circle.Intersects(rect)); circle.Position.X = 300; circle.Position.Y = 350; Assert.False(circle.Intersects(rect)); }
public void RotatedRectangleCornersGetWorks() { var rect = new BoundingRectangle(4, 2) { Position = new Vector2d(3, 2), Rotation = -Math.PI / 2 }; var topLeft = rect.TopLeft.Clone(); var topRight = rect.TopRight.Clone(); var botLeft = rect.BotLeft.Clone(); var botRight = rect.BotRight.Clone(); topLeft.Apply(Math.Round); topRight.Apply(Math.Round); botLeft.Apply(Math.Round); botRight.Apply(Math.Round); Assert.True(topLeft.Equivalent(new Vector2d(2, 4))); Assert.True(topRight.Equivalent(new Vector2d(2, 0))); Assert.True(botLeft.Equivalent(new Vector2d(4, 4))); Assert.True(botRight.Equivalent(new Vector2d(4, 0))); }
public override bool Intersects(BoundingRectangle obj) { if (Rotation == 0 && obj.Rotation == 0) { Vector2d myTopLeft = TopLeft, myBotRight = BotRight, theirTopLeft = obj.TopLeft, theirBotRight = obj.BotRight; return theirTopLeft.X <= myBotRight.X && theirBotRight.X >= myTopLeft.X && theirTopLeft.Y <= myBotRight.Y && theirBotRight.Y >= myTopLeft.Y; } else if (obj.Position.Distance(Position).Magnitude() <= obj.Size.Radius + Size.Radius) // Check if we're somewhat close to the object that we might be colliding with { var axisList = new Vector2d[] { TopRight - TopLeft, TopRight - BotRight, obj.TopLeft - obj.BotLeft, obj.TopLeft - obj.TopRight }; var myVertices = Vertices; var theirVertices = obj.Vertices; foreach (var axi in axisList) { var myProjections = Vector2dHelpers.GetMinMaxProjections(axi, myVertices); var theirProjections = Vector2dHelpers.GetMinMaxProjections(axi, theirVertices); // No collision if (theirProjections.Max < myProjections.Min || myProjections.Max < theirProjections.Min) { return false; } } return true; } return false; }
public void RotatedRectanglesCollideCorrectly() { var rect1 = new BoundingRectangle(4.24, 2.83) { Position = new Vector2d(2.5, 2.5), Rotation = - Math.PI / 4 }; var rect2 = new BoundingRectangle(6, 4) { Position = new Vector2d(9, 4) }; Assert.False(rect1.Intersects(rect2)); Assert.False(rect2.Intersects(rect1)); rect2.Position.X-=2; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect2.Rotation = Math.PI / 2; rect2.Position++; Assert.False(rect1.Intersects(rect2)); Assert.False(rect2.Intersects(rect1)); rect2.Rotation = Math.PI; rect2.Position--; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect2.Position.Y+=10; Assert.False(rect1.Intersects(rect2)); Assert.False(rect2.Intersects(rect1)); rect2.Position.Y -= 10; rect1.Position.X += 5; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); rect1.Position.X += 9; Assert.False(rect1.Intersects(rect2)); Assert.False(rect2.Intersects(rect1)); rect1.Rotation = 0; rect1.Position.X = 12; rect2.Rotation = Math.PI; Assert.True(rect1.Intersects(rect2)); Assert.True(rect2.Intersects(rect1)); }
public void RotatedRectangleContainsPointWorks() { var rect = new BoundingRectangle(4.24, 2.83) { Position = new Vector2d(2.5, 2.5), Rotation = Math.PI / 4 }; Assert.True(rect.ContainsPoint(rect.Position)); foreach (var vertex in rect.Vertices) { Assert.True(rect.ContainsPoint(vertex)); } Assert.False(rect.ContainsPoint(new Vector2d(-1, 0))); Assert.False(rect.ContainsPoint(rect.TopRight + 1)); Assert.False(rect.ContainsPoint(rect.BotRight + 1)); Assert.False(rect.ContainsPoint(rect.TopLeft - 1)); Assert.False(rect.ContainsPoint(rect.BotLeft - 1)); Assert.True(rect.ContainsPoint(rect.TopRight - 1)); Assert.True(rect.ContainsPoint(rect.BotRight - 1)); Assert.True(rect.ContainsPoint(rect.TopLeft + 1)); Assert.True(rect.ContainsPoint(rect.BotLeft + 1)); }
public void UnrotatedRectangleContainsPointWorks() { var rect = new BoundingRectangle(6, 4) { Position = new Vector2d(3, 2) }; Assert.True(rect.ContainsPoint(rect.Position)); foreach (var vertex in rect.Vertices) { Assert.True(rect.ContainsPoint(vertex)); } Assert.False(rect.ContainsPoint(new Vector2d(-1, 0))); Assert.False(rect.ContainsPoint(rect.TopRight + 1)); Assert.False(rect.ContainsPoint(rect.BotRight + 1)); Assert.False(rect.ContainsPoint(rect.TopLeft - 1)); Assert.False(rect.ContainsPoint(rect.BotLeft - 1)); }
public abstract bool Intersects(BoundingRectangle rectangle);