开发者

Box2D Rectangle Collision

开发者 https://www.devze.com 2023-01-23 09:06 出处:网络
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

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!


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);
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号