0
votes

I am using Box2D for physics for a game I am making, and I wanted to see if there was a way to use Box2D so that a could take a rectangle and see if it collide with another rectangle without it doing any actual physics. Example:

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

Thanks in advance!

1

1 Answers

1
votes

Assuming that rect{x1,y1,x2,y2}, and that x1<x2 and y1<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);
}