xxxxxxxxxx
73
// before vectors
// no gravity, no wind!
// https://www.youtube.com/watch?v=UHx0EoveT90
let ballArray = [];
let grav, drag, wind;
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 50; ++i) {
ballArray.push(new Ball(random(width), -30));
}
grav = createVector(0, 0.2);
wind = createVector(0, 0);
drag = createVector(0.99, 0.99);
}
function draw() {
background(220);
for (let b of ballArray) {
b.show();
b.move();
b.bounce();
b.applyForce(grav);
b.applyForce(wind);
b.applyDrag(drag);
}
}
function mousePressed() {
for (let b in ballArray) {
b.loc = set(random(width), -30);
}
}
class Ball {
constructor(x, y) {
this.x = x;
this.y = y;
this.loc = createVector(x, y);
//this.xspeed = 0;
//this.yspeed = 1;
this.speed = createVector(0,random(0.66,1));
this.r = 15;
}
show() {
ellipse(this.loc.x, this.loc.y, this.r * 2);
}
move() {
//this.x += this.xspeed;
//this.y += this.yspeed;
this.loc.add(this.speed);
}
bounce() {
if (this.loc.y >= height - this.r) {
this.loc.y = height - this.r;
this.speed.y *= -1;
}
if (this.x >= width - this.r) {
this.loc.x = width - this.r;
this.speed.x *= -1;
}
}
applyForce(f) {
this.speed.add(f);
//grav.y += 0.00005;
}
applyDrag(d) {
this.speed.mult(d);
}
}