int ALPH = 0x20000000; int NUM_PARTICLES = 20; int NUM_ATTRACTORS = 8; float damp = 0.9; float accel = 10; color[] colours = { ALPH | 0x00A3D39C, ALPH | 0x0086AD80, ALPH | 0x00698764, ALPH | 0x00C9D49D, ALPH | 0x00A4AD80, ALPH | 0x00808764, ALPH | 0x002B2B2B, ALPH | 0x00D4D4D4, ALPH | 0x00000000 }; Particle[] particle; Attractor[] attractor; class Attractor { float x,y; Attractor() { x = random(width); y = random(height); } } class Particle { float x,y,vx,vy,px,py; Particle() { x = px = random(width); y = py = random(height); // vx = random(1) > 0.5 ? random(-15,-12) : random(12,15); // vy = random(1) > 0.5 ? random(-15,-12) : random(12,15); vx = random(-accel/2,accel/2); vy = random(-accel/2,accel/2); } void step() { px = x; py = y; for (int i = 0; i < attractor.length; i++) { float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y); if (d2 > 0.1) { vx += accel * (attractor[i].x-x) / d2; vy += accel * (attractor[i].y-y) / d2; } } x += vx; y += vy; vx *= damp; vy *= damp; } } void setup() { size(400,400); background(255); stroke(255); smooth(); noFill(); attractor = new Attractor[NUM_ATTRACTORS]; for (int i = 0; i < attractor.length; i++) { attractor[i] = new Attractor(); } particle = new Particle[NUM_PARTICLES]; for (int i = 0; i < particle.length; i++) { particle[i] = new Particle(); } } void loop() { for (int j = 0; j < particle.length; j++) { stroke(colours[j%colours.length]); line(particle[j].x,particle[j].y,particle[j].px,particle[j].py); particle[j].step(); } } void mousePressed() { background(255); for (int i = 0; i < attractor.length; i++) { attractor[i] = new Attractor(); } }