0

Possible Duplicate:
Determine if two rectangles overlap each other?

Considering I have 2 squares for which I know the x and y positions and I also know the size, what would be the formula to use if I wanted to see if the objects collide with eachother.

if(   ((shapeA->getX() - shapeA->getSize()) > (player->getX() - player->getSize())
    && (shapeA->getX() + shapeA->getSize()) < (player->getX() + player->getSize()))
    && (shapeA->getY() - shapeA->getSize()  > player->getY() - player->getSize()
    && (shapeA->getY() + shapeA->getSize()) < (player->getY() + player->getSize()))
                )

This works, but it works strange (not all the time). I must be missing something

4

4 回答 4

4

检查一个矩形是否与另一个矩形相交或接触非常容易。看看下面的图片:

矩形交叉口

如您所见,如果 ([x,x+a] 和 [X,X+A]) 和 ([y,y+b] 和 [Y,Y+B]) 之间的交点都不是,则两个矩形相交t 为空。

struct Rectangle{
    bool intersects(const Rectangle&);

    unsigned int a; //!< width of the rectangle
    unsigned int b; //!< height of the rectangle
    unsigned int x; //!< x position
    unsigned int y; //!< y position
};

bool Rectangle::intersects(const Rectangle& oRectangle){        
    return  (x < oRectangle.x + oRectangle.a) && // [x,x+a], [X,X+A] intersection
            (oRectangle.x < x + a)            && // [x,x+a], [X,X+A] intersection
            (y < oRectangle.y + oRectangle.b) && // [y,y+b], [Y,Y+B] intersection
            (oRectangle.y < y + b);              // [y,y+b], [Y,Y+B] intersection
}

所以你的代码应该是

if(((shapeA->getX() + shapeA->getSize()) > (player->getX()) // x intersection
    && (shapeA->getX() < (player->getX() + player->getSize())) // x intersection
    && (shapeA->getY() < player->getY() + player->getSize()  // y intersection
    && (shapeA->getY() + shapeA->getSize()) > player->getY())  // y intersection
)
于 2012-05-31T10:20:26.067 回答
2

假设 getX/Y 给出正方形的左下角,

shapeMinX = shapeA; shapeMaxX = shapeB;
if (shapeA()->getX() > shapeB()->getX())
  swap (shapeMinX, shapeMaxX);
shapeMinY = shapeA; shapeMaxY = shapeB;
if (shapeA()->getY() > shapeB()->getY())
  swap (shapeMinY, shapeMaxY);

collision = (shapeMinX->getX()+shapeMinX->size() >= shapeMaxX()->getX) || (shapeMinX->getY()+shapeMinY->size() >= shapeMaxY()->getY);
于 2012-05-31T10:14:57.597 回答
2

你做错了测试,试试这个:

int left_bound_A=  shapeA->getX()-shapeA->getSize();
int right_bound_A= shapeA->getX()+shapeA->getSize();
int top_bound_A= shapeA->getY()-shapeA->getSize();
int bottom_bound_A= shapeA->getY()+shapeA->getSize();

int left_bound_B=  shapeB->getX()-shapeB->getSize();
int right_bound_B= shapeB->getX()+shapeB->getSize();
int top_bound_B= shapeB->getY()-shapeB->getSize();
int bottom_bound_B= shapeB->getY()+shapeB->getSize();

if( left_bound_A < right_bound_B &&
    right_bound_A > left_bound_B &&
    top_bound_A > bottom_bound_B &&
    bottom_bound_A < top_bound_B ) colide(shapeA,shapeB);

一般的方法是测试形状相交。如果您实现 Box 或 Rectangle 类,则代码简化为:

 Box colision= intersect( shapeA->getBoundBox(), shapeB->getBoundBox() );
 if( colision.have_positive_area() )
     colide(shapeA,shapeB,colision);
于 2012-05-31T10:29:08.480 回答
0

是的,检查两个矩形的方法很简单。正如一个建议,如果您想计算矩形列表中矩形之间所有可能的交集,请通过增加其边界的 x 来对它们进行预排序,然后利用这种关系开始比较。这个问题可能会有所帮助

交叉矩形的快速隐藏可能很有趣

于 2012-05-31T11:01:38.157 回答