public QuadtreeNode(float Size, Vector2 Position, QuadtreeNode Parent, int Depth) { this.Size = Size; this.Position = Position; this.Parent = Parent; this.Depth = Depth; Boxes = new LinkedList <AlignedBoundingBox>(); }
public void Subdivide() { float newSize = this.Size / 2; child0 = new QuadtreeNode(newSize, this.Position + newSize / 2 * new Vector2(1, 1), this, Depth + 1); child1 = new QuadtreeNode(newSize, this.Position + newSize / 2 * new Vector2(-1, 1), this, Depth + 1); child2 = new QuadtreeNode(newSize, this.Position + newSize / 2 * new Vector2(-1, -1), this, Depth + 1); child3 = new QuadtreeNode(newSize, this.Position + newSize / 2 * new Vector2(1, -1), this, Depth + 1); }
private void drawAlg(QuadtreeNode current, Color color, SpriteBatch batch, Camera cam) { if (current[0] != null) { drawAlg(current[0], color, batch, cam); drawAlg(current[1], color, batch, cam); drawAlg(current[2], color, batch, cam); drawAlg(current[3], color, batch, cam); } else { current.Draw(color, batch, cam); } }
private void insert(AlignedBoundingBox newBounds, QuadtreeNode current) { newBounds.ContainerTree = this; if (current.isColliding(newBounds) && current.Depth < MaxDepth) { if (current[0] == null) { current.Subdivide(); } insert(newBounds, current[0]); insert(newBounds, current[1]); insert(newBounds, current[2]); insert(newBounds, current[3]); } else if (current.isColliding(newBounds)) { current.InsertBounds(newBounds); } }
private void collisionSearch(AlignedBoundingBox searchBounds, QuadtreeNode current, ref LinkedList <AlignedBoundingBox> list) { if (current.isColliding(searchBounds) && current[0] != null) { collisionSearch(searchBounds, current[0], ref list); collisionSearch(searchBounds, current[1], ref list); collisionSearch(searchBounds, current[2], ref list); collisionSearch(searchBounds, current[3], ref list); } else if (current[0] == null) { if (current.Boxes.Count != 0) { foreach (AlignedBoundingBox b in current.Boxes) { if (b.isColliding(searchBounds) && !list.Contains(b) && b != searchBounds) { list.AddFirst(b); } } } } }
public Quadtree(Vector2 Position, float Size, int MaxDepth) { this.MaxDepth = MaxDepth; Root = new QuadtreeNode(Size, Position, null, 1); }