public void Remove(Collidable obj) { QuadTreeNode node = obj.GetMapArea(); if (node != null) { obj.GetMapArea().Contents.Remove(obj); } }
/// <summary> /// Updates the object and returns whether it still belongs to this node /// </summary> /// <param name="obj">Object to Update in the quad tree</param> /// <returns>Whether the object moved</returns> private bool UpdateObject(Collidable obj) { obj.ClearMapArea(); lock (_locker) { this.Contents.Remove(obj); } // Check if object has left the bounds of this node and is not root if (!this.Bounds.Contains(obj.GetBounds()) && this.Parent != null) { // We now belong to a parent this.Parent.ReverseInsert(obj); return(true); } else // We're within the same node, but could be in children, must insert { this.Insert(obj); // If we didn't move quad tree nodes if (obj.GetMapArea() == this) { return(false); } else { return(true); } } }
public void Update(GameTime gameTime) { bool[] objectsToKeepAround = new bool[_objects.Count]; Parallel.For(0, _objects.Count, i => { Collidable thisObject = _objects[i]; if (thisObject.Disposed) { objectsToKeepAround[i] = false; return; } // Retrieve objects that it could be colliding with List <Collidable> potentials = thisObject.GetMapArea().GetSubTreeContents(); for (int j = 0; j < potentials.Count; j++) { Collidable thisPotential = potentials[j]; // If the potential object is our outer object then move on if (thisPotential.Collided || thisPotential.ServerID() == thisObject.ServerID()) { continue; } if (thisObject.IsCollidingWith(thisPotential)) { thisObject.HandleCollisionWith(thisPotential, _space); thisPotential.HandleCollisionWith(thisObject, _space); if (thisObject.Disposed) { objectsToKeepAround[i] = false; return; } } } objectsToKeepAround[i] = true; }); for (int i = objectsToKeepAround.Length - 1; i >= 0; i--) { if (!objectsToKeepAround[i]) { _objects[i] = _objects[_objects.Count - 1]; _objects.RemoveAt(_objects.Count - 1); } } }
/// <summary> /// Will return all objects "underneath" (in the tree) the obj. /// Called partial because if an object "above" the obj is colliding /// with it this function will not return that object. Therefore when /// calling this function be sure to traverse the whole collision list. /// </summary> /// <param name="obj">Object to check collisions</param> public List <Collidable> GetPartialCollisionCheckList(Collidable obj) { return(obj.GetMapArea().GetSubTreeContents()); }
/// <summary> /// Updates the object and returns whether it still belongs to this node /// </summary> /// <param name="obj">Object to Update in the quad tree</param> /// <returns>Whether the object moved</returns> private bool UpdateObject(Collidable obj) { obj.ClearMapArea(); lock (_locker) { this.Contents.Remove(obj); } // Check if object has left the bounds of this node and is not root if (!this.Bounds.Contains(obj.GetBounds()) && this.Parent != null) { // We now belong to a parent this.Parent.ReverseInsert(obj); return true; } else // We're within the same node, but could be in children, must insert { this.Insert(obj); // If we didn't move quad tree nodes if (obj.GetMapArea() == this) { return false; } else { return true; } } }
/// <summary> /// Will return all objects "underneath" (in the tree) the obj. /// Called partial because if an object "above" the obj is colliding /// with it this function will not return that object. Therefore when /// calling this function be sure to traverse the whole collision list. /// </summary> /// <param name="obj">Object to check collisions</param> public List<Collidable> GetPartialCollisionCheckList(Collidable obj) { return obj.GetMapArea().GetSubTreeContents(); }