xxxxxxxxxx
76
// Worked off Daniel Shiffman's pendulum simulation array https://youtu.be/NBWMtlbbOag
let penduls = [];
let spacing = 20;
function setup() {
createCanvas(800, 800);
let total = floor(height / spacing);
for (let i = 0; i < total; i++) {
x = i * spacing;
penduls[i] = new Pendulum(x, 0, 50+i*20, spacing);
}
}
function draw() {
background(21, 8, 50, 30);
for (let pendul of penduls) {
pendul.update();
pendul.show();
if (mouseIsPressed){
let wind = 0.05;
pendul.applyForce(wind);
}
}
}
// function mousePressed(){
// let wind = 0.002;
// pendul.applyForce(wind);
// }
class Pendulum {
constructor(x, y, r, bobR) {
this.origin = createVector(x, y);
this.position = createVector();
this.r = r;
this.angle = PI / 4;
this.aVelocity = 0.0;
this.aAcceleration = 0.0;
this.damping = 0.99;
this.ballr = bobR;
}
update() {
let gravity = 0.4;
this.aAcceleration = (-1 * gravity / this.r) * sin(this.angle);
this.aVelocity += this.aAcceleration;
this.aVelocity *= this.damping;
this.angle += this.aVelocity;
}
show() {
this.position.set(this.r * sin(this.angle), this.r * cos(this.angle), 0);
this.position.add(this.origin);
stroke(252, 238, 33);
strokeWeight(2);
line(this.origin.x, this.origin.y, this.position.x, this.position.y);
ellipseMode(CENTER);
noStroke();
fill(255, 246, 152);
ellipse(this.position.x, this.position.y, this.ballr*0.5);
}
applyForce(f) {
this.angle += f;
}
gravity(){
let gravity = (-1*0.6/this.ballr)*sin(this.aAcceleration);
applyForce(gravity);
}
}