xxxxxxxxxx
64
let grav = false;
let win = false;
let movers = [];
class Mover {
constructor() {
this.position = createVector(random(0,width),random(0,height));
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
this.mass = random(0,1);
}
applyForce(Force) {
this.acceleration = Force;
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
if (this.position.y >= height|| this.position.y < 0) {
this.position.y = height;
this.velocity.y = this.velocity.y * -0.9;
}
if (this.position.x >= width || this.position.x < 0) {
this.velocity.x = this.velocity.x * -0.9;
}
}
display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(this.position.x, this.position.y, 40, 40);
}
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 100; i++) {
movers.push(new Mover());
}
}
function draw() {
background(220);
let gravity = createVector(0, 1);
let wind = createVector(random(-2000, 2000),random(-2000, 2000));
let stepvar = random(1);
for (let i = 0; i < 100; i++) {
movers[i].applyForce(p5.Vector.mult(wind,movers[i].mass));
if (grav == true) {
movers[i].applyForce(gravity);
}
if (mouseIsPressed) {
grav = true;
} else {
grav = false;
// movers[i].velocity.y = 0;
// movers[i].acceleration.y = 0;
}
movers[i].update();
movers[i].display();
}
}