I'm trying to implement a quad tree into a small project just to practice. I have several particles bouncing around within a radius, and a quad tree is bound to the circle that it forms.
Everything works, except I believe there is some kind of leak in either my Insertion or Clear. After running for about 10 seconds with 1000 particles, it begins to lag horribly.
Here are the functions
_subDivisions is an array, _drawableGameObjects is a list
public void Clear()
{
_drawableGameObjects.Clear();
if (_subDivisions != null)
foreach (QuadTree quad in _subDivisions)
quad.Clear();
_subDivisions = null;
}
public void Insert(DrawableGameObject drawableGameObject)
{
if (_subDivisions != null)
{
int index = GetIndex(drawableGameObject);
if (index > -1)
_subDivisions[index].Insert(drawableGameObject);
return;
}
_drawableGameObjects.Add(drawableGameObject);
if (_drawableGameObjects.Count > _maxObjects && _level < _maximumLevel)
{
Subdivide();
int i = 0;
while (i < _drawableGameObjects.Count)
{
int index = GetIndex(_drawableGameObjects[i]);
if (index > -1)
{
DrawableGameObject currentObject = _drawableGameObjects[i];
_subDivisions[index].Insert(currentObject);
_drawableGameObjects.Remove(currentObject);
}
else
{
i++;
}
}
}
}