private BSPCollisionNode CreateNewNode(RectBox area) { byte splitAxis; float splitPos; if (area.width > area.height) { splitAxis = 0; splitPos = area.GetMiddleX(); } else { splitAxis = 1; splitPos = area.GetMiddleY(); } BSPCollisionNode newNode = GetBSPNode(); newNode.SetArea(area); newNode.SetSplitAxis(splitAxis); newNode.SetSplitPos(splitPos); return(newNode); }
public void AddObject(Actor actor) { RectBox bounds = this.GetActorBounds(actor); float by; if (this.bspTree == null) { byte treeArea; if (bounds.width > bounds.height) { treeArea = 0; by = bounds.GetMiddleX(); } else { treeArea = 1; by = bounds.GetMiddleY(); } this.bspTree = GetBSPNode(); this.bspTree.GetArea().Copy(bounds); this.bspTree.SetSplitAxis(treeArea); this.bspTree.SetSplitPos(by); this.bspTree.AddActor(actor); } else { int idx = 0; RectBox treeArea1 = this.bspTree.GetArea(); RectBox result1 = new RectBox(); RectBox result2 = new RectBox(); for (; !treeArea1.Contains(bounds) && idx < MAX_SIZE;) { RectBox newArea; BSPCollisionNode newTop; if (bounds.GetX() < treeArea1.GetX()) { by = (treeArea1.GetX() - treeArea1.width); newArea = new RectBox(by, treeArea1.GetY(), treeArea1.GetRight() - by, treeArea1.height); newTop = GetBSPNode(); newTop.GetArea().Copy(newArea); newTop.SetSplitAxis(0); newTop.SetSplitPos(treeArea1.GetX()); newTop.SetChild(1, this.bspTree); this.bspTree = newTop; treeArea1 = newArea; } if (bounds.GetRight() > treeArea1.GetRight()) { by = (treeArea1.GetRight() + treeArea1.width); newArea = new RectBox(treeArea1.GetX(), treeArea1.GetY(), by - treeArea1.GetX(), treeArea1.height); newTop = GetBSPNode(); newTop.GetArea().Copy(newArea); newTop.SetSplitAxis(0); newTop.SetSplitPos(treeArea1.GetRight()); newTop.SetChild(0, this.bspTree); this.bspTree = newTop; treeArea1 = newArea; } if (bounds.GetY() < treeArea1.GetY()) { by = (treeArea1.GetY() - treeArea1.height); newArea = new RectBox(treeArea1.GetX(), by, treeArea1.width, treeArea1.GetBottom() - by); newTop = GetBSPNode(); newTop.GetArea().Copy(newArea); newTop.SetSplitAxis(1); newTop.SetSplitPos(treeArea1.GetY()); newTop.SetChild(1, this.bspTree); this.bspTree = newTop; treeArea1 = newArea; } if (bounds.GetBottom() > treeArea1.GetBottom()) { by = (treeArea1.GetBottom() + treeArea1.height); newArea = new RectBox(treeArea1.GetX(), treeArea1.GetY(), treeArea1.width, by - treeArea1.GetY()); newTop = GetBSPNode(); newTop.GetArea().Copy(newArea); newTop.SetSplitAxis(1); newTop.SetSplitPos(treeArea1.GetBottom()); newTop.SetChild(0, this.bspTree); this.bspTree = newTop; treeArea1 = newArea; } idx++; } this.InsertObject(actor, bounds, bounds, treeArea1, this.bspTree, result1, result2); } }