xxxxxxxxxx
42
//reference to box2d world
let world;
//list of particles
let particles = [];
//an object to store information about the uneven surface
let surface;
function setup() {
createCanvas(560, 390);
//initialize box2d physics and create the world
world = createWorld();
//create the surface
surface = new Surface();
}
function draw() {
background(220);
//we must always step through time
let timeStep = 1.0 / 30;
//second and third arguments are velocity and position iterations
world.Step(timeStep, 10, 10);
//particles fall from the top every so often
if (random(1) < 0.5) {
let sz = random(4, 8);
particles.push(new Particle(width / 2, 10, sz));
}
//draw the surface
surface.display();
//display all the particles
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].display();
if (particles[i].done()) {
particles.splice(i, 1);
}
}
}