我为我的游戏使用libGDX库。我用户overlap
方法检测两个矩形之间的碰撞检测。
...
if (r1.overlaps(r2)) collisionTest();
...
我想检测矩形(顶部,底部,左侧或右侧)的触摸面:
r1 overlap r2 on the left side
任何人都可以给我代码,但这需要快速的方法。
由于
答案 0 :(得分:8)
您可以使用intersectRectangles
类中提供的方法Intersector
来确定两个矩形是否重叠,如果是,则重叠的位置。您可以使用此信息来确定它们是否与左,右,顶部和/或底部重叠。
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