有没有办法知道libgdx中两个矩形之间的相交矩形区域,如c#http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersect.aspx中的矩形?

我需要获取两个矩形之间的交集矩形区域,但无论两个矩形是否相交,libgdx中的overlay方法都只返回布尔值。
我读过Intersector类,但是它没有提供任何帮助。

最佳答案

确实,LibGDX没有内置此功能,因此我将执行以下操作:

/** Determines whether the supplied rectangles intersect and, if they do,
 *  sets the supplied {@code intersection} rectangle to the area of overlap.
 *
 * @return whether the rectangles intersect
 */
static public boolean intersect(Rectangle rectangle1, Rectangle rectangle2, Rectangle intersection) {
    if (rectangle1.overlaps(rectangle2)) {
        intersection.x = Math.max(rectangle1.x, rectangle2.x);
        intersection.width = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width) - intersection.x;
        intersection.y = Math.max(rectangle1.y, rectangle2.y);
        intersection.height = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height) - intersection.y;
        return true;
    }
    return false;
}

07-26 04:35