private void InsertObject(Actor actor, RectBox actorBounds, RectBox bounds, RectBox area, BSPCollisionNode node, RectBox result1, RectBox result2) { if (!node.ContainsActor(actor)) { if (!node.IsEmpty() && (area.width > actorBounds.width || area.height > actorBounds.height)) { RectBox leftArea = node.GetLeftArea(); RectBox rightArea = node.GetRightArea(); RectBox leftIntersects = RectBox.GetIntersection(leftArea, bounds, result1); RectBox rightIntersects = RectBox.GetIntersection(rightArea, bounds, result2); BSPCollisionNode newRight; if (leftIntersects != null) { if (node.GetLeft() == null) { newRight = this.CreateNewNode(leftArea); newRight.AddActor(actor); node.SetChild(0, newRight); } else { this.InsertObject(actor, actorBounds, leftIntersects, leftArea, node.GetLeft(), result1, result2); } } if (rightIntersects != null) { if (node.GetRight() == null) { newRight = this.CreateNewNode(rightArea); newRight.AddActor(actor); node.SetChild(1, newRight); } else { this.InsertObject(actor, actorBounds, rightIntersects, rightArea, node.GetRight(), result1, result2); } } } else { node.AddActor(actor); } } }
private BSPCollisionNode CheckRemoveNode(BSPCollisionNode node) { int idx = 0; for (; idx < MAX_SIZE;) { if (node != null && node.IsEmpty()) { BSPCollisionNode parent = node.GetParent(); int side = (parent != null) ? parent.GetChildSide(node) : 3; BSPCollisionNode left = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left == null) { if (parent != null) { if (right != null) { right.SetArea(node.GetArea()); } parent.SetChild(side, right); } else { this.bspTree = right; if (right != null) { right.SetParent((BSPCollisionNode)null); } } node.SetChild(1, (BSPCollisionNode)null); ReturnNode(node); node = parent; continue; } if (right == null) { if (parent != null) { if (left != null) { left.SetArea(node.GetArea()); } parent.SetChild(side, left); } else { this.bspTree = left; if (left != null) { left.SetParent((BSPCollisionNode)null); } } node.SetChild(0, (BSPCollisionNode)null); ReturnNode(node); node = parent; continue; } } idx++; return(node); } return(null); }