示例#1
0
        /// <summary>
        /// Return all objects that could collide with the given object
        /// </summary>
        public List <QuadObject> retrieve(List <QuadObject> returnObjects, QuadObject pRect)
        {
            int index = getIndex(pRect);

            if (index != -1 && nodes[0] != null)
            {
                nodes[index].retrieve(returnObjects, pRect);
            }

            returnObjects.AddRange(objects);

            return(returnObjects);
        }
示例#2
0
        /// <summary>
        /// Insert the object into the quadtree. If the node
        /// exceeds the capacity, it will split and add all
        /// objects to their corresponding nodes.
        /// </summary>
        public void insert(QuadObject obj)
        {
            if (nodes[0] != null)
            {
                int index = getIndex(obj);

                if (index != -1)
                {
                    nodes[index].insert(obj);

                    return;
                }
            }

            objects.Add(obj);

            if (objects.Count > objectPerQuad && level < maxDepth)
            {
                if (nodes[0] == null)
                {
                    split();
                }

                int i = 0;
                while (i < objects.Count)
                {
                    int index = getIndex(objects[i]);
                    if (index != -1)
                    {
                        nodes[index].insert(objects[i]);
                        objects.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Returns the index of the passed rect
        /// -1 means it's not found in the tree
        /// </summary>
        private int getIndex(QuadObject obj)
        {
            int   index              = -1;
            float verticalMidpoint   = this.rect.x + (this.rect.width / 2);
            float horizontalMidpoint = this.rect.y + (this.rect.height / 2);

            // Object can completely fit within the top quadrants
            bool topQuadrant = (obj.rect.y < horizontalMidpoint && obj.rect.y + obj.rect.height < horizontalMidpoint);
            // Object can completely fit within the bottom quadrants
            bool bottomQuadrant = (obj.rect.y > horizontalMidpoint);

            // Object can completely fit within the left quadrants
            if (obj.rect.x < verticalMidpoint && obj.rect.x + obj.rect.width < verticalMidpoint)
            {
                if (topQuadrant)
                {
                    index = 1;
                }
                else if (bottomQuadrant)
                {
                    index = 2;
                }
            }
            // Object can completely fit within the right quadrants
            else if (obj.rect.x > verticalMidpoint)
            {
                if (topQuadrant)
                {
                    index = 0;
                }
                else if (bottomQuadrant)
                {
                    index = 3;
                }
            }

            return(index);
        }