OSG LineSegmentIntersector进行碰撞检测

1407 篇文章 2 订阅
118 篇文章 4 订阅

view plaincopy to clipboardprint?
#include <osgViewer/Viewer>  
#include <osgViewer/ViewerEventHandlers>  
#include <osgGA/StateSetManipulator>  
#include <osgDB/ReadFile>  
#include <osgUtil/LineSegmentIntersector>  
#include <osgUtil/IntersectionVisitor>  
#include <osg/MatrixTransform>  
#include <osg/Group>  
#include <osg/Node>  
int main()  
{  
    osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");//第一个节点  
    osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("axes.osg");//第二个节点  
    osg::ref_ptr<osg::MatrixTransform> mt_cow = new osg::MatrixTransform;  
    mt_cow->setMatrix(osg::Matrix::scale(osg::Vec3(0.5,0.5,0.5))*osg::Matrix::translate(osg::Vec3(-10,0,0)));  
    mt_cow->addChild(cow.get());  
    osg::ref_ptr<osg::MatrixTransform> mt_axes = new osg::MatrixTransform;  
    mt_axes->addChild(axes.get());  
    osg::ref_ptr<osg::Group> root = new osg::Group;  
    root->addChild(mt_cow.get());  
    root->addChild(mt_axes.get());  
    osgViewer::Viewer * viewer = new osgViewer::Viewer;  
    viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));  
    /************************************************************************/ 
    /* 碰撞检测                                                             */ 
    /************************************************************************/ 
    osg::Vec3 p_start(-100,0,0);  
    osg::Vec3 p_end(100,0,0);  
    osgUtil::LineSegmentIntersector::Intersections _intersections;  
    osg::ref_ptr< osgUtil::LineSegmentIntersector > _lineSegmentIntersector = new osgUtil::LineSegmentIntersector(p_start,p_end);  
    osgUtil::IntersectionVisitor _iv(_lineSegmentIntersector.get());  
    cow->accept(_iv);  
    _intersections=_lineSegmentIntersector->getIntersections();  
    int _intersectionNumber=_intersections.size();          
    if (_intersectionNumber!=0)  
    {           
        osgUtil::LineSegmentIntersector::Intersections::iterator hitr = _intersections.begin();  
        osg::Vec3 vec3_p1 =hitr->getWorldIntersectPoint()*mt_cow->getMatrix();//这样得到的交点就是绝对的世界坐标了。  
        mt_axes->setMatrix(osg::Matrix::translate(vec3_p1));//把坐标系原点移动到交点,可以测试现在的坐标系原点是不是刚好和牛相交!  
        hitr++;  
        osg::Vec3 vec3_p2 =hitr->getWorldIntersectPoint()*mt_cow->getMatrix();  
    }  
    viewer->setSceneData(root.get());  
    return viewer->run();  
      

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector>
#include <osgUtil/IntersectionVisitor>
#include <osg/MatrixTransform>
#include <osg/Group>
#include <osg/Node>
int main()
{
 osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");//第一个节点
 osg::ref_ptr<osg::Node> axes = osgDB::readNodeFile("axes.osg");//第二个节点
 osg::ref_ptr<osg::MatrixTransform> mt_cow = new osg::MatrixTransform;
 mt_cow->setMatrix(osg::Matrix::scale(osg::Vec3(0.5,0.5,0.5))*osg::Matrix::translate(osg::Vec3(-10,0,0)));
 mt_cow->addChild(cow.get());
 osg::ref_ptr<osg::MatrixTransform> mt_axes = new osg::MatrixTransform;
 mt_axes->addChild(axes.get());
 osg::ref_ptr<osg::Group> root = new osg::Group;
 root->addChild(mt_cow.get());
 root->addChild(mt_axes.get());
 osgViewer::Viewer * viewer = new osgViewer::Viewer;
 viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
 /************************************************************************/
 /* 碰撞检测                                                             */
 /************************************************************************/
 osg::Vec3 p_start(-100,0,0);
 osg::Vec3 p_end(100,0,0);
 osgUtil::LineSegmentIntersector::Intersections _intersections;
 osg::ref_ptr< osgUtil::LineSegmentIntersector > _lineSegmentIntersector = new osgUtil::LineSegmentIntersector(p_start,p_end);
 osgUtil::IntersectionVisitor _iv(_lineSegmentIntersector.get());
 cow->accept(_iv);
 _intersections=_lineSegmentIntersector->getIntersections();
 int _intersectionNumber=_intersections.size();       
 if (_intersectionNumber!=0)
 {        
  osgUtil::LineSegmentIntersector::Intersections::iterator hitr = _intersections.begin();
  osg::Vec3 vec3_p1 =hitr->getWorldIntersectPoint()*mt_cow->getMatrix();//这样得到的交点就是绝对的世界坐标了。
  mt_axes->setMatrix(osg::Matrix::translate(vec3_p1));//把坐标系原点移动到交点,可以测试现在的坐标系原点是不是刚好和牛相交!
  hitr++;
  osg::Vec3 vec3_p2 =hitr->getWorldIntersectPoint()*mt_cow->getMatrix();
 }
 viewer->setSceneData(root.get());
 return viewer->run();
 
}

在参考了多个例子后写了这么一个例子,总算明白了进行线段相交的各个步骤的含义了。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/include1224/archive/2010/03/09/5357741.aspx

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值