首页 文章

OpenSceneGraph - 如何检测两个Node相互交叉?

提问于
浏览
1

我想在OpenSceneGraph中的两个节点之间发生冲突 . 我想要确保两个节点相交 . 怎么做 . 我显示如下我的功能来检测碰撞 .

void movetruck(osg::ref_ptr<osg::PositionAttitudeTransform> land, osg::ref_ptr<osg::PositionAttitudeTransform> truck, osg::ref_ptr<osg::Node> ob1, osg::ref_ptr<osg::Node> ob2)
{
    osg::Vec3d trPos = truck->getPosition();


    if ("must be true if both ob1 and ob2 are intersected")
    {
        trPos[2] += 0.01;
    }else{
        trPos[0] += 0.01;
    }
    truck->setPosition(trPos);
}

1 回答

  • 0

    OpenSceneGraph有 BoundingSphereBoundingBox . 后者仅用于 Geometry 节点(除非你自己实现一点),所以 BoundingSphere 是最简单的方法 . 这不会给出模型的正确交集,但是在两个球体之间,每个球体都在模型中心居中并且具有半径,使得球体完全包含所有模型(/子图) . 即如果模型的中心是(0,0,0)并且该点加速为(1,1,1),则半径将完全为 sqrt(3) .

    您可以通过调用 getBound() 来查询节点的 BoundingSphere ,并且可以通过在其中一个球体上调用 intersects 并使用另一个作为参数来查询两个球体的交集 .

    在你的情况下,它会是这样的

    if(ob1->getBound().intersects(ob2->getBound()))
    {
        trPos[2] += 0.01;
    }
    

相关问题