我有两个带有不同纹理的精灵。如何检查它们是否相交? 我正在使用libgdx 0.9.8。
if (sprite1.getBoundingRectangle().contains(Sprite2.getBoundingRectangle())
{
//gives true when sprite2 one is completly inside sprite1
}
also
if(sprite1.getBoundingRectangle().overlaps(Sprite2.getBoundingRectangle())
{
// gives true when sprite 2 intersects sprite1
}
Intersector
提供了很多检查重叠和交集的方法,不仅适用于矩形,还包括圆形、点、多边形等。
在你的情况下,你可以使用以下方法:
Rectangle boundingRect1 = sprite1.getBoundingRectangle();
Rectangle boundingRect2 = sprite2.getBoundingRectangle();
if (Intersector.overlaps(boundingRect1, boundingRect2)) {
...
}