setup()
setup() runs once when the sketch starts. It initializes the physics engine, creates static boundaries, and populates the world with objects. The noLoop() at the end keeps the screen frozen until the user interacts—a nice way to show instructions without animation running.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize Matter.js engine and world
engine = Engine.create();
world = engine.world;
world.gravity.scale = 0.001; // Reduced gravity for a more deliberate shatter effect
// Create the ground
ground = Bodies.rectangle(width / 2, height, width, 50, { isStatic: true });
Composite.add(world, ground);
// Create side walls
let wallThickness = 20;
walls.push(Bodies.rectangle(0, height / 2, wallThickness, height, { isStatic: true }));
walls.push(Bodies.rectangle(width, height / 2, wallThickness, height, { isStatic: true }));
Composite.add(world, walls);
// Add an initial pile of objects that can shatter
for (let i = 0; i < 50; i++) {
addShatterableBody(width / 2, height - 100 - i * 20); // Stack them up
}
// Draw initial instructions
textAlign(CENTER, CENTER);
textSize(32);
fill(255);
noStroke();
text('Click or drag your mouse to smash objects!', width / 2, height / 2);
// Stop the draw loop until the first mouse interaction
noLoop();
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
engine = Engine.create();
world = engine.world;
world.gravity.scale = 0.001;
Creates a Matter.js physics engine and world with very low gravity to keep blocks suspended longer
ground = Bodies.rectangle(width / 2, height, width, 50, { isStatic: true });
Composite.add(world, ground);
// Create side walls
let wallThickness = 20;
walls.push(Bodies.rectangle(0, height / 2, wallThickness, height, { isStatic: true }));
walls.push(Bodies.rectangle(width, height / 2, wallThickness, height, { isStatic: true }));
Composite.add(world, walls);
Creates immobile ground and wall bodies that blocks collide with but cannot move
for (let i = 0; i < 50; i++) {
addShatterableBody(width / 2, height - 100 - i * 20); // Stack them up
}
Creates 50 blocks stacked vertically, each 20 pixels higher than the last
createCanvas(windowWidth, windowHeight);- Creates a full-window canvas that responds to the browser's dimensions
engine = Engine.create();- Instantiates a new Matter.js physics engine—this object will calculate all physics updates
world = engine.world;- Retrieves the physics world from the engine—bodies added here will experience gravity and collisions
world.gravity.scale = 0.001;- Sets gravity to a very low value (normally 0.001, which is 1/1000th of default) so blocks fall slowly and deliberately
ground = Bodies.rectangle(width / 2, height, width, 50, { isStatic: true });- Creates a static (non-moving) rectangular body at the bottom of the canvas to act as the ground
Composite.add(world, ground);- Adds the ground body to the physics world so it participates in collisions
walls.push(Bodies.rectangle(0, height / 2, wallThickness, height, { isStatic: true }));- Creates a static wall on the left edge (x=0) and adds it to the walls array
for (let i = 0; i < 50; i++) {- Loops 50 times to create a stack of blocks
addShatterableBody(width / 2, height - 100 - i * 20);- Calls addShatterableBody() to create a random block; each iteration i stacks blocks 20 pixels higher (i * 20)
noLoop();- Stops the draw loop from running automatically—it will only run when the user clicks