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 void GetIntersectingObjects(float[] r, CollisionQuery query, ISet resultSet, BSPCollisionNode startNode) { lock (cacheNodeStack) { cacheNodeStack.Clear(); try { if (startNode != null) { CollectionUtils.Add(cacheNodeStack, startNode); } int idx = 0; for (; !(cacheNodeStack.Count == 0) && idx < MAX_SIZE;) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); lock (node) { if (node.GetArea().Intersects(r[0], r[1], r[2], r[3])) { IIterator i = node.GetActorsIterator(); for (; i.HasNext();) { Actor left = (Actor)i.Next(); if (query.CheckCollision(left) && !CollectionUtils.Contains(left, resultSet)) { CollectionUtils.Add(resultSet, left); } } BSPCollisionNode left1 = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left1 != null) { CollectionUtils.Add(cacheNodeStack, left1); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } } idx++; } } catch (Exception ex) { Loon.Utils.Debug.Log.Exception(ex); } } }
private void ReturnNode(BSPCollisionNode node) { cache[tail++] = node; if (tail == MAX_SIZE) { tail = 0; } size = MathUtils.Min(size + 1, MAX_SIZE); if (node.GetLeft() != null || node.GetRight() != null) { throw new Exception("Size Error !"); } }
public virtual IList GetObjects(Type cls) { lock (cacheSet) { CollectionUtils.Clear(cacheSet); } lock (cacheNodeStack) { if (this.bspTree != null) { CollectionUtils.Add(cacheNodeStack, this.bspTree); } for (; !(cacheNodeStack.Count == 0);) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); IIterator i = node.GetActorsIterator(); while (i.HasNext()) { Actor left = (Actor)i.Next(); if (cls == null || cls.IsInstanceOfType(left)) { CollectionUtils.Add(cacheSet, left); } } BSPCollisionNode left1 = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left1 != null) { CollectionUtils.Add(cacheNodeStack, left1); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } List <Actor> result = new List <Actor>(cacheSet.Count); for (IEnumerator it = cacheSet.GetEnumerator(); it.MoveNext();) { result.Add((Actor)it.Current); } return(result); } }
private Actor GetOnlyObjectDownTree(Actor ignore, RectBox r, CollisionQuery query, BSPCollisionNode startNode) { if (startNode == null) { return(null); } else { lock (cacheNodeStack) { cacheNodeStack.Clear(); if (startNode != null) { CollectionUtils.Add(cacheNodeStack, startNode); } while (!(cacheNodeStack.Count == 0)) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); if (node.GetArea().Intersects(r)) { Actor res = this.CheckForOnlyCollision(ignore, node, query); if (res != null) { return(res); } BSPCollisionNode left = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left != null) { CollectionUtils.Add(cacheNodeStack, left); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } } } return(null); } }
private Actor GetOnlyIntersectingDown(RectBox r, CollisionQuery query, Actor actor) { if (this.bspTree == null) { return(null); } else { lock (cacheNodeStack) { cacheNodeStack.Clear(); CollectionUtils.Add(cacheNodeStack, this.bspTree); int idx = 0; for (; !(cacheNodeStack.Count == 0) && idx < MAX_SIZE;) { BSPCollisionNode node = (BSPCollisionNode)cacheNodeStack .RemoveLast(); if (node.GetArea().Contains(r)) { Actor res = this.CheckForOnlyCollision(actor, node, query); if (res != null) { return(res); } BSPCollisionNode left = node.GetLeft(); BSPCollisionNode right = node.GetRight(); if (left != null) { CollectionUtils.Add(cacheNodeStack, left); } if (right != null) { CollectionUtils.Add(cacheNodeStack, right); } } } } return(null); } }
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); }