/* * Insert the object into the quadtree. If the node * exceeds the capacity, it will split and add all * objects to their corresponding nodes. */ public void Insert(Sprite nsprite) { if (nodes[0] != null) { int index = GetIndex(nsprite.CollisionBox); if (index != -1) { nodes[index].Insert(nsprite); return; } } objects.Add(nsprite); if (objects.Count > maxobjects && level < maxlevels) { if (nodes[0] == null) { Split(); } int i = 0; while (i < objects.Count) { int index = GetIndex(objects.ElementAt(i).CollisionBox); if (index != -1) { nodes[index].Insert(objects.ElementAt(i)); objects.RemoveAt(i); } else { i++; } } } }
/* * Return all objects that could collide with the given object */ public List<Sprite> retrieve(List<Sprite> returnObjects, Sprite mainSprite) { int index = GetIndex(mainSprite.CollisionBox); if (index != -1 && nodes[0] != null) { nodes[index].retrieve(returnObjects, mainSprite); } returnObjects.AddRange(objects); return returnObjects; }