# Role games code

This topic is 2611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

There is some code experts from jim adams's book Programming Role playing games
if(Size > m_MaxSize && Num > m_MaxPolygons) {
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 !

##### Share on other sites
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).

##### Share on other sites
Cross Post, closed.

##### Share on other sites

This topic is 2611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631725
• Total Posts
3001905
×