var particles = []
let value = 0
var r
var g
var b
function setup() {
createCanvas(640, 360);
}
function mousePressed() {
var p = new Particle(mouseX, mouseY, random(3, 7))
particles.push(p);
r = random(255);
g = random(255);
b = random(255);
}
function draw() {
background(0);
for (i = 0; i < particles.length; i++) {
var gravity = createVector(0, 0.1 * particles[i].mass);
var wind = createVector(0.05 * particles[i].mass,0);
particles[i].applyForce(gravity);
particles[i].applyForce(wind);
particles[i].display();
particles[i].update();
particles[i].edges();
}
}
function Particle(x, y, m) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = m
this.display = function() {
fill(r,g,b);
ellipse(this.pos.x, this.pos.y, this.mass * 3, this.mass * 3);
}
this.applyForce = function(force) {
var f = force.copy();
f.div(this.mass)
this.acc.add(f)
}
this.update = function() {
var mouse = createVector(mouseX, mouseY);
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
this.edges = function() {
if (this.pos.y > height - this.mass * 3 / 2) {
this.vel.y *= -1
this.pos.y = height - this.mass * 3 / 2;
}
if (this.pos.x > width - this.mass * 3 / 2) {
this.vel.x *= -1
this.pos.x = width - this.mass * 3 / 2;
}
if (this.pos.y - this.mass * 3 < 0) {
this.vel.y *= -1
this.pos.y = this.mass * 3;
}
if (this.pos.x < this.mass * 3 / 2) {
this.vel.x *= -1
this.pos.x = this.mass * 3 / 2;
}
}
}