Libgdx - 从Rectangle.overlap(Rectangle)获取交叉矩形

Alg*_*man 1 java eclipse android libgdx

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

我需要获得两个矩形之间的交集矩形区域,但libgdx中的重叠方法只返回两个矩形是否相交的布尔值.我已经阅读过Intersector类,但它没有提供任何帮助.

nEx*_*are 8

实际上,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;
}
Run Code Online (Sandbox Code Playgroud)


Saw*_*wny 5

您可以使用Intersector类。

import com.badlogic.gdx.math.Intersector;

Intersector.intersectRectangles(rectangle1, rectangle2, intersection);
Run Code Online (Sandbox Code Playgroud)