Role games code

Started by
1 comment, last by Evil Steve 12 years, 11 months ago
There is some code experts from jim adams's book Programming Role playing games
if(Size > m_MaxSize && Num > m_MaxPolygons) {
for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) {
XOff = (((i % 2) < 1) ? -1.0f : 1.0f) * (Size / 4.0f);
ZOff = (((i % 4) < 2) ? -1.0f : 1.0f) * (Size / 4.0f);
YOff = (((i % 8) < 4) ? -1.0f : 1.0f) * (Size / 4.0f);

// See if any polygons in new node bounding box
if(CountPolygons(XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f)) {

Node->Nodes = new sNode(); // Create new child node

// Sort the polygons with the new child node
SortNode(Node->Nodes,XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f);
}
}
does someone undertstand why we caculate the Xoff and Yoff and ZOff as this way,try to explain that code very detail don't use the depth math than I can undertstand !

Advertisement
It looks like standard bit-encoding for the child nodes of a node in an octree or quadtree.

Consider the case of an octree. With three bits to work with, you have the following values (or something close to this - no guarantee I'll get this exactly right):

000 : 0
001 : 1
010 : 2
011 : 3
100 : 4
101 : 5
110 : 6
111 : 7

Since a bit can have one of two values, it can encode whether the node is on the front or back side of a splitting plane. For an octree, the splitting planes are (typically) parallel to the cardinal planes, xy, yz, and zx. So, that's one bit for each plane, or 3 bits, which gives you the integers in the range [0, 7]. These in turn can serve as indices into an array of child nodes.

In short, it's just a convenient way of quickly finding a child node based on some query or other (e.g. a point classification query).
Cross Post, closed.

This topic is closed to new replies.

Advertisement