public QTreeNode(Rectangle bounds, QTreeNode parent) { this.Bounds = bounds; this.Entities = new List <Entity>(); this.Children = new List <QTreeNode>(); this.Parent = parent; }
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; }
public void PrepareLineEntities(QTreeNode node) { this.GetLineEntities(node.GetRectangle()); if (node.GetNumberOfChildren() > 0) { foreach (QTreeNode nodeChild in node.GetChildren()) { this.PrepareLineEntities(nodeChild); } } }
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(); }
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); } } }