Workshop › Pinball › Step 12 of 42
Pushing the ball out is not enough: its speed still points into the wall. We reflect the part of the velocity that
goes into the wall, keeping half of it (bounce = 0.5).
```js
const vn = ball.vx * nx + ball.vy * ny
if (vn < 0) {
ball.vx -= (1 + bounce) * vn * nx
ball.vy -= (1 + bounce) * vn * ny
}
```vn is the dot product of the velocity and the normal: how fast the ball moves out of the wall. Negative means into it.vn along the normal would stop it; taking away (1 + bounce) × vn
sends it back out at bounce times the speed.In hitSegment, write the five lines under ball.y = py + ny * R, above return true.
Skills: Velocity and gravity, Collision detection
← Push the ball out Small steps and a speed limit →