Box2dx:在光线投射中指定要忽略的灯具?

时间:2010-04-05 00:43:00

标签: c# xna box2d

我游戏世界中的每个人都有附加传感器形状的灯具。当我进行光线投射时,它会击中这些灯具,但我只想击中至少一个不是传感器的灯具。这可能吗?

我正在使用Box2dx - C#端口。

此外,回调是做什么的?

     world.PhysicsWorld.RayCast((f, p, n, fr) =>
        {
            fixture = f;
            position = p;
            return fr;
        }, point1, point2);

这是我正在调用的光线投射功能。文档说回调可用于指定要返回的形状数量,但我不知道如何做到这一点:

    /// Ray-cast the world for all fixtures in the path of the ray. Your callback
    /// controls whether you get the closest point, any point, or n-points.
    /// The ray-cast ignores shapes that contain the starting point.
    /// @param callback a user implemented callback class.
    /// @param point1 the ray starting point
    /// @param point2 the ray ending point
    public void RayCast(Func<Fixture, Vector2, Vector2, float, float> callback, Vector2 point1, Vector2 point2)
    {
        RayCastInput input = new RayCastInput();
        input.maxFraction = 1.0f;
        input.p1 = point1;
        input.p2 = point2;

        _rayCastCallback = callback;
        _contactManager._broadPhase.RayCast(_rayCastCallbackWrapper, ref input);
        _rayCastCallback = null;
    }

1 个答案:

答案 0 :(得分:1)

由于没有其他人回答我会给你我能想到的东西,提供的文档有点粗略,功能显然依赖于另一段代码来完成实际工作而我不知道C#,所以我无法告诉你一切。

回调是给你结果的函数方法,你必须编写一个接受给定参数集的函数。你在调用RayCast时将该函数作为参数传递,当你发现重叠时你的函数会被调用,你的回调函数可能会返回一些值来指示是否继续光线投射,我认为你应该返回true或者假的。

选择仅使用非传感器阵列的灯具的最佳选择可能是在您的回调函数中检查这一点,并且仅在满足该条件时才采取行动。