2014-03-28 18 views
0

我有幾個Shape對象 - Circle,RectanglePolygon與JavaFX中的.intersects()的2D碰撞 - 錯誤的準確性

我使用內置的.intersects(Bounds1, Bounds2)方法來檢測碰撞,但它是相當不準確的。

爲什麼碰撞準確性如此糟糕,我該如何改進?碰撞檢測對於多邊形對象是非常不好的。從字面上看,至少有50個像素關閉。

編輯:這裏是我的代碼:

for (int i = 0; i < shapes.size(); i++) { 
    Bounds b1 = shapes.get(i).getBoundsInParent();       
    Bounds b2 = shapes.get(i).getBoundsInParent(); 

if (b1.intersects(b2)) { 
    //intersection happened and I remove the shapes from the scene 
    } 
} 

,這是我Polygon s表示代表的ArrayList Shapes的代碼。

class Monster { 

Polygon poly; 

Monster() { 
    poly = new Polygon(new double[] { 125.0,15.0, 150.0,30.0, 150.0,60.0, 125.0,75.0, 100.0,60.0, 100.0,30.0 }); 
    } 
} 
+0

我從來沒有經歷過的JavaFX邊界相交的任何誤差。請編輯您的問題,以包含[最低限度,完整,可驗證的示例](http://stackoverflow.com/help/mcve)來演示此問題。 – jewelsea

+0

@jewelsea完成!添加了代表我的Monster class +碰撞檢測的代碼。 – Gregg1989

+0

感謝Gregg,你能否讓我可以複製粘貼來編譯和運行。 – jewelsea

回答

0

從您發佈的內容,您正在設置CircleRectangle的半徑,然後寬度和高度。但是你沒有指定位置。也許這是你的問題?

嘗試使用帶有xy座標的構造函數,看看是否改善了碰撞檢測。

所以像

Circle c1 = new Circle(10,10,20); // center at (10,10), radius of 20 
Rectangle r1 = new Rectangle(10,10,20,20); // upper left corner at (10,10), height and width of 20