2014-03-28 19 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