Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/085893414d1addd16d46 to your computer and use it in GitHub Desktop.
Save jkwok91/085893414d1addd16d46 to your computer and use it in GitHub Desktop.
class Firework {
Spark[] sparks;
Spark ignition;
PVector origin;
color col;
Firework(int x, int y, color c) {
col = c;
origin = new PVector(x, y);
sparks = new Spark[ceil(random(width/25, width/10))];
for (int i = 0; i < sparks.length; i++) {
sparks[i] = new Spark(origin.x, origin.y, col);
}
ignition = new Spark(origin.x, height, col);
// depending on velocity, some of them are duds ('realism')
ignition.vel = new PVector(0, -1*(height/30));
}
void run() {
//when ignition.location == origin, kill it and explode the sparks
if (ignition.loc.y >= origin.y) {
ignition.update();
ignition.display();
}
else {
//explode!
for (Spark s : sparks) {
if (!(s.isDead())) {
s.update();
s.display();
}
}
}
}
boolean isDead() {
boolean flag = true;
for (Spark s : sparks) {
flag = flag && s.isDead();
}
return flag;
}
}
/*
misha's fireworks
onclick, launch a particle up to that point and then explode it
*/
int w = 500;
int h = 500;
int cx, cy;
ArrayList<Firework> fshow; // fireworks show!
color yellow = color(255, 255, 0);
void setup() {
size(w, h, P2D);
cx = width/2;
cy = height/2;
background(0);
fshow = new ArrayList<Firework>();
}
void draw() {
background(0);
if (fshow.size() < 1) {
fill(255);
text("click to launch more!", cx, cy);
}
for (int i = fshow.size()-1; i >= 0; i--) {
Firework f = fshow.get(i);
if (f.isDead()) {
fshow.remove(i);
}
else {
f.run();
}
}
}
void mousePressed() {
color c = color(255, random(255), random(255));
// new firework
fshow.add(new Firework(mouseX, mouseY, c));
}
class Spark {
PVector loc;
PVector vel;
PVector acc;
float r = 7;
int t = 5;
PVector[] tail = new PVector[t];
float opacity;
color fcol;
Spark(float x, float y, color col) {
loc = new PVector(x, y);
vel = new PVector(random(-2, 2), random(-4, 0));
acc = new PVector(0, 0.0981);
for (int i = 0; i < t; i++) {
tail[i] = loc.get();
}
opacity = 100-t;
fcol = col;
}
void update() {
vel.add(acc);
loc.add(vel);
for (int i = 0; i < t-1; i++) {
tail[i] = tail[i+1];
}
tail[tail.length-1] = loc.get();
}
boolean isDead() {
return loc.y > height;
}
void display() {
// create a fade
for (int i = t-1; i >= 0; i--) {
PVector pos = tail[i];
stroke(fcol, opacity+i);
fill(fcol, opacity+i);
ellipse(pos.x, pos.y, r*pow(.75, t-i), r*pow(.75, t-i));
}
opacity-=0.5;
}
String toString() {
return "loc: ("+loc.x+","+loc.y+")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment