Ejemplo n.º 1
0
        // If pxBoundingBox is null, it will search everywhere
        public void GetAssociatedNodes(Entity entity, FRectangle?pxBoundingBox, ref List <QuadTreeNode> result)
        {
            if (IsLeafNode)
            {
                if (Entities.Contains(entity))
                {
                    result.Add(this);
                }
                return;
            }

            if (pxBoundingBox == null || ChildNode1.Intersects(pxBoundingBox.Value))
            {
                ChildNode1.GetAssociatedNodes(entity, pxBoundingBox, ref result);
            }
            if (pxBoundingBox == null || ChildNode2.Intersects(pxBoundingBox.Value))
            {
                ChildNode2.GetAssociatedNodes(entity, pxBoundingBox, ref result);
            }
            if (pxBoundingBox == null || ChildNode3.Intersects(pxBoundingBox.Value))
            {
                ChildNode3.GetAssociatedNodes(entity, pxBoundingBox, ref result);
            }
            if (pxBoundingBox == null || ChildNode4.Intersects(pxBoundingBox.Value))
            {
                ChildNode4.GetAssociatedNodes(entity, pxBoundingBox, ref result);
            }
        }
Ejemplo n.º 2
0
        internal void GetEntities(FRectangle?pxRegion, ref List <Entity> result)
        {
            if (Entities.Count > 0)
            {
                foreach (Entity entity in Entities)
                {
                    if (!result.Contains(entity))           // TODO: This needs to be removed. It is slow and should not be here
                    {
                        result.Add(entity);
                    }
                }
            }
            else
            {
                if (IsLeafNode)
                {
                    return;
                }

                if (pxRegion == null || ChildNode1.Intersects(pxRegion.Value))
                {
                    ChildNode1.GetEntities(pxRegion, ref result);
                }
                if (pxRegion == null || ChildNode2.Intersects(pxRegion.Value))
                {
                    ChildNode2.GetEntities(pxRegion, ref result);
                }
                if (pxRegion == null || ChildNode3.Intersects(pxRegion.Value))
                {
                    ChildNode3.GetEntities(pxRegion, ref result);
                }
                if (pxRegion == null || ChildNode4.Intersects(pxRegion.Value))
                {
                    ChildNode4.GetEntities(pxRegion, ref result);
                }
            }
        }
Ejemplo n.º 3
0
        public void Add(Entity entity)
        {
            float pxHalfWidth  = pxBounds.Width / 2.0f;
            float pxHalfHeight = pxBounds.Height / 2.0f;

            if ((IsLeafNode && Entities.Count < QuadTree.EntityLimit) || pxHalfWidth <= QuadTree.pxTileWidth || pxHalfHeight <= QuadTree.pxTileHeight)
            {
                if (!Entities.Contains(entity))
                {
                    Entities.Add(entity);
                }
                return;
            }

            if (IsLeafNode)
            {
                ChildNode1 = QuadTree.GetQuadTreeNode(
                    pxBounds.X,
                    pxBounds.Y,
                    pxHalfWidth,
                    pxHalfHeight,
                    this);
                ChildNode2 = QuadTree.GetQuadTreeNode(
                    pxBounds.X + pxHalfWidth,
                    pxBounds.Y,
                    pxHalfWidth,
                    pxHalfHeight,
                    this);
                ChildNode3 = QuadTree.GetQuadTreeNode(
                    pxBounds.X,
                    pxBounds.Y + pxHalfHeight,
                    pxHalfWidth,
                    pxHalfHeight,
                    this);
                ChildNode4 = QuadTree.GetQuadTreeNode(
                    pxBounds.X + pxHalfWidth,
                    pxBounds.Y + pxHalfHeight,
                    pxHalfWidth,
                    pxHalfHeight,
                    this);
            }

            // Add the entity specified in the function paramaters
            if (ChildNode1.Intersects(entity.CurrentBoundingBox))
            {
                ChildNode1.Add(entity);
            }
            if (ChildNode2.Intersects(entity.CurrentBoundingBox))
            {
                ChildNode2.Add(entity);
            }
            if (ChildNode3.Intersects(entity.CurrentBoundingBox))
            {
                ChildNode3.Add(entity);
            }
            if (ChildNode4.Intersects(entity.CurrentBoundingBox))
            {
                ChildNode4.Add(entity);
            }

            // Distrobute any entities found in this current Node
            foreach (Entity entityToAdd in this.Entities)
            {
                if (ChildNode1.Intersects(entityToAdd.CurrentBoundingBox))
                {
                    ChildNode1.Add(entityToAdd);
                }
                if (ChildNode2.Intersects(entityToAdd.CurrentBoundingBox))
                {
                    ChildNode2.Add(entityToAdd);
                }
                if (ChildNode3.Intersects(entityToAdd.CurrentBoundingBox))
                {
                    ChildNode3.Add(entityToAdd);
                }
                if (ChildNode4.Intersects(entityToAdd.CurrentBoundingBox))
                {
                    ChildNode4.Add(entityToAdd);
                }
            }

            Entities.Clear();
        }