|
想实现的功能:当吊具下放的时候检测,是否碰到集装箱,是的话停止下放,可惜不知道哪出了什么问题,我觉得关键可能是无法获取DOFTransfrom节点的position值,所以检测线段构造有问题,请教大家怎么解决,谢谢了
#include "chqDataType.h"
using std::endl;
chqDataType::chqDataType(osg::Node* n)
{
m_x=0.0;
m_z=0.0;
//获取吊具节点
findNodeVisitor findDiaoJu("master_1");
n->accept(findDiaoJu);
diaojuNode=dynamic_cast<osgSim:OFTransform*>(findDiaoJu.getFirst());
//获取吊绳节点
findNodeVisitor findRope("rope_1");
findDiaoJu.getFirst()->accept(findRope);
ropeNode=dynamic_cast<osgSim::DOFTransform*>(findRope.getFirst());
findNodeVisitor findRopeC("rope_c_1");
findDiaoJu.getFirst()->accept(findRopeC);
ropeCNode=dynamic_cast<osgSim::DOFTransform*>(findRopeC.getFirst());
//获取吊钩节点
findNodeVisitor findHook("hook_pj_1");
findDiaoJu.getFirst()->accept(findHook);
hookNode=dynamic_cast<osgSim::DOFTransform*>(findHook.getFirst());
}
chqDataType::~chqDataType(void)
{
}
void chqDataType::updateDiaoJuMove()
{
if (m_x<=12.0)
{
//移动吊具
m_x += 0.1;
diaojuNode->setCurrentTranslate(osg::Vec3(m_x,0,0));
}
else
{
//放下吊钩
updateHookDown();
}
}
void chqDataType::updateHookDown()
{
if (m_z>=-8)
{
//放下吊钩
m_z-=0.05;
hookNode->setCurrentTranslate(osg::Vec3(0,0,m_z));
//伸缩吊绳
ropeNode->setCurrentScale(osg::Vec3(1,1,1+abs(m_z)/3));
ropeCNode->setCurrentScale(osg::Vec3(1,1,1+abs(m_z)/1.55));
osgUtil::IntersectVisitor iv;
osg::ref_ptr<osg:ineSegment> segDown=new osg::LineSegment();
const osg::BoundingSphere& bs=hookNode->getBound();
float zMax=bs.center().z()+bs.radius();
float zMin=bs.center().z()-bs.radius();
std::cout<<zMax<<zMin<<std::endl;
segDown->set(osg::Vec3(-20,0,20),osg::Vec3(-20,0,0));
iv.addLineSegment(segDown.get());
hookNode->accept(iv);
if(iv.hits())
{
osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get());
if(!hitList.empty())
{
osgUtil::Hit heightTestResults;
heightTestResults = hitList.front();
//与箱子发生碰撞,因此停止运动
std::cout<<"吊到箱子了"<<std::endl;
}
}
}
} |
|