有一個Geode
,其幾何是一個球,其上面分配有一個MatrixTransform()
。它的回調函數使它下降。當球與地面相交時,我希望將其從場景中移除。如何從其回調函數中刪除節點?
下面的代碼拋出異常:
//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;
}
如何正確做呢?謝謝!
不會分配'NULL'回調工作?類似'node-> addWhateverCallback(0)' –