|
本帖最后由 yugang2010 于 2011-10-17 11:04 编辑
osg在更新回调operator()函数中是不是仅仅能对当前节点进行姿态的控制,不能对当前节点进行添加节点的操作???谢谢
下面代码,检测某个节点是否发生碰撞。如果碰撞,添加paticle粒子效果。不知道这种方式是不是不正确。是否有其他思路,谢谢指点。
-
-
- class bulletcallback : public osg::NodeCallback
- {
- public:
- bulletcallback(osg::Group* group)
- {
- root=group;
- }
- virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
- {
-
- osg::MatrixTransform* pat=dynamic_cast<osg::MatrixTransform*> (node);
-
- osg::BoundingSphere bs = pat->getBound();
- osg::ref_ptr<osgUtil::LineSegmentIntersector> inter=new osgUtil::LineSegmentIntersector(bs.center(),bs.center()+osg::Vec3(0.0f,0.0f,bs.radius()));
- osgUtil::IntersectionVisitor v(inter.get());
- pat->accept(v);
- unsigned int size=inter->getIntersections().size();
- if ( size>0 ) {
- osg::Vec3 resultCoord=inter.get()->getIntersections().begin()->getWorldIntersectPoint();
- osg::Vec3 coord;
- coord[0]=resultCoord[0];
- coord[1]=resultCoord[1];
- coord[2]=resultCoord[2];
-
- float posx=coord[0];
- float posy=coord[1];
- float posz=coord[2];
- osg::ref_ptr<osg::Group> explode=new osg::Group();
- osg::Vec3 wind(1.0f,0.0f,0.0f);
- osg::Vec3 position(posx,posy,posz);
- osg::ref_ptr<osgParticle::ExplosionEffect> explosion=new osgParticle::ExplosionEffect(position,2.0f);
- osg::ref_ptr<osgParticle::ExplosionDebrisEffect> explosionDebri=new osgParticle::ExplosionDebrisEffect(position,2.0f);
- osg::ref_ptr<osgParticle::SmokeEffect> smoke=new osgParticle::SmokeEffect(position,5.0f);
- osg::ref_ptr<osgParticle::FireEffect> fire=new osgParticle::FireEffect(position,2.0f);
- explosionDebri->setWind(wind);
- explosion->setWind(wind);
- smoke->setWind(wind);
- fire->setWind(wind);
- explode->addChild(explosion.get());
- explode->addChild(explosionDebri.get());
- explode->addChild(smoke.get());
- explode->addChild(fire.get());
- root->addChild(explode.get());
- root->addChild(nod);
- }
- private : osg::Group* root;
- };
-
复制代码 |
|