예제 #1
0
 public QTreeNode(Rectangle bounds, QTreeNode parent)
 {
     this.Bounds   = bounds;
     this.Entities = new List <Entity>();
     this.Children = new List <QTreeNode>();
     this.Parent   = parent;
 }
예제 #2
0
 public QuadTree(Rectangle bounds, int max_entities_per_node, Boolean visualize = false)
 {
     this.Bounds             = bounds;
     this.Root               = new QTreeNode(this.Bounds, null);
     this.MaxEntitiesPerNode = max_entities_per_node;
     this.Entities           = new List <Entity>();
     this.LineEntities       = new List <Entity>();
     this.Visualize          = visualize;
 }
예제 #3
0
        public void PrepareLineEntities(QTreeNode node)
        {
            this.GetLineEntities(node.GetRectangle());

            if (node.GetNumberOfChildren() > 0)
            {
                foreach (QTreeNode nodeChild in node.GetChildren())
                {
                    this.PrepareLineEntities(nodeChild);
                }
            }
        }
예제 #4
0
        public void Recalculate()
        {
            this.Root         = new QTreeNode(this.Bounds, null);
            this.LineEntities = new List <Entity>();

            foreach (Entity entity in this.Entities)
            {
                entity.QTreeNodes = new List <QTreeNode>();
                this.AddEntity(entity, this.Root);
            }

            //this.PrepareLineEntities();
        }
예제 #5
0
        public void AddEntity(Entity entity, QTreeNode node)
        {
            if (!entity.Collidable)
            {
                return;
            }

            Vector2 entityPosition = entity.GetProjectedPosition();

            if (!entity.GetRectangle(entityPosition).Intersects(node.GetRectangle()))
            {
                return;
            }

            if (node.GetNumberOfChildren() == 0)
            {
                if (node.GetNumberOfEntities() < this.MaxEntitiesPerNode)
                {
                    node.AddEntity(entity);
                }
                else
                {
                    node.Divide();

                    foreach (Entity childEntity in node.GetEntities())
                    {
                        childEntity.EmptyQTreeNodes();

                        foreach (QTreeNode childNode in node.GetChildren())
                        {
                            this.AddEntity(childEntity, childNode);
                        }
                    }

                    node.EmptyEntities();

                    foreach (QTreeNode childNode in node.GetChildren())
                    {
                        this.AddEntity(entity, childNode);
                    }
                }
            }
            else
            {
                foreach (QTreeNode childNode in node.GetChildren())
                {
                    this.AddEntity(entity, childNode);
                }
            }
        }