xxxxxxxxxx
// just a quick sketch to see what happens when two particles with brownian movement are attracted to each other
let particles = [];
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(2);
background(0);
let particle = new Particle(0, height/2);
particles.push(particle);
particle = new Particle(width, height/2);
particles.push(particle);
noStroke();
fill(0, 5);
}
function draw() {
rect(0,0, windowWidth,windowHeight);
for (let i=0; i<2; i++) {
particles[i].update(particles[i?0:1]);
particles[i].show();
}
}
class Particle {
constructor(x,y) {
this.pos = createVector(x,y);
this.prevPos = this.pos.copy();
}
update(other) {
let step = createVector(random(-3, 3), random(-3, 3));
let diff = createVector(this.pos.x, this.pos.y);
let point = diff.sub(other.pos);
let mag = point.mag() * -0.0005;
point.setMag(mag);
this.pos.add(step);
this.pos.add(point);
}
show() {
stroke(255);
strokeWeight(1);
line(this.pos.x, this.pos.y, this.prevPos.x, this.prevPos.y);
this.prevPos.set(this.pos);
}
}