Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
        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;
        }