Game Development Reference
F IGURE 12.10
Identifying an object's location in a quad-tree.
Testing which of the four areas an object lies in is a simple matter of comparing
the corresponding components of their position vector. For an object at (1,4,5) and
a QuadTreeNode at (2,0,0), we know that it must be in the top left area, as shown in
figure 12.10, because the x coordinate of the object is less than the node's coordinate
and the z coordinate is greater. We can calculate which child in the array to use with
the following simple algorithm:
// ... Other code as before ...
unsigned int getChildIndex(const Vector3 &object)
unsigned int index;
if (object.x > position.x) index += 1;
if (object.z > position.z) index += 2;