/* * Model for rendering fireworks at location of mouse click. */ // parameters to tune model int MAX_NUM = 200; // number of particles float MAX_SPEED = 2; int MAX_LIFE = 120; int numLive = 0; // number of currently live particles int cx, cy; // location of active mouse click int time; // time units since active mouse clock // parameters to model particle j float angle[] = new float[MAX_NUM]; // theta[j] is angle of motion float speed[] = new float[MAX_NUM]; // speed[j] is velocity int alive[] = new int[MAX_NUM]; // alive[j] is number of remaining time units void setup() { size(600, 400); noStroke(); } void draw() { background(0); if (numLive > 0) { time++; for (int j=0; j < MAX_NUM; j++) { if (alive[j] > 0) { float x = cx + time*speed[j]*cos(angle[j]); float y = cy + time*speed[j]*sin(angle[j]); ellipse(x,y,3,3); alive[j]--; if (alive[j] == 0) { numLive--; } } } } } void mouseClicked() { if (numLive == 0) { // start new explosion fill(random(127,255),random(127,255),random(127,255)); time = 0; cx = mouseX; cy = mouseY; numLive = MAX_NUM; for (int j=0; j < numLive; j++) { angle[j] = random(TWO_PI); speed[j] = random(0.0*MAX_SPEED, MAX_SPEED); alive[j] = int(random(0.3*MAX_LIFE, MAX_LIFE)); } } }