Example #1
0
        /// <summary>
        /// DOES NOT WORK. WIP. probably because the additems() doesnt work correctly.
        /// </summary>
        /// <param name="items"></param>
        /// <param name="area"></param>
        /// <param name="skipChecks"></param>
        public void GetItemsWithinArea(ref List <T> items, ref FRect area, bool skipChecks)
        {
            if (skipChecks || AABB.Intersects(area))
            {
                // When the area if entirely within the Cell then skip collision checks on the child nodes
                if (area.FullyEncloses(AABB))
                {
                    skipChecks = true;
                }

                foreach (T item in items)
                {
                    if (skipChecks || item.AABB.Intersects(area))
                    {
                        items.Add(item);
                    }
                }

                if (!IsLeaf)
                {
                    TopLeft.GetItemsWithinArea(ref items, ref area, skipChecks);
                    TopRight.GetItemsWithinArea(ref items, ref area, skipChecks);
                    BottomLeft.GetItemsWithinArea(ref items, ref area, skipChecks);
                    BottomRight.GetItemsWithinArea(ref items, ref area, skipChecks);
                }
            }
        }
        /// <summary>
        /// Author: Napoleon.
        /// Tested and works.
        /// Only apply this to non-rotated rectangles.
        /// Touching rectangles don't count in this function
        /// </summary>
        /// <param name="movingRect">Usually the player or moving object.</param>
        /// <param name="staticRect">Usually the wall or something. The collided sides are the sides from this rectangle</param>
        /// <returns></returns>
        /// <example>
        ///  if ((collidedSides & Collision.RectSide.Top) == Collision.RectSide.Top)
        ///    // Top side was hit, do something here
        /// </example>
        public static RectSide CollidedSidesNoTouch(this FRect movingRect, FRect staticRect)
        {
            RectSide result = RectSide.None;

            if (!movingRect.Intersects(staticRect))
            {
                return(result); // No intersection at all
            }
            if (movingRect == staticRect)
            {
                return(RectSide.Equal);
            }

            // Left
            if (movingRect.Right > staticRect.Left && movingRect.Left < staticRect.Left)
            {
                result = (result | RectSide.Left);
            }

            // Top
            if (movingRect.Bottom > staticRect.Top && movingRect.Top < staticRect.Top)
            {
                result = (result | RectSide.Top);
            }

            // Right
            if (movingRect.Left < staticRect.Right && movingRect.Right > staticRect.Right)
            {
                result = (result | RectSide.Right);
            }

            // Bottom
            if (movingRect.Top < staticRect.Bottom && movingRect.Bottom > staticRect.Bottom)
            {
                result = (result | RectSide.Bottom);
            }

            if (result == RectSide.None)
            {
                return(RectSide.Inside); // moving rectangle is inside the static rectangle
            }
            return(result);
        }