Question

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++;
                }
            }
        }
    }
Was it helpful?

Solution

I found the problem, but I obliviously left that chunk of code out of the question.

For a purpose other than the quad tree functionality, I was keeping track of each individual quad in game layers. So the quads were children to not only their parent quad, but also a game layer. When I was deleting the quads, I was clearing them from their parent quads but not from their parent game layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top