Sign in to follow this  
freeworld

implementing a generic quad tree.

Recommended Posts

freeworld    341
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.

Share this post


Link to post
Share on other sites
frob    44908
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. Edited by frob

Share this post


Link to post
Share on other sites
freeworld    341

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?

Edited by freeworld

Share this post


Link to post
Share on other sites
larspensjo    1561

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this