Workshop › Asteroids › Step 16 of 41
An asteroid has a position, a velocity and a size: 3 big, 2 medium, 1 small. Its radius comes from the table
SIZES. It flies in a random direction, smaller ones faster. shape holds how far each of its 10 corners is from the
center; all 1 for now.
```js
const SIZES = [0, 15, 28, 45] // asteroid radius for size 1, 2 and 3
function makeAsteroid(x, y, size) {
const angle = Math.random() * Math.PI * 2
const speed = 0.6 + Math.random() * (4 - size) * 0.5 // smaller asteroids are faster
const shape = Array.from({ length: 10 }, () => 1)
return { x, y, vx: Math.cos(angle) * speed, vy: Math.sin(angle) * speed, size, r: SIZES[size], shape }
}
```SIZES[size] looks up the radius; index 0 is a placeholder so size 3 is simply SIZES[3].Math.random() is a random number from 0 up to 1: times 2π it is any angle. The speed is at least 0.6, and
(4 - size) is bigger for small rocks.cos and sin, like thrust.Array.from({ length: 10 }, () => 1) makes a list of ten 1s.{ x, y, ... size } is short for { x: x, y: y, ... size: size }.BULLET_LIFE write SIZES.function shoot() { write makeAsteroid, followed by an empty line.Skills: Arrays and objects, Velocity and gravity
← A lifetime for bullets Four big rocks →