Javascript 球碰撞js公式
我知道矩形的碰撞是这样计算的:Javascript 球碰撞js公式,javascript,physics,Javascript,Physics,我知道矩形的碰撞是这样计算的: ((a.y + a.height) < (b.top)) || (a.y > (b.y + b.height)) || ((a.x + a.width) < b.x) || (a.x > (b.x + b.width)) 我想要这个公式来计算两个圆是否碰撞 谢谢计算它们之间的距离。然后,如果距离小于它们的半径之和,则它们发生碰撞。计算它们之间的距离。如果距离小于半径之和,那么它们会碰撞。谢谢,但这不应该在答案部分吗?好的,那么,完成了。
((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;
}