xxxxxxxxxx
81
var myParticles = [];
var nParticles = 150;
function setup() {
createCanvas(400, 400);
initParticles();
}
function draw() {
background(200);
var nsc = 0.01; // noise scale
for (var y = 0; y <= height; y += 10) {
for (var x = 0; x <= width; x += 10) {
var fx = noise(1 + x * nsc, 5 + y * nsc) - 0.5;
var fy = noise(9 + x * nsc, 7 + y * nsc) - 0.5;
line(x, y, x + 50 * fx, y + 50 * fy);
}
}
var fieldStrength = 1.0;
for (var i = 0; i < nParticles; i++) {
var x = myParticles[i].px;
var y = myParticles[i].py;
var fx = fieldStrength * (noise(1 + x * nsc, 5 + y * nsc) - 0.5);
var fy = fieldStrength * (noise(9 + x * nsc, 7 + y * nsc) - 0.5);
myParticles[i].impel(fx, fy);
myParticles[i].update();
myParticles[i].render();
}
}
function mousePressed() {
initParticles();
}
function initParticles() {
noiseSeed(millis());
myParticles = [];
for (var i = 0; i < nParticles; i++) {
var rx = random(width * 0.05, width * 0.95);
var ry = random(width * 0.05, width * 0.95);
myParticles.push(new Particle(rx, ry, 0, 0));
}
}
//----------------------------------------
class Particle {
constructor(inpx, inpy, invx, invy) {
this.mass = 1.0;
this.FRICTION = 0.95;
this.px = inpx;
this.py = inpy;
this.vx = invx;
this.vy = invy;
}
impel(fx, fy) {
// Add an acceleration (a = F/m)
this.vx += fx / this.mass;
this.vy += fy / this.mass;
}
update() {
// Euler integration
this.vx *= this.FRICTION;
this.vy *= this.FRICTION;
this.px += this.vx;
this.py += this.vy;
}
render() {
ellipse(this.px, this.py, 20, 20);
}
}