int cnt = 0; int maxParticles = 50; int timeInterval = 10; int particleNum = 0; Particle[] particles = new Particle[maxParticles]; void setup() { frameRate(30); smooth(); size(400,400); background(127); } void draw() { fill(127,10); rect(0,0,width,height); if (particleNum < maxParticles) { cnt = (cnt + 1) % timeInterval; if (cnt == 0) { particles[particleNum] = new Particle(); particleNum ++; } } for (int i = 0; i < particleNum; i++) { for (int j = 0; j < particleNum; j ++) { particles[i].attract(particles[j]); } particles[i].update(); } } class Particle { private int life; public float x,y,vx,vy,mass; Particle () { init(); } void init() { life = timeInterval * maxParticles; mass = random(2,20); x = random(width); y = random(height); float angle = random(360); vx = cos(radians(angle)) * 10; vy = sin(radians(angle)) * 10; } void attract(Particle p) { float distance = sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); float massProduct = mass * p.mass; if (distance != 0) { vx += (massProduct * (p.x - x) / distance) / mass / maxParticles / 2; vy += (massProduct * (p.y - y) / distance) / mass / maxParticles / 2; } } void update() { life --; if (life == 0) { init(); } x += vx; y += vy; vx *= 0.9; vy *= 0.9; if (x > width) { x -= width; } if (x < 0) { x += width; } if (y > height) { y -= height; } if (y < 0) { y += height; } stroke(200); noFill(); line(x, y ,x - vx * mass / 10, y - vy * mass / 10); ellipse(x, y, mass / 4, mass / 4); } }