2010-11-02 167 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