Game Development Reference
In-Depth Information
F IGURE 12.10
Identifying an object's location in a quad-tree.
struct QuadTreeNode
{
Vector3 position;
QuadTreeNode child[4];
};
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:
struct QuadTreeNode
{
// ... 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;
Search Nedrilad ::




Custom Search