Reputation: 402
I understand that the collision of rectangles is calculated like this:
((a.y + a.height) < (b.top)) ||
(a.y > (b.y + b.height)) ||
((a.x + a.width) < b.x) ||
(a.x > (b.x + b.width))
I want the formula to calculate if two circles collide.
Thanks
Upvotes: 0
Views: 114
Reputation: 1322
// 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;
}
Upvotes: 0
Reputation: 181
Calculate the distance between them. Then if the distance is less than the sum of their radii, then they collide.
Upvotes: 1