public void RotatedRectanglesCollideCorrectly()
        {
            var rect1 = new BoundingRectangle(new Vector2d(2.5, 2.5), new Size2d(4.24, 2.83))
            {
                Rotation = - Math.PI / 4
            };
            var rect2 = new BoundingRectangle(new Vector2d(9, 4), new Size2d(6, 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 UnrotatedRectangleCornersGetWorks()
        {
            var rect = new BoundingRectangle(new Vector2d(3, 3), new Size2d(6, 6));

            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 void UnRotatedRectanglesCollideCorrectly()
        {
            var rect1 = new BoundingRectangle(new Vector2d(5, 3), new Size2d(10, 6));
            var rect2 = new BoundingRectangle(new Vector2d(5, 3), new Size2d(3, 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 IsCollidingWithRectangleWorks()
        {
            var rect = new BoundingRectangle(Vector2d.Zero, new Size2d(10, 6))
            {
                Position = new Vector2d(5, 3)
            };

            var circle = new BoundingCircle(Vector2d.Zero, 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(Vector2d.Zero, 50)
            {
                Position = new Vector2d(156, 165)
            };
            rect = new BoundingRectangle(Vector2d.Zero, 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(new Vector2d(3, 2), new Size2d(4, 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 void RotatedRectangleContainsPointWorks()
        {
            var rect = new BoundingRectangle(new Vector2d(2.5, 2.5), new Size2d(4.24, 2.83))
            {
                Rotation = Math.PI / 4
            };

            Assert.True(rect.Contains(rect.Position));

            foreach (var vertex in rect.Corners())
            {
                Assert.True(rect.Contains(vertex));
            }

            Assert.False(rect.Contains(new Vector2d(-1, 0)));
            Assert.False(rect.Contains(rect.TopRight + 1));
            Assert.False(rect.Contains(rect.BotRight + 1));
            Assert.False(rect.Contains(rect.TopLeft - 1));
            Assert.False(rect.Contains(rect.BotLeft - 1));

            Assert.True(rect.Contains(rect.TopRight - 1));
            Assert.True(rect.Contains(rect.BotRight - 1));
            Assert.True(rect.Contains(rect.TopLeft + 1));
            Assert.True(rect.Contains(rect.BotLeft + 1));
        }
        public void UnrotatedRectangleContainsPointWorks()
        {
            var rect = new BoundingRectangle(new Vector2d(3, 2), new Size2d(6, 4));

            Assert.True(rect.Contains(rect.Position));

            foreach (var vertex in rect.Corners())
            {
                Assert.True(rect.Contains(vertex));
            }

            Assert.False(rect.Contains(new Vector2d(-1, 0)));
            Assert.False(rect.Contains(rect.TopRight + 1));
            Assert.False(rect.Contains(rect.BotRight + 1));
            Assert.False(rect.Contains(rect.TopLeft - 1));
            Assert.False(rect.Contains(rect.BotLeft - 1));
        }
        /// <summary>
        /// Determines if the current BoundingRectangle contains the provided BoundingRectangle.
        /// </summary>
        /// <param name="rectangle">A rectangle to check containment on.</param>
        /// <returns>Whether the BoundingRectangle contains the rectangle.</returns>
        public override bool Contains(BoundingRectangle rectangle)
        {
            var corners = rectangle.Corners();

            for (var i = 0; i < corners.Length; i++)
            {
                if (!this.Contains(corners[i]))
                {
                    return false;
                }
            }

            return true;
        }
        /// <summary>
        /// Determines if the current BoundingRectangle is intersecting the provided BoundingRectangle.
        /// </summary>
        /// <param name="obj">BoundingRectangle to check intersection with.</param>
        /// <returns>Whether the BoundingRectangle intersects the BoundingRectangle.</returns>
        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 myCorners = Corners();
                var theirCorners = obj.Corners();

                foreach (var axi in axisList)
                {
                    var myProjections = Vector2dHelpers.GetMinMaxProjections(axi, myCorners);
                    var theirProjections = Vector2dHelpers.GetMinMaxProjections(axi, theirCorners);

                    // No collision
                    if (theirProjections.Max < myProjections.Min || myProjections.Max < theirProjections.Min)
                    {
                        return false;
                    }
                }

                return true;
            }

            return false;
        }
Esempio n. 10
0
 /// <summary>
 /// Determines if the current bounded object is intersecting the provided BoundingRectangle.    
 /// </summary>
 /// <param name="rectangle">BoundingRectangle to check intersection with.</param>
 /// <returns>Whether the bounds intersects the BoundingRectangle.</returns>
 public abstract bool Intersects(BoundingRectangle rectangle);
Esempio n. 11
0
 /// <summary>
 /// Determines if the current bounded object completely contains the provided BoundingRectangle.
 /// </summary>
 /// <param name="rectangle">A rectangle to check containment on.</param>
 /// <returns>Whether the bounds contains the rectangle.</returns>
 public abstract bool Contains(BoundingRectangle rectangle);
        /// <summary>
        /// Determines if the current BoundingCircle is intersecting the provided BoundingRectangle.
        /// </summary>
        /// <param name="rectangle">BoundingRectangle to check intersection with.</param>
        /// <returns>Whether the BoundingCircle intersects the rectangle.</returns>
        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));
        }