xxxxxxxxxx
56
//reference to box2d world
let world;
//list for particles
let particles = [];
//an object to describe a Windmill (two bodies and one joint)
let windmill;
function setup() {
createCanvas(560, 390);
let text = createP("Click mouse to toggle motor");
text.position(10, 5);
//initialize box2d physics and create the world
world = createWorld();
//make the windmill at an x,y position
windmill = new Windmill(width / 2, 175);
}
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);
if (random(1) < 0.1) {
let sz = random(4, 8);
particles.push(new Particle(random(width / 2 - 100, width / 2 + 100), -20, sz));
}
//display all the pairs
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].display();
if (particles[i].done()) {
particles.splice(i, 1);
}
}
//draw the windmill
windmill.display();
let status = "OFF";
if (windmill.motorOn()) status = "ON";
}
function mousePressed() {
windmill.toggleMotor();
// let status = "OFF";
// if (windmill.motorOn()) status = "ON";
// txt.html("Click mouse to toggle motor.\nMotor: " + status);
}