0

I'm implementing a quadtree structure, for simplifying collision code,. but I'm unsure as to the best practice for doing so. Currently, the quadtree creates subtrees during setup down to a preset maximum depth, then I insert objects into its appropriate tree, for use in pair generation(the actual maths stuff). However, I've heard of other approaches, which only generate subtrees when a certain number of objects are stored. I know my method has a space overhead, but might be computationally faster during update cycles. What would be the best way to handle it?

Ian Young
  • 1,712
  • 1
  • 16
  • 33

1 Answers1

2

One approach is to store k elements in each node, starting with one parent node which spans the entire collision space. When inserting element k+1, you subdivide the space and place the new element in the correct quadrant.

Additionally you can use this approach to statically allocate the data structure, assuming you know the maximum number of nodes that will be used, and that there will be some maximum density. This requires a fixed array of nodes and elements to be allocated for the life of the application, but it avoids costly dynamic allocations, which should be a speed gain.

Lokno
  • 590
  • 3
  • 15
  • Like an overflow mechanic? – Ian Young Dec 31 '13 at 05:12
  • Yes, so the quadtree expands adaptively. This is also nice because you can just define each node in the tree to hold only a set number of elements. In my own code I have a completely statically allocated quad tree that assumes some maximum number of nodes. – Lokno Dec 31 '13 at 05:17
  • I can see why you would like that approach due to the reduced memory footprint. My version works quite well too, but I'd like to know which method should have higher performance. – Ian Young Dec 31 '13 at 05:23
  • Your method sounds pretty static already, so it may not be much of a gain. I updated my post with more information, specifically that my method does not improve space performance. Its about avoiding dynamic memory allocations. However, the tree will grow based on the density of the data, so there may be slightly less structure to traverse. – Lokno Dec 31 '13 at 16:58