2017-06-15 129 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

計算它們之間的距離。那麼如果距離小於它們的半徑之和,則它們相撞。