xxxxxxxxxx
52
var Particle = function(x, y, mass) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = mass;
this.applyForce = function(f) {
this.acc.add(p5.Vector.div(f, this.mass));
}
this.update = function() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc = createVector(0, 0);
}
this.draw = function() {
fill(220);
ellipse(this.pos.x, this.pos.y, 48, 48);
}
}
var maxParticles = 6;
var particle = []
function setup() {
createCanvas(640, 480);
for (var i = 0; i < maxParticles; i++) {
particle.push(
new Particle(
(i + 1) * (width / (maxParticles + 1)),
//i * (height / (maxParticles + 1))));
height / 2,
random(1, 3)));
}
}
function draw() {
background(160);
var gravity = createVector(0, 0.1);
for (var i = 0; i < maxParticles; i++) {
particle[i].applyForce(gravity);
particle[i].update();
particle[i].draw();
}
}