/// <summary> /// Removes childnodes /// </summary> /// <param name="data"></param> protected void Remove(QuadTreeData data) { if (IsLeaf()) { int removeIndex = -1; for (int i = 0; i < contents.Count; i++) { if (contents[i].obj == data.obj) { removeIndex = i; break; } } if (removeIndex != -1) { contents.RemoveAt(1); } } else { for (int i = 0; i < children.Count; i++) { children[i].Remove(data); } } Shake(); }
/// <summary> /// Insert object into a new node /// </summary> /// <param name="data"></param> public void Insert(QuadTreeData data) { if (!Collisions.RectangleRectangle(data.bounds, nodeBounds)) { return; } if (IsLeaf() && contents.Count + 1 > maxObjectsPerNode) { Split(); } if (IsLeaf()) { contents.Add(data); } else { for (int i = 0; i < children.Count; i++) { children[i].Insert(data); } } }
/// <summary> /// The Update function needs to be called whenever an object moves. This function will remove the object from the tree, and reinsert the object. /// </summary> /// <param name="data"></param> public void Update(QuadTreeData data) { Remove(data); Insert(data); }