我在我的游戏中使用了libGDX库。我使用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