Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Angry-Birds-1 brings in Red, the first bird. He waits by the slingshot’s spot and hops now and then. Click anywhere in the game, and he’s thrown up and to the right at a fixed speed. When he hits something he looks hurt and loses feathers, and once he’s stopped he bursts into smoke and a fresh Red drops in.

There’s no slingshot yet, so the click is a stand-in that the next lesson replaces.

The Bird

Bird is a GameEntity around a circle body. Its definition in src/config/entities.js holds the numbers that differ from bird to bird, like its radius and how bouncy it is:

src/entities/Bird.js
56
Bodies.circle(x, y, definition.radius, {
57
label: type,
58
density: 0.02,
59
// No air drag, so a shot holds its speed along an even arc.
60
frictionAir: 0,
61
restitution: definition.restitution,
62
collisionFilter: { group: -1 },
63
}),
  • Density sets the mass from the size. A bird is far denser than any block, so it can knock towers over.
  • Restitution is how much speed a body keeps when it bounces, from 0 to 1.
  • collisionFilter.group keeps two bodies with the same negative group from ever colliding. Birds waiting in line would otherwise knock each other about.

Bird States

A bird is always doing one thing, so it has a state machine like Link’s in Zelda. Each state is a BirdState, which owns an animation and a Timer and cancels the timer when it exits. They’re in src/states/bird/, one file each:

  • Waiting hops every few seconds. It only hops if the bird is on the ground.
  • Flying marks the bird as in flight. Once it’s hit something and stopped, or after three seconds of rolling, it calls land(), which ends the flight. A bird that leaves the world dies straight away.
  • Dead calls die(), which takes the body out of the world and starts an Explosion, and sheds a burst of feathers.

BirdStateName.Ability, canUseAbility, and useAbility() are for the birds with abilities in the assignment, so Red never uses them.

Bird.isFinished() is true once the explosion has played out. That’s when Level drops in the next Red. Here he goes through all three:

Effects

Feathers, smoke, and the shards of broken blocks are only for show. Effects in src/objects/Effects.js moves them with a little gravity of its own, outside Matter. They can’t knock anything over, and hundreds of them cost almost nothing. Effects is given, so you only need to know how to call it. burst() throws pieces from a point.

Sounds

playSound() in src/services/sound.js plays one random file from a sound’s group of files, so repeated hits don’t all sound the same:

src/services/sound.js
12
const index = getRandomPositiveInteger(0, soundGroups[group] - 1);
13
14
game.sounds.play(getSoundName(group, index));

A body tumbling across the ground starts several collisions in a row. playHitSound() in src/entities/GameEntity.js keeps a short cooldown, so those all make one sound instead of a rattle.

Collision Events

Matter works out every collision, but it doesn’t know what a bird is. It reports collisions through events. The level subscribes once, and Matter calls back after every step that started any new collisions.

src/objects/Level.js
36
Events.on(
37
engine,
38
EventName.CollisionStart,
39
(/** @type {MatterCollisionEvent} */ event) => {
40
this.onCollisionStart(event);
41
},
42
);

This is the Observer pattern from Zelda. Matter is the subject, the side that sends notifications, and the level is an observer. The event holds a list of pairs. Each pair has bodyA and bodyB, the two bodies that just touched.

Here are the first pairs from a throw. Each line is one pair, with the label each body was made with, their speeds, and the faster of the two. That’s the hit speed, and both entities are told it.

What a Body Knows

A Matter body is a plain object full of properties that Matter keeps up to date every step. Matter has dozens, and MatterBody in src/globals.d.ts lists the ones this game reads:

src/globals.d.ts
24
type MatterBody = {
25
position: Point;
26
velocity: Point;
27
angle: number;
28
speed: number;
29
angularSpeed: number;
30
mass: number;
31
bounds: { min: Point; max: Point };
32
entity: GameEntity;
33
};
  • speed is how fast the body is moving, in pixels per step, whichever way it’s going.
  • bounds is the smallest box around the body, which isOnGround() uses to find its bottom edge.
  • entity isn’t Matter’s. GameEntity adds it, so a body from a collision leads back to the bird or block it belongs to.

onCollide(speed, other) in GameEntity.js does nothing, and entities that care override it. Bird in src/entities/Bird.js overrides it to show its hurt frame on the first hit, and to shed feathers and play a sound on hard ones. A bird that isn’t flying, like Red while he waits, ignores its collisions.

Pass on the collisions

Click anywhere in the game to throw Red. He flies, lands, and rolls to a stop, but he never looks hurt, and he never bursts into smoke. That’s because onCollisionStart() in src/objects/Level.js is empty, so Bird.onCollide() is never called. Red never knows he’s hit anything, and his Flying state waits for a hit before it ends his flight.

Loop over pairs with forEach(), and for each pair:

  1. Take bodyA and bodyB from the pair.
  2. Work out the hit speed. It’s the faster of the two bodies’ speeds, which Math.max() gives you. A bird hitting a still block counts as hard as a block falling on a still bird.
  3. Tell both entities. Call bodyA.entity.onCollide() with that speed and bodyB.entity, then call bodyB.entity.onCollide() with that speed and bodyA.entity.

Then delete the // biome-ignore comment above onCollisionStart(). It only kept Biome quiet about the unused pairs until you used it.

The Challenge tab gets a ✓ once you’ve seen Red hit something.

pairs.forEach((pair) => { ... }) runs the code inside once for each pair. const { bodyA, bodyB } = pair; takes both bodies out of it.

Each entity hears about the other one, with the same speed. bodyA.entity.onCollide(speed, bodyB.entity); tells A that it hit B, and the second call swaps them.

Replace the TODO with:

pairs.forEach((pair) => {
const { bodyA, bodyB } = pair;
const speed = Math.max(bodyA.speed, bodyB.speed);
bodyA.entity.onCollide(speed, bodyB.entity);
bodyB.entity.onCollide(speed, bodyA.entity);
});

📚 References

Powered by WebContainers
Files
Preparing Environment