xxxxxxxxxx
53
let movers = [];
let center;
function setup() {
createCanvas(400, 400);
center = createVector(width / 2, height / 2);
randomizeMovers();
}
function randomizeMovers() {
for (let i = 0; i < 10; i++) {
let randomPosition = createVector(random(width), random(height));
let randomMass = random(2, 8);
movers[i] = new Mover(randomPosition, randomMass);
}
}
function drawBackground(groundHeight) {
background("SkyBlue");
noStroke();
fill("green");
rect(0, height - groundHeight, width, height - groundHeight);
}
function draw() {
drawBackground(1);
let wind = createVector(0, 0);
if (mouseIsPressed) {
let mouse = createVector(mouseX, mouseY);
wind = p5.Vector.sub(center, mouse).setMag(0.5);
drawArrow(center, p5.Vector.mult(wind, 250), "DeepSkyBlue", 20);
}
for (let mover of movers) {
let gravity = createVector(0, 0.2);
let weight = p5.Vector.mult(gravity, mover.mass);
mover.applyForce(weight);
mover.applyForce(wind);
mover.friction();
mover.update();
mover.edges();
mover.display();
}
}
function keyPressed() {
randomizeMovers();
}