Box2D矩形碰撞

时间:2010-11-02 20:37:27

标签: c++ box2d collision

我正在为我正在制作的游戏使用Box2D物理,我想看看是否有办法使用Box2D这样一个矩形可以看到它是否与另一个矩形碰撞而没有它做任何实际的物理。例如:

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

提前致谢!

1 个答案:

答案 0 :(得分: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);
} 
相关问题