Workshop › Asteroids › Step 10 of 41
The speed is the length of the velocity vector, Math.hypot(vx, vy). Over the limit, both parts are scaled down by the
same factor: the direction stays and only the speed shrinks.
```js
const MAX_SPEED = 6
const speed = Math.hypot(ship.vx, ship.vy)
if (speed > MAX_SPEED) {
ship.vx *= MAX_SPEED / speed
ship.vy *= MAX_SPEED / speed
}
```Math.hypot(vx, vy) is the length of the velocity, Pythagoras.MAX_SPEED / speed is below 1 when too fast; multiplying both parts by it makes the length exactly MAX_SPEED.FRICTION write MAX_SPEED.update, above ship.x += ship.vx, write the speed limit.Skills: Velocity and gravity
← A little friction Around the screen →