有没有更简单的方法来检测 HTML canvas 上的碰撞?

Is there an easier way to detect collision on an HTML canvas?

HTML canvas 上的图像和形状之间是否有更简单的二维碰撞检测方法?我厌倦了每次要检测碰撞时都输入以下 if 语句。

if (a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y) {
    // rectangle a and rectangle b are colliding
}

此外,该算法只能检测 2 个矩形之间的碰撞,不能检测 2 个圆或一个圆与一个矩形之间的碰撞。

据我所知,没有任何专门为 2d 碰撞构建的库,但如果你想查看一个很酷的 2d 物理库,请查看 matter.js,否则如果你不这样做的话想要使用整个库,请查看:https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection. If you are working on making a platformer, you should check out these platformer physics I made in javascript: https://platformerphysics.netlify.app.