/// <summary> /// folds up unused quads by checking this node and it's parent, and on up if it keeps finding empty nodes /// </summary> private void Verify() { if (IsParent) { //if it's 0, collapse check parent var children = ChildCount(); if (children == 0) { NW = SW = NE = SE = null;//ugly but it works better for this circumstance parent.Verify(); } else if (children == 1) { var objects = GatherAll()[0]; NW = SW = NE = SE = null; containedObject = objects; objects.CurrentQuad = this; if (parent != null) //doh, forgot that the master quad doesn't have a parent... { parent.Verify(); } } //if it's more than 1, do nothing } else { if (parent != null) //temp bug fix not sure why this was null... { parent.Verify(); } } }
/// <summary> /// if it's the very top one, set parent as null /// </summary> /// <param name="bounds">collidable area</param> /// <param name="parent">parent of this node</param> public ElasticQuadTree(RectangleFloat bounds, ElasticQuadTree parent) { this.parent = parent; this.bounds = bounds; this.queryBounds = bounds; }
/// <summary> /// splits this node into 4 quads and places it's held object into the appropriate child /// </summary> private void Split() { //build em up //nw left top NW = new ElasticQuadTree(new RectangleFloat(bounds.Left, bounds.Top, bounds.Width / 2, bounds.Height / 2), this); //ne center, top NE = new ElasticQuadTree(new RectangleFloat(bounds.Left + bounds.Width / 2, bounds.Top, bounds.Width / 2, bounds.Height / 2), this); //sw left center SW = new ElasticQuadTree(new RectangleFloat(bounds.Left, bounds.Top + bounds.Height / 2, bounds.Width / 2, bounds.Height / 2), this); //se center center SE = new ElasticQuadTree(new RectangleFloat(bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2, bounds.Width / 2, bounds.Height / 2), this); //semi dangerous assumption that it contains an object at this point var temp = containedObject; containedObject = null; if (NW.Insert(temp)) return; else if (SW.Insert(temp)) return; else if (NE.Insert(temp)) return; else if (SE.Insert(temp)) return; MasterInsert(temp); }