Page 1 of 1

How to change parent of a Scenenode (detach/attach)

Posted: Wed Nov 15, 2017 5:23 am
by Garibalde
I am trying to remove a scenenode and to reattach it to another scenenode as a child of that node.
I thought this would be very easy to do. But I think I am missing something as only a part of the object is reattached (i.e the top node).
the child nodes just disappears.

Below is my scene loaded with "dotsceneloader"

Code: Select all

mloader.parseDotScene("Tool.scene", "General", mSceneMgr, NULL, prepend.c_str());
Here is my "Tool.scene"

Code: Select all

<scene formatVersion="1.0" upAxis="z" unitsPerMeter="100" unitType="centimeters" minOgreVersion="1.9" ogreMaxVersion="2.6.4" author="OgreMax Scene Exporter (www.ogremax.com)" application="3DS Max">
    <environment>
        <colourAmbient r="0.333333" g="0.333333" b="0.333333"/>
        <colourBackground r="0" g="0" b="0"/>
        <clipping near="0" far="100000"/>
    </environment>
    <nodes>
        <node name="Tool">
            <position x="0" y="0" z="0"/>
            <scale x="1" y="1" z="1"/>
            <rotation qx="0" qy="0" qz="0" qw="1"/>
            <entity name="Tool" castShadows="true" receiveShadows="true" meshFile="Tool.mesh">
                <subentities>
                    <subentity index="0" materialName="Tool_A_m1"/>
                    <subentity index="1" materialName="Tool_B_m1"/>
                </subentities>
            </entity>
            <node name="Tool_Roller">
                <position x="0" y="0" z="0"/>
                <scale x="1" y="1" z="1"/>
                <rotation qx="-2.45869e-007" qy="-4.47035e-008" qz="-2.16067e-007" qw="1"/>
                <entity name="Tool_Roller" castShadows="true" receiveShadows="true" meshFile="Tool_Roller.mesh">
                    <subentities>
                        <subentity index="0" materialName="Tool_A_m1"/>
                    </subentities>
                </entity>
            </node>
        </node>
    </nodes>
</scene>
After the Dotsceneloader call. I can see my Tool on the scene at the origin (0,0,0) as expected. Both parts Tool and Tool_Roller are visible.
Then when I want to switch the object to be a child of "Hand". Here si the code I use.

Code: Select all

Ogre::SceneNode* MyToolSceneNode = mSceneMgr->getSceneNode(Toolname);

//! Reassign the tool to the Hand.
Ogre::SceneNode* MyHandSceneNode = mSceneMgr->getSceneNode(ObjHitData.HitObjName);
Ogre::SceneNode* MyHandToolNode = MyHandSceneNode->createChildSceneNode();

Ogre::MovableObject* MyMoveObj = MyToolSceneNode->getAttachedObject(Toolname);
Ogre::MovableObject* MyToolMoveObj = mSceneMgr->getMovableObject(Toolname, Ogre::EntityFactory::FACTORY_TYPE_NAME);
MyToolSceneNode->removeAllChildren();
MyToolSceneNode->detachAllObjects();

MyHandToolNode->attachObject(MyToolMoveObj);
MyHandToolNode->setPosition(0.0, 0.0, 5.0);
After this only the top Tool.mesh is moved correctly.. the Tool_roller.mesh is not visible.
it seems the move does not move the complete tree of "Tool" only the top element.

How do I move all the children of tool from a scenenode to another scenenode.

Thanks

Re: How to change parent of a Scenenode (detach/attach)

Posted: Wed Nov 15, 2017 10:35 pm
by Lax
Hi,

when you re-attach a node, its position and orientation will be changed, so you need to store it before you do the re-attaching like that:

Code: Select all

Ogre::Vector3 resultPosition = node1->convertWorldToLocalPosition(node2->getPosition());
Ogre::Quaternion resultOrientation = node1->convertWorldToLocalOrientation(node2->getOrientation());

node1->getParentSceneNode()->removeChild(node2);
node1->addChild(node2);
node2->setPosition(resultPosition);
node2->setOrientation(resultOrientation);
node2->setInheritScale(false);
Regards
Lax

Re: How to change parent of a Scenenode (detach/attach)

Posted: Sat Nov 18, 2017 2:14 am
by Garibalde
Thanks i was over complicating the issue with removing and detach etc..
it worked.

Re: How to change parent of a Scenenode (detach/attach)

Posted: Wed Nov 14, 2018 6:55 pm
by tomecki
Very nice advice Garibalde, but there is one thing here worth to mention.
You assumed that in initial state the child node is under root scene node. It may lead to some issues. So in general first two lines of the code should be like:

Code: Select all

Ogre::Vector3 resultPosition = node1->convertWorldToLocalPosition(node2->getPosition());
Ogre::Quaternion resultOrientation = node1->convertWorldToLocalOrientation(node2->getOrientation());
I know this is old topic, but I faced very recently errors related to what I wrote above.

Re: How to change parent of a Scenenode (detach/attach)

Posted: Wed Nov 14, 2018 10:27 pm
by Lax
Hi tomecki,

what is different in the code of your last post, comparing to my post? Where is the issue?

Regards
Lax