LIBGDX RayCast:我如何阻止射线? (如何返回世界上想要的夹具)

时间:2016-12-28 20:51:53

标签: java libgdx raycasting

你能帮我理解吗,

当它碰到墙壁时,我怎么能停止光线投射?

让我们说:

$==player, #==wall, %==Enemy, RayCast == ------.

我有这个级别:

___________________________


       #
       #
% -----#---$---
___________________________

在这种情况下,如何阻止敌人射击我?

我怎样才能停止光线投射,以便看到墙壁夹具后面有什么?"

现在我只是得到他们两个:

     RayCastCallback callback= new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {

            if (fixture.getFilterData().categoryBits == Application.PLAYER){
                return fraction;
            }


            if (fixture.getFilterData().categoryBits == Application.ENEMY){
                return fraction;
            }

            return 0;
        }
    };


    world.rayCast(callback, p1, p2);

那么可以达到的分数是多少?如果是这样,怎么样?

非常感谢!

1 个答案:

答案 0 :(得分:2)

采用这种方法,它可以满足我的需求:

private static final int NOTHING = 0;
private static final int WALL = 1;
private static final int PLAYER = 2;
private int type = NOTHING;
private Vector2 position;

@Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {

    if (fixture.getFilterData().categoryBits == Application.WALL){
        type = WALL;
    }
    if (fixture.getFilterData().categoryBits == Application.PLAYER){
        type = PLAYER;
    }

    return fraction;
}

所以当我打印类型时:

System.out.println(this.rayCastStatus);

结果将是当它投射到墙上时停止光线投射。

嗯,不是真的要停下来,换句话说,只是为了得到我需要的结果,在这种情况下,当墙壁出现时,不打印播放器。

从文档中,关于回报:

public interface RayCastCallback {
/** Called for each fixture found in the query. You control how     the ray cast proceeds by returning a float: return -1: ignore
* this fixture and continue return 0: terminate the ray cast return fraction: clip the ray to this point return 1: don't clip
* the ray and continue.
* 
* The {@link Vector2} instances passed to the callback will be reused for future calls so make a copy of them!
* 
* @param fixture the fixture hit by the ray
* @param point the point of initial intersection
* @param normal the normal vector at the point of intersection
* @return -1 to filter, 0 to terminate, fraction to clip the ray for closest hit, 1 to continue **/
public float reportRayFixture (Fixture fixture, Vector2 point, Vector2 normal, float fraction);
}