class Rectangle{
public:
float x, y, width, height;
// (x,y) is the lower left corner of the rectangle
};这个算法正确吗?
bool Rectangle::colidesWith(Rectangle other) {
if (x+width < other.x) return false; // "other" is on the far right
if (other.x+other.width < x) return false; //"other" is on the far left
if (y+height < other.y) return false // "other" is up
if (other.y+other.height < y) return false // "other" is down
return true;
}发布于 2012-12-18 01:50:48
这是如果矩形是填充的(即,您将其中一个在另一个内部的情况计为碰撞)。
发布于 2012-12-18 01:57:42
是啊。你可以把它看作超平面分离定理的特例,超平面分离定理是这个问题的一般版本。您正在将这些矩形投影到X和Y轴上,然后检查生成的线段之间是否有一些分隔。
发布于 2012-12-18 15:36:50
对我来说,写这个条件的更直观的方式是:
( max(r1.x, r2.x) < min(r1.x+r1.w, r2.x+r2.w) ) &&
( max(r1.y, r2.y) < min(r1.y+r1.h, r2.y+r2.h) )实际上,这可以推广到任何维度。
https://stackoverflow.com/questions/13919303
复制相似问题