Matter.js physics blocks enable realistic physics simulation in b5, including gravity, collisions, and rigid body dynamics. These blocks are from the library source and require proper initialization.
Physics blocks are based on Matter.js, a 2D physics engine for the web. The b5 integration simplifies Matter.js into easy-to-use blocks.
Initializes the Matter.js physics engine. This block must be called ONCE in the Factory Variable section before using any other physics blocks.Filter:setup, unique - Can only be used in Variable section and only one instance allowed.Example Usage:From the Physics example:
Variable: engine Line 0: [matter_startEngine] // Initializes physics world // Enables gravity and collision detectionPlayground: // Now you can use physics blocks [matter_box] [matter_ball] [matter_ground]
Required Initialization: You MUST create a variable with matter_startEngine before using any physics blocks. Without it, physics blocks will not work.
Line 0: [mouseIsPressed] -> trigger [backgroundPicker: gray]Line 1: [mouse] -> x, y [random size] <- mouseIsPressed → w, h (10-40 range)Line 2: [strokePicker: white]Line 3: [fillPicker: dark gray]Line 4: [matter_box] <- mouseIsPressed, mouseX, mouseY, w, h // Creates boxes at mouse position when clicked // Boxes fall and collide with each other and ground
Line 0: [canvasSize] -> width, heightLine 1: [fractionSlider: 95, 0-100] <- height → 95% of height [true] -> use default thicknessLine 4: [matter_ground] <- groundY, true // Creates ground near bottom of canvas // Objects fall and land on it
Variable: engine [matter_startEngine]Playground: Line 0: [mouseIsPressed] -> spawn trigger [backgroundPicker: gray] Line 1: [mouse] -> spawn position [random size] <- trigger → random dimensions Line 2: [strokePicker: white] Line 3: [fillPicker: dark gray] Line 4: [matter_box] <- trigger, x, y, w, h [matter_ground] <- groundY [frameRateShow]
Click and drag to spawn boxes that fall and pile up.
Variable: engine [matter_startEngine]Playground: Line 0: [canvasSize] -> width [random] <- true, 0, width → random X [number: 0] -> top of canvas Line 1: [fillPicker: blue] Line 2: [matter_ball] <- true, randomX, 0, 10 // Creates ball at random X position at top // New ball every frame = rain effect Line 3: [matter_ground] <- height, 20 // Balls accumulate on ground
Performance: Creating physics objects every frame (trigger: true) can slow down your sketch. For continuous spawning, consider:
Variable: engine [matter_startEngine]Playground: Line 0: [canvasSize] -> height [fractionSlider: 95, 0-100] <- height → position Line 1: [matter_ground] <- 95% height, default thickness // Ground near bottom but not at edge // Leaves space for objects to rest on bottom