0

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++;
                }
            }
        }
    }
John Savage
  • 103
  • 6
  • Its a bit hard to undesrtand whats going o with the code just dumped like that. I had implemented a QuadTree before in C# and you ca take a look at it [here](https://github.com/KillaW0lf04/Some-2D-RPG/blob/master/GameEngine/DataStructures/QuadTree.cs) if it helps. I do recall once having a problem with QuadTrees similiar to yours. If I remember the cause ill let you know. – Michael Aquilina Sep 22 '13 at 10:36
  • Something else worth mentioning - in most cases its easier and faster to use a HashList which I also implemented [here](http://en.wikipedia.org/wiki/Hash_list). As I'm sure your noticing, QuadTrees can be difficult to debug and the smallest problem will break all of your code. If you do decide to go with a QuadTree, write lots of unit tests to test each portion of your code to make sure its working for you instead of against you. – Michael Aquilina Sep 22 '13 at 10:40

1 Answers1

0

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.

John Savage
  • 103
  • 6