xxxxxxxxxx
44
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
let movers = []
function setup() {
createCanvas(windowWidth, 300);
movers[0] = new Mover(200, 100, 5);
createP("Click mouse to apply wind force.");
// disable right click context menu with the for-cycle below
for (let element of document.getElementsByClassName("p5Canvas")) {
element.addEventListener("contextmenu", (e) => e.preventDefault());
}
}
function draw() {
background(255);
let gravity = createVector(0, 0.1);
for (let i=0; i < movers.length; i++) {
movers[i].applyForce(gravity);
if (movers[i].contactEdge()) {
let c = 0.1;
let friction = movers[i].velocity.copy();
friction.mult(-1);
friction.setMag(c);
movers[i].applyForce(friction);
}
if (mouseIsPressed) {
let wind = createVector(0.1, 0);
movers[i].applyForce(wind);
}
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}