Workshop › Pinball › Step 30 of 42
Try it: a ball resting on a flipper barely moves when the flipper flips, because the bounce only looks at the ball's
own speed. The fix is to bounce on the ball's speed relative to the surface. hitSegment gets an optional sixth
parameter, a function that tells how fast the surface moves at a point.
```js
// Push the ball out of a segment and bounce it. `surface` is how fast the segment itself moves at that point:
// the bounce works on the speed of the ball relative to the surface, which is how a flipper throws the ball.
function hitSegment(x1, y1, x2, y2, bounce, surface) {
const sx = surface ? surface(px, py).x : 0
const sy = surface ? surface(px, py).y : 0
const vn = (ball.vx - sx) * nx + (ball.vy - sy) * ny
```undefined, which counts as false in a condition. So surface ? ... : 0 means:
if there is a surface function, ask it for the speed at the closest point (px, py); otherwise the surface is still.vn now uses ball.v − surface v. A surface moving at 10 into a still ball gives vn = −10, so the ball leaves at
the surface's speed.hitSegment with the two new lines, and add , surface to its parameters.const vn = ... line in hitSegment with the three lines shown.Skills: Velocity and gravity, Functions