2016-06-21 28 views
0

我的libgdx遊戲中有激光,我想知道是否光束擊中目標。libgdx與線和多邊形的碰撞(Intersector)

我有一些功能(總是成真)有一些麻煩,所以我做了一個非常簡單的測試,我仍然是真的!

我錯過了什麼嗎?

爲什麼此函數返回true?

if(Intersector.intersectLinePolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) { 
    System.out.println("true"); 
} 

在此先感謝!

回答

0

我第一次使用Intersector時也被這個問題困住了。

intersectLinePolygon()方法工作在一條無限延伸的線上,而不僅僅是您指定的兩個點之間。

使用intersectSegmentPolygon()方法,你想要做什麼......

if(Intersector.intersectSegmentPolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) { 
    System.out.println("true"); 
} 
+0

謝謝!這解決了這個問題,我得到了這些函數之間的區別,但它不會是假的,因爲即使是無限的線條,它也不會在我的例子中相交? – user1818410

+0

它會在(0,0) –