implementing a generic quad tree.

Started by
2 comments, last by larspensjo 11 years, 2 months ago
As a learning experience im putting together a templated quad tree as a container. But theres a few things im curious about and google on my phone is being a nuisance.

Im storing objects by there center and forcing them to only one quad at a time. My tree will be populated quite heavily with a limit to the amount per quad. My biggest concern lies in moving the objects. This will probably be trivial... 100 or more. Per frame with that number being very dynamic aswell as my tree possible being 20-30 + nodes deep.

The way im doing it right now is the owner of the objects will remove them from the tree then reinsert them every time it moves.

But all morning I was thinking maybe I should give a member function to my nodes to move an object. So I could do it logically. Such as no need to reinsert if it still is in the current node.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
I think you would be better served with a loose quadtree, which allows for a bigger range of motion because each quadtree node has motion on it.


As for your tree being "20-30+ nodes deep", you must either have an amazingly big world or a extremely tiny subdivision space.

At 1 meter per cell, a 20 deep quadtree will cover an area of just over one million meters by one million meters. Roughly the entire land mass of New York City at a one-meter-per-cell ratio.

A 30 deep quadtree will cover over the entire state of California, or the entire state of Texas, at one millimeter per cell accuracy.


For our complete 10km square world we have a depth 10 loose quadtree at 1 cell per meter.

I think you would be better served with a loose quadtree, which allows for a bigger range of motion because each quadtree node has motion on it.


As for your tree being "20-30+ nodes deep", you must either have an amazingly big world or a extremely tiny subdivision space.

At 1 meter per cell, a 20 deep quadtree will cover an area of just over one million meters by one million meters. Roughly the entire land mass of New York City at a one-meter-per-cell ratio.

A 30 deep quadtree will cover over the entire state of California, or the entire state of Texas, at one millimeter per cell accuracy.


For our complete 10km square world we have a depth 10 loose quadtree at 1 cell per meter.

It came out right in my head.... I meant it could be checking that many nodes each time, (5 - 7 nodes deep multiplied by the 4 nodes each might possibly check). I'm using the quad tree for several objects in my game, all but one are very static so I don't have to worry about those.

One container stores all the aI-creatures though, and I use a system similar to diablo where only awake creatures are ever updated, and any creatures outside a certain range from the player are automatically considered a sleep. So in theory I will only ever move creatures in close range to the player. But these objects can move from one side of the world to the other instantly but that shouldn't happen often.

The other big question I forgot to ask, is an easy way to find all the nodes that border a node?

[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

I'm using the quad tree for several objects in my game, all but one are very static so I don't have to worry about those.

If you need to detect collisions between any two of these objects, then this may be a good choice. However, otherwise consider using more than one quad tree. If there is only one of the objects that isn't static, then it may be an advantage to have a quad tree of its own for this object type.

...Such as no need to reinsert if it still is in the current node.

Are you sure you need this? It is a performance optimization, but do you have measurements that show this is needed?

Using more than one quadtree is a simple solution that can also improve performance.

[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

This topic is closed to new replies.

Advertisement