xxxxxxxxxx
76
const Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Bodies = Matter.Bodies,
Composite = Matter.Composite;
let engine;
let particles = [];
let balls = []
const props = {
friction: 0,
frictionAir: 0,
restitution: 1, frictionStatic: 0,
inertia: Infinity,
slop: 0.001,
};
function setup() {
createCanvas(400, 400);
engine = Engine.create();
engine.world.gravity.y = 0;
Matter.Resolver._restingThresh = 0.00001;
for (let i = 0; i < 80; i++) {
const particle = Bodies.circle(random(width), random(height), 3, props);
const vel = p5.Vector.random2D().setMag(3);
Matter.Body.setVelocity(particle, vel);
particles.push(particle);
}
const walls = [
Bodies.rectangle(width + 5, height / 2, 10, height, {
isStatic: true,
props,
}),
Bodies.rectangle(-5, height / 2, 10, height, { isStatic: true, props }),
Bodies.rectangle(width / 2, -5, width, 10, { isStatic: true, props }),
Bodies.rectangle(width / 2, height+5, width, 10, {
isStatic: true,
props,
}),
];
for (let i = 0; i < 10; i++) {
const ball = Bodies.circle(random(width), random(height), 15, props)
balls.push(ball)
}
Composite.add(engine.world, particles);
Composite.add(engine.world, walls);
Composite.add(engine.world, balls);
}
function draw() {
background(220, 150);
var bodies = Composite.allBodies(engine.world);
for (var i = 0; i < bodies.length; i += 1) {
var vertices = bodies[i].vertices;
beginShape();
for (const v of vertices) {
vertex(v.x, v.y);
}
endShape(CLOSE);
}
particles.forEach(p => Matter.Body.setSpeed(p, 3))
Engine.update(engine);
}