bounding box growth

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

bounding box growth

Post by Seven »

code to find all encompassing boundingbox of the node, including all children


is there a better method for doing this?

Code: Select all

 
    class CSDummySceneNode : public ISceneNode
    {
    public:
        aabbox3d<f32> TotalBoundingBox;
 
        CSDummySceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id = -1,
            const core::vector3df& position = core::vector3df(0, 0, 0),
            const core::vector3df& rotation = core::vector3df(0, 0, 0),
            const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
            : ISceneNode(parent, mgr, id, position, rotation, scale)
        {
        }
 
        virtual void OnRegisterSceneNode()
        {
            if (IsVisible) SceneManager->registerNodeForRendering(this);
            ISceneNode::OnRegisterSceneNode();
        }
 
        //! Renders the node.
        virtual void render()
        {
            video::IVideoDriver* driver = SceneManager->getVideoDriver();
            driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
            driver->setTransform(video::ETS_WORLD, IdentityMatrix);
 
            // for debug purposes only:
            video::SMaterial mat;
 
            // overwrite half transparency
            if (DebugDataVisible & scene::EDS_HALF_TRANSPARENCY)
                mat.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
            driver->setMaterial(mat);
 
            // for debug purposes only:
            if (DebugDataVisible)
            {
                video::SMaterial m;
                m.Lighting = false;
                m.AntiAliasing = 0;
                driver->setMaterial(m);
 
                if (DebugDataVisible & scene::EDS_BBOX)
                {
                    driver->draw3DBox(getTotalTransformedBoundingBox(), video::SColor(255, 255, 255, 255));
                }
                if (DebugDataVisible & scene::EDS_BBOX_BUFFERS)
                {
                    driver->draw3DBox(getTotalTransformedBoundingBox(), video::SColor(255, 190, 128, 128));
                }
            }
        }
 
        virtual const core::aabbox3d<f32>& getBoundingBox() const
        {
            return aabbox3d<f32>(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
        }
 
        core::aabbox3d<f32> getTotalTransformedBoundingBoxRecursive(ISceneNode* node, aabbox3d<f32> &box)
        {
            ISceneNodeList::ConstIterator it = node->getChildren().begin();
            for (; it != node->getChildren().end(); ++it)
            {
                aabbox3d<f32> box2 = (*it)->getTransformedBoundingBox();
                if (!box2.isFullInside(box))
                {
                    if (box2.MinEdge.X < box.MinEdge.X) box.MinEdge.X = box2.MinEdge.X;
                    if (box2.MinEdge.Y < box.MinEdge.Y) box.MinEdge.Y = box2.MinEdge.Y;
                    if (box2.MinEdge.Z < box.MinEdge.Z) box.MinEdge.Z = box2.MinEdge.Z;
                    if (box2.MaxEdge.X > box.MaxEdge.X) box.MaxEdge.X = box2.MaxEdge.X;
                    if (box2.MaxEdge.Y > box.MaxEdge.Y) box.MaxEdge.Y = box2.MaxEdge.Y;
                    if (box2.MaxEdge.Z > box.MaxEdge.Z) box.MaxEdge.Z = box2.MaxEdge.Z;
                }
                box = getTotalTransformedBoundingBoxRecursive((*it), box);
            }
            return box;
        }
 
        core::aabbox3d<f32> getTotalTransformedBoundingBox()
        {
            aabbox3d<f32> box(-0.5,-0.5,-0.5,.5,.5,.5);
            box = getTotalTransformedBoundingBoxRecursive(this, box);
            return box;
        }
 
    };
 
Last edited by Seven on Sat Feb 14, 2015 9:19 pm, edited 2 times in total.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: bounding box growth

Post by Seven »

I should point out what I am trying....

game object class has a PrimarySceneNode of type CSDummySceneNode*;
all scenenodes of the object are a child of the primarySceneNode;
of course, then these children can have children and great grand children.
in the editor, the user can only select primarySceneNodes for editing.

something new that I am not sure how it will work out, but the need for this particular code is so the user can select the primaryscenenode using the total bounding box of all of it's lineage.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: bounding box growth

Post by CuteAlien »

Looks mostly fine. You can save a few lines by using aabbox3d::addInternalBox which does the adding of boxes which you did manually.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply