Workshop › Pinball › Step 11 of 42
When the ball is inside a wall, we push it back out along the normal: the direction from the closest point to
the ball's centre. Then update checks every wall each frame.
```js
if (d >= R || d === 0) return false
const nx = (ball.x - px) / d
const ny = (ball.y - py) / d
ball.x = px + nx * R
ball.y = py + ny * R
return true
ball.y += ball.vy
for (const w of WALLS) hitSegment(w[0], w[1], w[2], w[3], 0.5)
```(ball.x - px, ball.y - py) points from the wall to the ball; dividing by its length d makes it one pixel long.
That is the normal (nx, ny).px + nx * R is the point R pixels out from the wall along the normal: the ball now just touches.update calls hitSegment for every wall after moving; w[0] to w[3] are the wall's four numbers, and 0.5
is its springiness.hitSegment, write the four lines between if (d >= R ... and return true.update, under ball.y += ball.vy, write the for line.Skills: Collision detection
← Does the ball touch a wall? Bounce off the wall →