0
據我所知,矩形的碰撞是這樣計算的:球碰撞JS公式
((a.y + a.height) < (b.top)) ||
(a.y > (b.y + b.height)) ||
((a.x + a.width) < b.x) ||
(a.x > (b.x + b.width))
我想要的公式來計算,如果兩個圓碰撞。
謝謝
據我所知,矩形的碰撞是這樣計算的:球碰撞JS公式
((a.y + a.height) < (b.top)) ||
(a.y > (b.y + b.height)) ||
((a.x + a.width) < b.x) ||
(a.x > (b.x + b.width))
我想要的公式來計算,如果兩個圓碰撞。
謝謝
// calculates distance between two points
function distance (p0, p1) {
var dx = p1.x - p0.x,
dy = p1.y - p0.y;
return Math.sqrt(dx * dx + dy * dy);
}
// if the distance between the points is less then or equal to the sum of radii
// it returns true i.e collision else false
function circleCollision (c0, c1) {
return distance(c0, c1) <= c0.radius + c1.radius;
}
計算它們之間的距離。那麼如果距離小於它們的半徑之和,則它們相撞。
計算它們之間的距離。那麼如果距離小於它們的半徑之和,則它們相撞。 – xunatai
謝謝,但不應該在答案部分? – kcode
好吧,完成。 – xunatai