Workshop › Angry Birds-style Slingshot › Step 10 of 47
Space launches the bird: a body of 20 by 20 on the sling, with a velocity along the aim that grows with the pull.
```js
const LAUNCH = 0.2 // speed per pixel of pull
// The launch velocity: pulled back by `pull` pixels, the bird flies the opposite way.
const launchVelocity = () => ({ vx: Math.cos(aim.angle) * aim.pull * LAUNCH, vy: Math.sin(aim.angle) * aim.pull * LAUNCH })
function launch() {
const { vx, vy } = launchVelocity()
bird = body('bird', SLING.x - BIRD, SLING.y - BIRD, BIRD * 2, BIRD * 2)
bird.vx = vx
bird.vy = vy
bodies.push(bird)
}
else if (event.key === ' ') launch()
```launchVelocity() returns { vx, vy }: the aim's direction (cos, sin) times the pull times LAUNCH.const { vx, vy } = ... takes the two fields out into two names.SLING.x - BIRD is its left edge), gets the velocity and joins
bodies, so the loop in draw paints it.' ' is the name of the Space key.MAX_PULL write the LAUNCH line.keydown listener write launchVelocity and launch.else return, add the Space line.Skills: Velocity and gravity, Keyboard and mouse input
← Bodies Move by the velocity →