// gliter - By Nick - coolbubble.com - 24.10.04 color colBackground; float friction = 0.98f; float gravity = 0.1f; float startSpeed = 1.5f; int maxLife = 100; int numParticles=5000; int lastFreeParticleIndex=0; particle[] particles = new particle[numParticles]; float mouseScaler=0.3f; float mox=mouseX,moy=mouseY; void setup() { size(400,400); for (int i=0;i0) { particles[i].process(); } } // Add new particles. float mdx=(mouseX-mox)*mouseScaler; float mdy=(mouseY-moy)*mouseScaler; mox=mouseX; moy=mouseY; for (int i=0;i<8;i++) { if (lastFreeParticleIndex>numParticles-1) lastFreeParticleIndex=0; if (particles[lastFreeParticleIndex].life==0) { particles[lastFreeParticleIndex].life=maxLife; particles[lastFreeParticleIndex].x = mouseX; particles[lastFreeParticleIndex].y = mouseY; particles[lastFreeParticleIndex].vx = mdx; particles[lastFreeParticleIndex].vy = mdy; particles[lastFreeParticleIndex].addRandomVelocity(); particles[lastFreeParticleIndex].r = 255.0f-(random(1.0f)*10.0f); particles[lastFreeParticleIndex].g = 255.0f-(random(1.0f)*40.0f); particles[lastFreeParticleIndex].b = 160.0f-(random(1.0f)*20.0f); } lastFreeParticleIndex++; } } class particle { float x,y,vx,vy; float r,g,b; int life; void process() { if (!mousePressed) { applyForces(0.0,gravity); } draw(); life--; } void addRandomVelocity() { float angle = random(6.282f); float randomFactor = random(0.5f)+0.5f; vx += sin(angle) * startSpeed * randomFactor; vy += cos(angle) * startSpeed * randomFactor; } void applyForces(float fx, float fy) { vx+=fx; vy+=fy; vx*=friction; vy*=friction; x+=vx; y+=vy; if (x<0) { x=-x; vx=vx*-0.7f; } if (x>400) { x=400-(400-x); vx=vx*-0.7f; } } void draw() { float lightness = (float)life/(float)maxLife; lightness+=random(0.8f); stroke(r*lightness,g*lightness,b*lightness); if (lightness>0.8f) { line(x-2,y,x+2,y); line(x,y-2,x,y+2); } else if (lightness>0.4f) { line(x-1,y,x+1,y); line(x,y-1,x,y+1); } else point(x,y); } }