Workshop › Puzzle Bobble-style Bubble Shooter › Step 13 of 31
A shot stops as soon as it touches something: the ceiling, or a bubble in the grid, when the centres are less than 2R
apart. We use 2R - 4, a little forgiving, so a shot can slip through a gap that looks just wide enough.
```js
// Does a bubble at (x, y) touch the ceiling or a bubble in the grid?
function touches(x, y) {
if (y - R <= TOP) return true
for (let r = 0; r < ROWS; r++) {
for (let c = 0; c < cols(r); c++) {
if (grid[r][c] < 0) continue
const p = cellPos(r, c)
if (Math.hypot(p.x - x, p.y - y) < 2 * R - 4) return true
}
}
return false
}
```y - R; at the strip's bottom (TOP) it touches the ceiling.Math.hypot is the distance between the centres; closer than 2R - 4 counts as touching.Above function update() { write touches with its comment, and an empty line after it.
Skills: Collision detection
← Bounce off the walls Stick into the grid →