得到最远的光线交点

时间:2015-01-22 20:17:57

标签: c++ box2d

我希望得到两点之间最远的交点。但是当我这样做时:

RaysCastCallback callBack;
_world->RayCast(&callBack, in.p1, in.p2);

然后交点将是从p1到p2最近的交点。 有没有办法获得最远的交叉点,还是有更好的方法来实现我的目标?

1 个答案:

答案 0 :(得分:0)

如果你的回调是这样的,交叉点将只是最接近的 - 没有内置的回调默认为你提供最近的点。

这是一个回调,它将记录相交的最远点。

class RayCastClosestCallback : public b2RayCastCallback
{
public:
    bool m_hit;
    b2Vec2 m_point;

    RayCastClosestCallback() {
        m_hit = false;
    }

    float32 ReportFixture(b2Fixture* fixture, 
                          const b2Vec2& point, 
                          const b2Vec2& normal,
                          float32 fraction) {
        m_point = point;
        m_hit = true;
        return 1; // keep going and don't change the ray length
    }
};