[osg-users] How to remove a node from its callback function?

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Tue Jun 2 07:14:47 PDT 2015


Hi "Dark Zero",
Could you please use a human-readable name, so mailing list users can 
address you with a name?

Your code probably throws an exception cause you are modifying the scene 
graph while traversing it.
Simply add your nodes to be removed to a list in your callback and 
remove them between frames.
There are multiple ways of doing this, but for starters you might split 
the viewer.run into a loop calling

viewer.advance();
viewer.eventTraversal();
viewer.updateTraversal();
viewer.renderingTraversals();

and perform the removal from the scene-graph before the updateTraversal

Cheers
Sebastian
> Hi,
>
> There is a Geode whose Geometry is a ball with a MatrixTransform() assigned above it. It's callback function makes it falls. When the ball intersects with the ground, I hope to remove it from the scene.
>
> The following code throws exception:
>
>
> Code:
>
> //inside the ball's callback
>
> virtual void operator()(osg::Node* node ,osg::NodeVisitor* nv)
> {
>      using namespace osg;
>      MatrixTransform* matrix_node = dynamic_cast<MatrixTransform*>(node);
>      Matrix matrix = matrix_node->getMatrix();
>
>      velocity += Vec3(0, 0, -0.002);
>
>      matrix.postMultTranslate(velocity);
>
>      matrix_node->setMatrix(matrix);
>
>      Vec3 now_position = start_position * matrix;
>
>      osgUtil::IntersectVisitor ivXY;
>
>      osg::ref_ptr<osg::LineSegment> lineXY = new osg::LineSegment(now_position, now_position+velocity);
>
>      ivXY.addLineSegment(lineXY);
>
>      GAME.main_camera->m_pHostViewer->getSceneData()->accept(ivXY) ;
>      if(ivXY.hits())
>      {
>          node->getParent(0)->removeChild(node);
>      }
>      return;
> }
>
>
>
>
> How to do it correctly? Thank you!
>
> ...
>
> Cheers,
> DarkZero
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=63915#63915
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




More information about the osg-users mailing list