Skip to main content
gamedev.net
🔒 Locked

Octree object insertion problem

Started by Fluxtah 1 reply 950+ views
Original Post
Fluxtah
Fluxtah
Hi I am trying to write a method that will put an object into the correct octree node. I am stuck with a nasty if statement and I cant think any further on how to insert it more effectively. This is the structure of one of my node:

        //           .--------------.
        //    8 --->/       /      /|<--- 5
        //         /--------------/ |
        //        /       /      /| |
        //       .--------------. | |
        //       |       |      | |/|
        // 7 --->|   4   |  1   | / |<--- 6
        //       |       |      |/|6|
        //       |-------|------| | .
        //       |       |      | |/
        //       |   3   |  2   | /
        //       |       |      |/
        //       .--------------.
This is my unfinished method where Center is the center point of the current node.

        public void InsertObject(Renderable obj)
        {
            bool straddle = false;
            if (obj.BoundingBox.Max.X < Center.X)
            {
                // left of x plane
            }
            else if (obj.BoundingBox.Min.X > Center.X)
            {
                // right of x plane
            }
            else
            {
                straddle = true;
            }

            if (obj.BoundingBox.Max.Y < Center.Y)
            {
                // bottom of y plane
            }
            else if (obj.BoundingBox.Min.Y > Center.Y)
            {
                // top of y plane
            }
            else
            {
                straddle = true;
            }

            if (obj.BoundingBox.Max.Z < Center.Z)
            {
                // back of z plane
            }
            else if (obj.BoundingBox.Min.Z > Center.Z)
            {
                // front of z plane
            }
            else
            {
                straddle = true;
            }
        }



I am doing a test on the objects AABB to see which side of the XYZ planes the object will be on or wether it will straddle a plane, in that case I will insert it into the current node. I just think that this test there is to many steps, I will be left with a result such as left, top, back, or right, bottom, front but then I will need to figure out which node that corresponds to, which I could do, but thats an additional check to what I have already. Any advice on how I could achieve this in a more efficient manner? I am probably going about this the wrong way!
A wise man can learn more from a foolish question, than a fool can learn from a wise answer - Bruce Lee
coelurus
coelurus
If you access the octree children via an array (children[0..7]), then you can simply count up an index lookup variable:

int index = 0;if (left) index++;if (up) index += 2;if (away) index += 4;...children[index] = nodeNew;


You'll probably have to adjust the additions to the proper axes.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.