Workshop › Pinball › Step 13 of 42
A fast ball can jump over a thin wall between two frames. So each frame is split into four small steps, and the
speed is capped at MAX_SPEED.
```js
const SUB = 4 // physics steps per frame
const MAX_SPEED = 18
function step() {
ball.vy += GRAVITY / SUB
ball.x += ball.vx / SUB
ball.y += ball.vy / SUB
for (const w of WALLS) hitSegment(w[0], w[1], w[2], w[3], 0.5)
}
function update() {
for (let i = 0; i < SUB; i++) step()
const speed = Math.hypot(ball.vx, ball.vy)
if (speed > MAX_SPEED) {
ball.vx *= MAX_SPEED / speed
ball.vy *= MAX_SPEED / speed
}
}
```step() is the old update with everything divided by SUB: a quarter of the gravity and a quarter of the move.for (let i = 0; i < SUB; i++) step() runs it four times per frame.Math.hypot(vx, vy) is the ball's speed; if it is above the limit, both parts are scaled down by the same factor,
so the direction stays the same.GRAVITY write SUB and MAX_SPEED.update function with step and the new update.Skills: Velocity and gravity, Loops
← Bounce off the wall Ready or playing →