This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
Angry-Birds-2 puts Red in a slingshot. Drag him back and let go, and he’s flung the other way, faster the further you pulled. The click from the last lesson is gone.
Constraints
The sling is a Matter constraint, a link that keeps two points a set distance apart. Each end is either a point on a body or a fixed spot in the world.
A constraint has two ends, A and B. bodyA and bodyB are what each end is attached to. These aren’t the collision pair’s bodyA and bodyB from the last lesson. pointA and pointB are offsets from those bodies’ centres, or world positions for an end with no body.
Drag the crate below. Its constraint ties pointA, a fixed spot, to pointB on bodyB, the crate. It pulls when it’s longer than its length and pushes when it’s shorter, and stiffness says how hard. ↺ the sling’s settings sets it up the way the slingshot’s is:
The sling ties a fixed point, SLINGSHOT_LOCATION, to the bird:
67this.sling = Constraint.create({68 pointA: SLINGSHOT_LOCATION,69 // The bird is attached later, and Matter can't work these out without one.70 pointB: { x: 0, y: 0 },71 angleB: 0,72 stiffness: SLINGSHOT_STIFFNESS,73 length: 0,74});With a length of 0 the constraint pulls the bird toward the sling’s spot, and with a low stiffness it does so like a spring. load() attaches a bird by setting this.sling.bodyB, and setting it to null detaches it again.
Dragging
SlingMouse in src/objects/SlingMouse.js is given. It wraps Matter’s MouseConstraint, a second constraint between the mouse and whatever body it grabs. Collision categories, labels in each body’s collisionFilter that say which bodies may touch, keep it off everything except the loaded bird, it lets a click just next to the bird still grab it, and it stops a pull at MAX_PULL. When the player grabs the bird, the sling detaches it, so the bird follows the mouse instead of meeting it halfway.
The elastic bands are drawn by SlingBands in src/objects/SlingBands.js, also given. They’re only for show, and the constraint does the work.
Letting Go
When the player lets go, two things happen a moment apart, both in src/objects/Slingshot.js.
release() sets the bird’s speed. The further back it was pulled, the faster it goes, up to LAUNCH_SPEED at a full pull, and it heads the opposite way to the pull. A tiny pull is a click, not a shot, so the bird goes back in the sling. You don’t need to follow the maths in release(), only what it does.
launch() takes the bird out of the sling. The bird doesn’t leave the sling the moment the player lets go. It shoots back through the sling’s spot first, like a real one, and leaves once it’s passed it. isReadyToLaunch() checks for that after every physics step, through Matter’s afterUpdate event, and calls launch() when it’s time. Checking once per frame instead could let the bird fly well past the sling first, on a slow frame that runs several steps.
Here’s a release, slowed right down, with a dot for each physics step:
launch() is what turns a bird in the sling into a bird in flight:
loadedBirdis the bird in the sling, andflyingBirdis the one in the air. The bird moves from one to the other.slingMouse.free(bird)lets the bird collide with everything again. While it was in the sling it only collided with the mouse, so pulling it low couldn’t bump it on the ground.- The bird changes from the new Aiming state, described below, to Flying, so it starts watching for hits and landing.
A bird in the sling is in that new Aiming state, src/states/bird/BirdAimingState.js, which does nothing but throw away early presses of Space. When the flying bird is finished, Level loads a fresh Red, as the queue doesn’t exist yet.
Level.destroy() now calls slingshot.destroy() too. Matter’s mouse adds listeners to the page, and each level would otherwise leave its own behind.
Let go
Drag Red back and let go. He flies, but the slingshot never hands him over. He still counts as loaded, so he never looks hurt, never bursts into smoke, and no new Red arrives.
launch() in src/objects/Slingshot.js has two TODOs.
- Replace the first with the move from the sling to the air. Set
this.flyingBirdto the bird, and emptythis.loadedBirdwithnull. - Replace the second with a change to the Flying state, through the bird’s
stateMachineandBirdStateName.Flying, so the bird starts watching for hits and landing.
Then delete the // biome-ignore comment above the @src/enums.js import at the top of the file. It only kept Biome quiet about the unused BirdStateName until you used it.
The Challenge tab gets a ✓ once you’ve launched a bird.
The two TODOs go in different places. The first replaces the comment above this.wasReleased = false;, and the second the comment above the sounds.
If the bird flies but no new Red arrives, the change to Flying is missing, so the bird never enters Flying and never lands.
Replace the first TODO with:
this.flyingBird = bird;this.loadedBird = null;Replace the second with:
bird.stateMachine.change(BirdStateName.Flying);