首先,我知道已经回答了非常类似的问题,但是我还没有看到这个问题。
我有一个椭圆形和一个矩形,我想检测它们是否相交。我知道您可以使用两个矩形来做到这一点,如下所示:
if (new Rectangle(x1, y1, w1, h1).intersects(new Rectangle(x2, y2, w2, h2))) {
//code here for when collision occurs.
}
是否可以像这样的椭圆形做到这一点?
if (new Oval(x1, y1, w1, h1).intersects(new Rectangle(x2, y2, w2, h2))) {
//code here for when collision occurs.
}
提前致谢!
当我搜索一个椭圆形的类时,我没有看到它,因此我将以Ellipse为例。如果您在此处查看文档,则将看到Shape
该类的文档。
此类同时被扩展Rectangle
,Ellipse
并且还包含一个intersect(Shape, Shape)
方法,因此您将可以在这两个对象上使用相交。
如果您的Oval
类和Rectangle
类都Shape
在intersect(Shape, Shape)
继承自的场景中扩展了同一个类,那么它也将在您的场景中工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句