xxxxxxxxxx
75
// Example based on https://www.youtube.com/watch?v=urR596FsU68
// 5.17: Introduction to Matter.js - The Nature of Code
// by @shiffman
// module aliases
Matter.Resolver._restingThresh = 0.001;
var Engine = Matter.Engine,
// Render = Matter.Render,
World = Matter.World,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Bodies = Matter.Bodies;
let engine;
let world;
let boxes = [];
let circles = [];
let grounds = [];
let mConstraint;
let canvas;
let sizes = [5, 10, 20, 30, 40];
function setup() {
canvas = createCanvas(windowWidth, windowHeight);
engine = Engine.create();
world = engine.world;
world.gravity.y = 0;
world.gravity.x = 0;
// Engine.run(engine);
grounds.push(new Boundary(0, height / 2, 10, height));
grounds.push(new Boundary(width, height / 2, 10, height));
grounds.push(new Boundary(width / 2, 0, width, 10));
grounds.push(new Boundary(width / 2, height, width, 10));
World.add(world, grounds);
let mouse = Mouse.create(canvas.elt);
mouse.pixelRatio = pixelDensity() // for retina displays etc
let options = {
mouse: mouse
}
mConstraint = MouseConstraint.create(engine, options);
World.add(world, mConstraint);
}
let count = 0;
function draw() {
background(51);
if (frameCount % 5 === 0 && count++ < 10) {
print(count);
let circle = new Circle(width / 2, 0, 20);
console.log(circle);
circle.body.friction = 0;
circle.body.frictionAir = 0;
circle.body.frictionStatic = 0;
Matter.Body.setInertia(circle.body, Infinity);
circle.body.restitution = 1;
circles.push(circle);
}
Engine.update(engine);
for (let box of boxes) {
box.show();
}
for (let circle of circles) {
circle.show();
}
for (let ground of grounds) {
ground.show();
}
}