|
先说下我的问题吧,我在osg中放了两个球movingNode,TargetNode。用movingNode向TargetNode移动,在前者的内部做一条以包围盒中心点和边界点为端点的线段,就用这条线段与TargetNode求交以做碰撞检测。
但是我发现明明两个球没有接触,就检测到碰撞了。
下面是我的一些代码
一个handler,控制小球移动,并每帧检测碰撞- class PickHandler : public osgGA::GUIEventHandler {
- public:
- PickHandler(){}
- ~PickHandler() {}
- bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
-
- };
- bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
- {
- switch(ea.getEventType())
- {
-
- case(osgGA::GUIEventAdapter::KEYDOWN):
- {
- if (ea.getKey()=='1')
- {
- osg::Matrix d = mtl->getMatrix();
- osg::Vec3 v = d.getTrans();
- v.set(v.x()+0.5,v.y(),v.z());
- d.setTrans(v);
- mtl->setMatrix(d);
- }
- if (ea.getKey()=='2')
- {
- osg::Matrix d = mtl->getMatrix();
- osg::Vec3 v = d.getTrans();
- v.set(v.x()- 0.5,v.y(),v.z());
- d.setTrans(v);
- mtl->setMatrix(d);
- }
- return false;
- }
- case(osgGA::GUIEventAdapter::FRAME):
- {
-
-
- if (tankLocationSegment->containsIntersections())
- {
-
- cout<<"1"<<endl;
- }
-
- }
- default:
- return false;
- }
- }
复制代码 一些全局变量- osg::ref_ptr<osg::MatrixTransform> mtl;
- osg::ref_ptr<osg::MatrixTransform> mtr;
- osg::ref_ptr<osg::Geode> movingNode;
- osg::ref_ptr<osg::Geode> targetNode;
- osgUtil::IntersectionVisitor findTankElevationVisitor;
- osgUtil::LineSegmentIntersector* tankLocationSegment;
复制代码 主函数- int _tmain(int argc, _TCHAR* argv[])
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
- osg::ref_ptr<osg::Group> root = new osg::Group;
-
- mtl = new osg::MatrixTransform;
- movingNode = new osg::Geode;
- movingNode = createBall();
- mtl->addChild(movingNode);
- root->addChild(mtl);
-
- mtr = new osg::MatrixTransform;
- mtr->setMatrix(osg::Matrix::translate(3.0,0.0,0.0));
- targetNode = new osg::Geode;
- targetNode = createBall();
- mtr->addChild(targetNode);
- root->addChild(mtr);
- //
-
- osg::BoundingSphere bs = movingNode->getBound();
- osg::Vec3 cen = bs.center();
- osg::Vec3 edge = bs.center()+osg::Vec3(1.0f,0.0f,0.0f);
-
- tankLocationSegment = new osgUtil::LineSegmentIntersector(cen,edge);
- findTankElevationVisitor.setIntersector(tankLocationSegment);
- targetNode->accept(findTankElevationVisitor);
-
- //把用于碰撞检测的线画出来
- osg::ref_ptr<osg::Geometry> line = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> lineVertices = new osg::Vec3Array;
- line->setVertexArray(lineVertices.get());
- lineVertices->push_back(cen);
- lineVertices->push_back(edge);
- line->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::LINES,0,2));
- osg::ref_ptr<osg::Geode> lineNode = new osg::Geode;
- lineNode->addDrawable(line.get());
- root->addChild(lineNode);
-
- viewer->setSceneData(root);
- viewer->addEventHandler(new PickHandler());
- viewer->run();
- return 0;
- }
复制代码 两个球的位置,以及实际的那条线的位置
|
|