libGDX,检测矩形间的边缘接触(侧面碰撞)

4

我在我的游戏中使用了libGDX库。我使用overlap方法来检测两个矩形之间的碰撞。

...
if (r1.overlaps(r2)) collisionTest();
...

我想要检测矩形的触碰位置(顶部、底部、左边或右边):
r1 overlap r2 on the left side

有没有人能给我提供这方面的代码,但是需要快速的方法。

谢谢。

1个回答

10

您可以使用Intersector类提供的intersectRectangles方法来确定两个矩形是否重叠,如果重叠,则确定它们的重叠部分。您可以使用此信息确定它们是否与左侧、右侧、顶部和/或底部重叠。

Rectangle r1 = /*Initialize*/;                             
Rectangle r2 = /*Initialize*/;                             
Rectangle intersection = new Rectangle();                  
Intersector.intersectRectangles(r1, r2, intersection);     
if(intersection.x > r1.x)                                  
    //Intersects with right side                              
if(intersection.y > r1.y)                                  
    //Intersects with top side                                
if(intersection.x + intersection.width < r1.x + r1.width)  
    //Intersects with left side                               
if(intersection.y + intersection.height < r1.y + r1.height)
    //Intersects with bottom side    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接