如何检查libgdx中的交集?

时间:2014-06-09 11:16:29

标签: java android libgdx

我有两个带有两个纹理的精灵。如何检查它们是否相交? 我使用的是libgdx 0.9.8。

2 个答案:

答案 0 :(得分:8)

if (sprite1.getBoundingRectangle().contains(Sprite2.getBoundingRectangle())
{
    //gives true when sprite2 one is completly inside sprite1
}

if(sprite1.getBoundingRectangle().overlaps(Sprite2.getBoundingRectangle())
{
    // gives true when sprite 2 intersects sprite1
}

答案 1 :(得分:3)

Intersector有很多方法可以检查重叠和交叉。不仅有矩形方法,还有圆形,点,多边形等。

在您的情况下,您可以使用以下内容:

Rectangle boundingRect1 = sprite1.getBoundingRectangle();
Rectangle boundingRect2 = sprite2.getBoundingRectangle();

if (Intersector.overlaps(boundingRect1, boundingRect2)) {
    ...
}