xxxxxxxxxx
53
let balls = [];
let g = 9.8;
function setup() {
createCanvas(400, 400, WEBGL);
for (let i = 0; i < 10; i++) {
balls.push(new Ball(100, 0, -i * 50, 100 - i * 1));
}
}
function draw() {
frameRate(30);
background(220);
lights();
noStroke();
fill(255);
for (a = 0; a < 20; a++) {
for (let ball of balls) {
ball.update()
}
}
for (let ball of balls) {
fill(255);
push();
translate(ball.pos);
sphere(20);
pop();
}
}
class Ball {
constructor(x, y, z, l) {
this.pos = createVector(x, y, z);
this.angle = PI / 2;
this.v = 0;
this.l = l;
this.h = 1 / (2 * 30);
}
update() {
this.v = this.v - (g / this.l) * this.h * sin(this.angle)
this.angle = this.angle + this.h * this.v
let x = sin(this.angle) * this.l;
let y = cos(this.angle) * this.l;
this.pos = createVector(x, y, this.pos.z);
}
}