2010-11-02 193 views
0

我正在使用Box2D作为我正在制作的游戏的物理特性,并且我想查看是否有方法使用Box2D,以便a可以取一个矩形并查看它是否与另一个矩形相冲突任何实际的物理。例如:Box2D矩形碰撞

bool RectInRect(rect p1, rect p2) 
{ 
    bool result = Box2D_do_rect_stuff(); 
    return result; 
} 

在此先感谢!

回答

1

假设rect{x1,y1,x2,y2},那x1<x2y1<y2

bool RectInRect(rect p1, rect p2) 
{ 
    pair<const int&, const int&> p1x = minmax(p1.x1, p1.x2); 
    pair<const int&, const int&> p1y = minmax(p1.y1, p1.y2); 
    pair<const int&, const int&> p2x = minmax(p2.x1, p2.x2); 
    pair<const int&, const int&> p2y = minmax(p2.y1, p2.y2); 

return max(p1x.first, p2x.first) <= min(p1x.second, p2x.second) && 
    max(p1y.first, p2y.first) <= min(p1y.second, p2y.second); 
} 
+0

这不会与旋转矩形工作,不是吗? – Matt 2010-11-02 21:21:04

+0

已编辑。现在它应该工作。 – Dialecticus 2010-11-02 21:43:12

+0

我希望你不要以任意角度旋转:-) – Dialecticus 2010-11-02 21:44:23