Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <returns>true when the item was added</returns>
        public bool AddItem(T item)
        {
            // Only add the item if it intersects with this node
            if (AABB.FullyEncloses(item.AABB))
            {
                if (IsLeaf)
                {
                    SubDivide();
                }

                // Try to add the item to a childnode but if no childnode will take it add it to this node
                if (!TopLeft.AddItem(item))
                {
                    if (!TopRight.AddItem(item))
                    {
                        if (!BottomLeft.AddItem(item))
                        {
                            if (!BottomRight.AddItem(item))
                            {
                                Items.Add(item);
                            }
                        }
                    }
                }

                return(true);
            }
            return(false);
        }
Exemplo n.º 2
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);
                }
            }
        }