2016-12-28 33 views
1

能否請你幫我瞭解,LIBGDX RayCast:我如何阻止射線? (如何在世界上只返回想要的夾具)

我如何阻止光線投射,當它撞上了一堵牆?

假設:

$==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); 

所以這是可以實現的幾分之幾?如果是這樣,怎麼樣?

非常感謝!

+0

出於好奇,當它擊中牆壁,什麼是分數的價值? – John

+0

如果我System.out.println(分數);所以它是:「0.027499877」 – ROSA

回答

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); 
}