Workshop › Asteroids › Step 21 of 41
Two circles touch when the distance between their centers is less than the sum of their radii. Comparing squared distances skips the square root and gives the same answer.
```js
function hits(a, ar, b, br) {
const dx = a.x - b.x
const dy = a.y - b.y
return dx * dx + dy * dy < (ar + br) * (ar + br)
}
```a and b are anything with x and y; ar and br their radii.true or false, and that is what the function returns.Above the keydown listener write hits, followed by an empty line.
Skills: Collision detection
← Drifting rocks Breaking a rock →