【问题标题】:Box2D Rectangle CollisionBox2D 矩形碰撞
【发布时间】:2010-11-03 05:42:37
【问题描述】:

我正在为我正在制作的游戏使用 Box2D 进行物理处理,我想看看是否有一种方法可以使用 Box2D 以便 a 可以获取一个矩形并查看它是否与另一个矩形发生碰撞而不进行任何实际物理操作.示例:

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

提前致谢!

【问题讨论】:

    标签: c++ box2d collision


    【解决方案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);
    } 
    

    【讨论】:

    • 我希望你的意思不是旋转任意角度:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多