Slide 1

Slide 1 text

EXPLORING 2D PHYSICS IN JAVASCRIPT WILL BOYD @lonekorean

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

BODIES WORLD ENGINE RUNNER RENDER △

Slide 4

Slide 4 text

BODIES WORLD ENGINE RUNNER RENDER △ The physical objects that are being simulated

Slide 5

Slide 5 text

The collection of bodies in a shared space BODIES WORLD ENGINE RUNNER RENDER △

Slide 6

Slide 6 text

BODIES WORLD ENGINE RUNNER RENDER △ Updates the simulation of the world

Slide 7

Slide 7 text

BODIES WORLD ENGINE RUNNER RENDER △ Continuously updates the engine

Slide 8

Slide 8 text

BODIES WORLD ENGINE RUNNER RENDER △ Visualizes the output of the engine as a element

Slide 9

Slide 9 text

Minimal Setup Minimal HTML

Slide 10

Slide 10 text

// engine let engine = Matter.Engine.create(); // render let render = Matter.Render.create({ element: document.body, engine: engine }); Matter.Render.run(render); // runner let runner = Matter.Runner.create(); Matter.Runner.run(runner, engine); Basic Setup

Slide 11

Slide 11 text

// params: x, y, radius let ball = Matter.Bodies.circle(400, 100, 50); Matter.World.add(engine.world, ball); Simple Body

Slide 12

Slide 12 text

y x (0, 0)

Slide 13

Slide 13 text

( , ) y x radius Matter.Bodies.circle(x, y, radius);

Slide 14

Slide 14 text

Matter.Bodies.rectangle(x, y, width, height); ( , ) y x width height

Slide 15

Slide 15 text

DEMO TIME! MINIMAL SETUP

Slide 16

Slide 16 text

Matemateca (IME/USP)/Rodrigo Tetsuo Argenton CC-BY-SA 4.0 LET’S MAKE A GALTON BOARD!

Slide 17

Slide 17 text

BOUNDARY WALLS

Slide 18

Slide 18 text

BOUNDARY WALLS DIVIDER WALLS

Slide 19

Slide 19 text

BOUNDARY WALLS DIVIDER WALLS PEGS

Slide 20

Slide 20 text

BOUNDARY WALLS DIVIDER WALLS PEGS BEADS

Slide 21

Slide 21 text

let render = Matter.Render.create({ element: document.body, engine: engine, options: { width: 560, height: 800, background: ‘#f8f9fa', wireframes: false } }); Setting Render Options

Slide 22

Slide 22 text

// params: x, y, width, height, options let bottom = Matter.Bodies.rectangle(280, 800, 560, 20, { isStatic: true, render: { fillStyle: '#868e96' } }); Matter.World.add(engine.world, bottom); Adding Bottom Wall

Slide 23

Slide 23 text

function wall(x, y, width, height) { return Matter.Bodies.rectangle(x, y, width, height, { isStatic: true, render: { fillStyle: '#868e96' } }); } Abstracting Wall Creation

Slide 24

Slide 24 text

Matter.World.add(engine.world, [ wall(280, 0, 560, 20), // top wall(280, 800, 560, 20), // bottom wall(0, 400, 20, 800), // left wall(560, 400, 20, 800), // right ]); Adding Boundary Walls

Slide 25

Slide 25 text

for (let x = 0; x <= 560; x += 80) { let divider = wall(x, 610, 20, 360); Matter.World.add(engine.world, divider); } Adding Divider Walls

Slide 26

Slide 26 text

function peg(x, y) { return Matter.Bodies.circle(x, y, 14, { isStatic: true, render: { fillStyle: '#82c91e' } }); } Peg Creation

Slide 27

Slide 27 text

let isStaggerRow = false; for (let y = 200; y <= 400; y += 40) { let startX = isStaggerRow ? 80 : 40; for (let x = startX; x <= 520; x+= 80) { Matter.World.add(engine.world, peg(x, y)); } isStaggerRow = !isStaggerRow; } Field Of Pegs

Slide 28

Slide 28 text

function bead() { return Matter.Bodies.circle(280, 40, 11, { render: { fillStyle: '#e64980' } }); } Bead Creation

Slide 29

Slide 29 text

function dropBead() { Matter.World.add(engine.world, bead()); } let dropBeadInterval = setInterval(dropBead, 2000); Dropping Beads

Slide 30

Slide 30 text

DEMO TIME! GALTON BOARD (ROUND 1)

Slide 31

Slide 31 text

Matter.Common.random(min, max); function rand(min, max) { return Math.random() * (max - min) + min; } Randomness - vs -

Slide 32

Slide 32 text

function dropBead() { let droppedBead = bead(); Matter.Body.setVelocity(droppedBead, { x: rand(-0.05, 0.05), y: 0 }); Matter.Body.setAngularVelocity(droppedBead, rand(-0.05, 0.05)); Matter.World.add(engine.world, droppedBead); } Making Things Less Perfect

Slide 33

Slide 33 text

function dropBead() { let droppedBead = bead(); Matter.Body.setVelocity(droppedBead, { x: rand(-0.05, 0.05), y: 0 }); Matter.Body.setAngularVelocity(droppedBead, rand(-0.05, 0.05)); Matter.World.add(engine.world, droppedBead); } Making Things Less Perfect

Slide 34

Slide 34 text

function peg(x, y) { return Matter.Bodies.circle(x, y, 14, { restitution: 0.5, // other properties here... }); } function bead() { return Matter.Bodies.circle(280, 40, 11, { restitution: 0.5, // other properties here... }); } Adding Some Bounce

Slide 35

Slide 35 text

function peg(x, y) { return Matter.Bodies.circle(x, y, 14, { label: 'peg', // other properties here... }); } Peg Labels

Slide 36

Slide 36 text

Matter.Events.on(engine, ‘collisionStart', lightPeg); function lightPeg(event) { event.pairs .filter((pair) => pair.bodyA.label === 'peg') .forEach((pair) => { pair.bodyA.render.fillStyle = '#4c6ef5'; }); } Lighting Up Pegs

Slide 37

Slide 37 text

Matter.Events.on(engine, ‘collisionStart', lightPeg); function lightPeg(event) { event.pairs .filter((pair) => pair.bodyA.label === 'peg') .forEach((pair) => { pair.bodyA.render.fillStyle = '#4c6ef5'; }); } Lighting Up Pegs

Slide 38

Slide 38 text

Matter.Events.on(engine, ‘collisionStart', lightPeg); function lightPeg(event) { event.pairs .filter((pair) => pair.bodyA.label === 'peg') .forEach((pair) => { pair.bodyA.render.fillStyle = '#4c6ef5'; }); } Lighting Up Pegs

Slide 39

Slide 39 text

DEMO TIME! GALTON BOARD (ROUND 2)

Slide 40

Slide 40 text

LET’S TALK ABOUT DEBUGGING WITH MATTER TOOLS!

Slide 41

Slide 41 text

DEMO TIME! MATTER TOOLS

Slide 42

Slide 42 text

Matter Tools Matter Tools HTML

Slide 43

Slide 43 text

function initGaltonBoard() { // existing code goes here... return { engine: engine, render: render, canvas: render.canvas, runner: runner, stop: function() { Matter.Render.stop(render); Matter.Runner.stop(runner); clearInterval(dropBeadInterval); } }; } Repackage Existing Code

Slide 44

Slide 44 text

MatterTools.Demo.create({ appendTo: document.body, tools: { gui: true }, examples: [ { id: 'galton-board', init: initGaltonBoard, } ] }); Matter Tools Setup

Slide 45

Slide 45 text

MatterTools.Demo.create({ appendTo: document.body, tools: { gui: true }, examples: [ { id: 'galton-board', init: initGaltonBoard, } ] }); Matter Tools Setup

Slide 46

Slide 46 text

let mouseConstraint = Matter.MouseConstraint.create(engine, { mouse: Matter.Mouse.create(render.canvas) }); Matter.World.add(engine.world, mouseConstraint); Mouse Control

Slide 47

Slide 47 text

DEMO TIME! LIGHTNING ROUND

Slide 48

Slide 48 text

DEMO TIME! PINBALL

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

ATTRACTORS + COLLISION FILTERS

Slide 53

Slide 53 text

ATTRACTORS + COLLISION FILTERS CONSTRAINTS

Slide 54

Slide 54 text

ATTRACTORS + COLLISION FILTERS CONSTRAINTS COMPOSITES

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

DEMO TIME! PAGE INTERACTIONS

Slide 60

Slide 60 text

THANKS! WILL BOYD @lonekorean ★ brm.io/matter-js/ ★ lonekorean.github.io/javascript-physics/