// catherine wheel - By Nick - coolbubble.com - 5.11.04 color colBackground; float friction = 0.96f; float gravity = 0.2f; float startSpeed = 1.5f; int maxLife = 50;//100; int numParticles=2000;//5000; int lastFreeParticleIndex=0; particle[] particles = new particle[numParticles]; float mouseScaler=0.3f; float mox=mouseX,moy=mouseY; float wheelSpeed = 0; float wheelAngle = 0; float wheelPosX=200; float wheelPosY=200; float wheelRadius = 50; 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++; } */ // Process wheel. float forceFraction=0.01f; float spinForce = 0.15f; wheelSpeed = (wheelSpeed*(1.0-forceFraction))+(spinForce*(forceFraction)); wheelAngle += wheelSpeed; if (mousePressed) wheelSpeed=0; // We need to calculate four directions, but they are all perpendicular so we can get away with one calculation float dx = sin(wheelAngle); float dy = cos(wheelAngle); float vx = -dy; float vy = dx; // Compute colours. float mouseCol = ((float)mouseX/400.0) * 255.0; float mouseSat = ((float)mouseY/400.0) * 255.0; colorMode(HSB, 255); color c = color(mouseCol, mouseSat, 255); float r = red(c); float g = green(c); float b = blue(c); mouseCol-=128; if (mouseCol<0) mouseCol+=255; color c2 = color(mouseCol, mouseSat, 255); float r2 = red(c2); float g2 = green(c2); float b2 = blue(c2); colorMode(RGB, 255); // Add particles addParticles(5, wheelPosX+(wheelRadius*dx), wheelPosY+(wheelRadius*dy), vx*8.0, vy*8.0, r,g,b); addParticles(5, wheelPosX+(wheelRadius*-dx), wheelPosY+(wheelRadius*-dy), -vx*8.0, -vy*8.0, r2,g2,b2); } void addParticles(int num, float x, float y, float dx, float dy, float r, float g, float b) { 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 = x; particles[lastFreeParticleIndex].y = y; particles[lastFreeParticleIndex].vx = dx; particles[lastFreeParticleIndex].vy = dy; particles[lastFreeParticleIndex].addRandomVelocity(); particles[lastFreeParticleIndex].r = r-(random(1.0f)*10.0f); particles[lastFreeParticleIndex].g = g-(random(1.0f)*40.0f); particles[lastFreeParticleIndex].b = b-(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; } if (y>400) { y=400-(400-y); vy=vy*-0.5f; } } 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); } }