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



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

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

我想检测矩形(上,下,左或右)的触摸边:

r1 overlap r2 on the left side

谁能给我这个代码,但这需要是快速的方法。

谢谢

您可以使用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    

最新更新