JS 如何判断两个 2d 框是否使用它们的顶点坐标接触?

JS How to tell if two 2d boxes are touching using their vertex coordinates?

我有一个 2d 游戏,我需要在其中正确检查碰撞。 例如,当我有两个盒子的左上角坐标及其边长时,我需要检查一个盒子是否与另一个盒子接触。

// where side is side length
function touches(x1, y1, size1, x2, y2, size2){
     ...
}

我在这里找不到任何类似的问题,有没有办法创建这样的函数?

要检查两个正方形是否接触或重叠,您可以检查 x 和 y 坐标是否在同一范围内。

function touches(x1, y1, size1, x2, y2, size2){
    return (x1 >= x2 && x1 <= x2 + size2 || x2 >= x1 && x2 <= x1 + size1)
        && (y1 >= y2 && y1 <= y2 + size2 || y2 >= y1 && y2 <= y1 + size1);
}