LIBGDX RayCast:如何停止射线? (如何return只是世界上想要的夹具)
LIBGDX RayCast: how do i stop the ray? (How to return just the wanted fixture in the world)
你能帮我理解一下吗,
光线投射撞墙时如何停止?
假设:
$==player, #==wall, %==Enemy, RayCast == ------.
我有这个水平:
___________________________
#
#
% -----#---$---
___________________________
在这种情况下,如何才能阻止敌人向我射击?
我怎样才能停止 "to see what there is after the wall fixture" 的光线投射?
现在我只得到它们:
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);
所以是分数可以达到这个目的?如果可以,怎么做?
非常感谢!
通过这种方法,它可以满足我的需要:
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);
结果将是停止光线投射,当它撞到墙上时。
好吧,不是真的要停下来,换句话说,只是为了得到我需要的结果,在这种情况下,当墙先出现时,不是打印播放器。
来自文档,关于 return:
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);
}
你能帮我理解一下吗,
光线投射撞墙时如何停止?
假设:
$==player, #==wall, %==Enemy, RayCast == ------.
我有这个水平:
___________________________
#
#
% -----#---$---
___________________________
在这种情况下,如何才能阻止敌人向我射击?
我怎样才能停止 "to see what there is after the wall fixture" 的光线投射?
现在我只得到它们:
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);
所以是分数可以达到这个目的?如果可以,怎么做?
非常感谢!
通过这种方法,它可以满足我的需要:
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);
结果将是停止光线投射,当它撞到墙上时。
好吧,不是真的要停下来,换句话说,只是为了得到我需要的结果,在这种情况下,当墙先出现时,不是打印播放器。
来自文档,关于 return:
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);
}