2017-06-15 125 views
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)) 

我想要的公式来计算,如果两个圆碰撞。

谢谢

+2

计算它们之间的距离。那么如果距离小于它们的半径之和,则它们相撞。 – xunatai

+0

谢谢,但不应该在答案部分? – kcode

+0

好吧,完成。 – xunatai

回答

0
// 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; 
      } 
1

计算它们之间的距离。那么如果距离小于它们的半径之和,则它们相撞。