Hello everyone, I’ve been lurking on this board for a bit and now I’d like to post a discussion topic, I hope I’m not making any faux-pas. I don’t think I’m stepping on any toes.
There have been discussions elsewhere about Quadtrees vs. Grids for optimized collision detection and the opinions of people who seem to know what they are talking about is:
- Grids save coder effort vs. Quadtrees
- Quadtrees are only useful for enormous maps with tons and tons of entities.
- The basic Quadtree implementation that you see on Wikipedia, forums, and elsewhere is unoptimized and modern quadtrees in real games are tuned to reduce the search size when looking for potential collisions.
I’ve got a few ideas for games that involve massive generated worlds, 2d spaces that will have coordinates into the millions, hundreds of millions, even potentially “infinite” if I can work out a good algorithm for an infinite map. Imagine something like a 2d EVE or Skyrim, so I’m working on an engine that will let me experiment and I’m definitely going to need collision detection across an enormous area. I think I need a quadtree.
So my question is what resources exist for optimizing giant quadtrees (specifically a region quadtree that looks for collisions between rectangular entities and map squares. I’ve heard it helps to keep everything in leaf nodes, but I don’t see how that’s possible. There seems to be an opinion that if there are really large numbers of entities moving around, rebalancing is needed, and in general handling entities moving from quadtree node to quadtree node dynamically isn’t discussed in the public presentation of trees that you see.
Second question, lets say my map consists of a regular arrangement of equally sized squares, perhaps broken down into a grid of regions that are dynamically generated as the player explores. What’s the best way to check for collisions between entities in the quadtree and grid squares. Should the map grid just be in the tree instead of in a separate grid? That seems like it could get out of hand for a huge map, but maybe I’m not thinking about the equal space and search requirements for a big enough grid and I should just keep all collision / proximity testing in the same data structure. Eventually it seems like there would be a need to load only parts of the map and corresponding areas of the quadtree at any one time.
As a reference point I’m looking at this quadtree implementation as an example of a “typical” unoptimized tree:
http://code.google.com/p/slick2d-tests/source/browse/SlickTutorials/trunk/src/de/myreality/dev/slick/collisions/Quadtree.java?r=3
Though there are others out there that are mostly similar. It does no rebalancing and can require a very deep search comparing many entities of many different levels. Also if the bounds on the outermost tree node are very large (say 2 ^ some significant power) and there are many entities clustered in a small local area, the tree will have to be divided into many many sublayers for a small number of entities and many nodes will be used to hold large empty areas.
Ideally, there’s a discussion in a journal, magazine, or textbook about this topic. I have access to a university library.
Thanks!