public void AddObjectsIntersectingShape(IList list, IShape shape) { if (_objects == null) { return; } for (var i = 0; i < _objects.Count; i++) { GameObject2D obj = _objects[i]; Rectangle bounds = obj.GetBoundsForQuadTree(); if (shape.Intersects(ref bounds)) { list.Add(obj); } } if (ChildNodes == null) { return; } for (var i = 0; i < ChildNodes.Length; i++) { WorldTree2DNode node = ChildNodes[i]; if (shape.Intersects(ref node.Bounds)) { node.AddObjectsIntersectingShape(list, shape); } } }
public void RenderDebug(RenderComposer c) { c.RenderOutline(Bounds, Color.Blue); if (ChildNodes == null) { return; } for (var i = 0; i < ChildNodes.Length; i++) { WorldTree2DNode node = ChildNodes[i]; node.RenderDebug(c); } }
public WorldTree2DNode GetNodeForBounds(Rectangle bounds) { if (ChildNodes == null) { return(this); } for (var i = 0; i < ChildNodes.Length; i++) { WorldTree2DNode node = ChildNodes[i]; if (node.Bounds.ContainsInclusive(bounds)) { return(node.GetNodeForBounds(bounds)); } } return(this); }
public WorldTree2DNode AddObject(Rectangle bounds, GameObject2D obj) { _objects ??= new List <GameObject2D>(); if (_objects.Count + 1 > Capacity && ChildNodes == null && MaxDepth > 0) { float halfWidth = Bounds.Width / 2; float halfHeight = Bounds.Height / 2; ChildNodes = new WorldTree2DNode[4]; ChildNodes[0] = new WorldTree2DNode(this, new Rectangle(Bounds.X, Bounds.Y, halfWidth, halfHeight), Capacity, MaxDepth - 1); ChildNodes[1] = new WorldTree2DNode(this, new Rectangle(Bounds.X + halfWidth, Bounds.Y, halfWidth, halfHeight), Capacity, MaxDepth - 1); ChildNodes[2] = new WorldTree2DNode(this, new Rectangle(Bounds.X, Bounds.Y + halfHeight, halfWidth, halfHeight), Capacity, MaxDepth - 1); ChildNodes[3] = new WorldTree2DNode(this, new Rectangle(Bounds.X + halfWidth, Bounds.Y + halfHeight, halfWidth, halfHeight), Capacity, MaxDepth - 1); WorldTree2DNode subNode = GetNodeForBounds(bounds); return(subNode.AddObject(bounds, obj)); } Debug.Assert(_objects.IndexOf(obj) == -1); _objects.Add(obj); return(this); }