xxxxxxxxxx
76
let m;
let windPerlin= 100;
const SIZE = 8;
function setup() {
createCanvas(400, 360);
m = []
for(let i =0; i < 10; i++){
m.push(new Mover());
}
}
function draw() {
background(0);
const gravity = createVector(0, 0.1);
const windForce = createVector(0.01, 0);
for(let i=0;i<m.length; i++){
m[i].applyForce(gravity);
m[i].applyForce(windForce);
m[i].update();
m[i].checkEdges();
m[i].draw();
windPerlin++;
}
}
class Mover {
constructor(){
this.location = createVector(random(0, width), random(0, height));
this.velocity = createVector(0,0);
this.acceleration = createVector(0, 0);
this.mass = random(1, 5);
}
update() {
this.velocity.add(this.acceleration);
this.location.add(this.velocity);
this.acceleration.mult(0);
}
applyForce(force){
let f = force.copy()
f.div(this.mass);
this.acceleration.add(f);
}
checkEdges(){
const offset = (this.mass*SIZE)/2;
if (this.location.x > width-offset) {
this.location.x = width-offset;
this.velocity.x *= -1;
} else if (this.location.x < offset) {
this.velocity.x *= -1;
this.location.x = offset;
}
if (this.location.y > height-offset) {
this.velocity.y *= -1;
this.location.y = height-offset;
} else if (this.location.y < offset) {
this.velocity.y *= -1;
this.location.y = offset;
}
}
draw() {
stroke("white");
fill("black");
ellipse(this.location.x, this.location.y, SIZE*this.mass, SIZE*this.mass);
}
}