Created
February 13, 2015 03:29
-
-
Save IanWitham/c4202a3ecc4d379ecd98 to your computer and use it in GitHub Desktop.
Processing code used to produce fireworks animations at: https://www.youtube.com/watch?v=wOhY-O0UqbI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.youtube.com/watch?v=wOhY-O0UqbI | |
ArrayList<Particle> particles; | |
PImage[] bullets; | |
PImage red_bullet; | |
PImage green_bullet; | |
PImage blue_bullet; | |
final float GRAVITY = 200; | |
void setup() { | |
frameRate(60); | |
noStroke(); | |
size(1920, 1080, P3D); | |
red_bullet = loadImage("bullet.jpg"); | |
green_bullet = loadImage("bullet2.jpg"); | |
blue_bullet = loadImage("bullet3.jpg"); | |
bullets = new PImage[]{red_bullet, green_bullet, blue_bullet}; | |
PVector GravityVector = new PVector(0, GRAVITY); | |
PVector rocket_vel = PVector.fromAngle(-PI/3.0); | |
rocket_vel.mult(150.0); | |
particles = new ArrayList<Particle>(); | |
//createExplosion(new PVector(500, 500), rocket_vel, 1); | |
for (float i=0; i<100; i++) { | |
createRocket(i*0.5); | |
} | |
} | |
void draw() { | |
background(0); | |
blendMode(ADD); | |
color black = color(0, 0, 0); | |
for (int i=0; i<8; i++) { | |
for(Particle p: particles) { | |
//float t = map(i, 0, 5, (frameCount%300/60.0)-0.8, (frameCount%300/60.0)); | |
float t = get_t() - i * 0.02; | |
if (t >= p.initial_t && t < p.end_t) { | |
//float fillVal = map(i, 0, 5, 0, 255); | |
//fill(lerpColor(p.baseColor, black, map(i, 0, 5, 1, 0))); | |
//ellipse(p.x.pos(t), p.y.pos(t), 10, 10); | |
PVector velVector = new PVector(p.x.vel(t), p.y.vel(t)); | |
pushMatrix(); | |
translate(p.x.pos(t), p.y.pos(t)); | |
rotate(velVector.heading() - PI * 0); | |
scale(0.2, 0.2); | |
drawSpark(p.image); | |
popMatrix(); | |
} | |
} | |
} | |
if (get_t() < 55.0) { | |
saveFrame(); | |
} | |
} | |
float get_t() { | |
return frameCount/60.0; | |
} | |
void createRocket(float time) { | |
PVector initial_pos = new PVector((width/2.0), height); | |
float burn_time = 1.5 + random(0.4); | |
float glide_time = 1.0 + random(0.5); | |
float thrust_mag = 400; | |
PVector thrust_vector = PVector.fromAngle(3.0*PI/2.0 + random(-0.2, 0.2)); | |
thrust_vector.setMag(thrust_mag); | |
PVector rocket_accel = new PVector(0, GRAVITY); | |
rocket_accel.add(thrust_vector); | |
Particle rocket_with_thrust = new Particle( | |
initial_pos, | |
new PVector(0, 0), | |
rocket_accel, | |
time, | |
time + burn_time, | |
color(255, 100, 100), | |
red_bullet | |
); | |
Particle rocket_without_thrust = new Particle( | |
rocket_with_thrust.pos(time + burn_time), | |
rocket_with_thrust.vel(time + burn_time), | |
new PVector(0, GRAVITY), | |
time + burn_time, | |
time + burn_time + glide_time, | |
color(255, 100, 100), | |
red_bullet | |
); | |
particles.add(rocket_with_thrust); | |
particles.add(rocket_without_thrust); | |
createExplosion( | |
rocket_without_thrust.pos(time + burn_time + glide_time), | |
rocket_without_thrust.vel(time + burn_time + glide_time), | |
time + burn_time + glide_time, | |
bullets[int(random(3))] | |
); | |
} | |
void createExplosion(PVector position, PVector rocketVector, float time, PImage image) { | |
for (int i=0;i<80;i++) { | |
float explosion_vel = 150; | |
PVector vel; | |
//vel = PVector.fromAngle(i * TWO_PI / 20.0); | |
vel = PVector.random3D(); | |
vel.mult(explosion_vel); | |
vel.add(rocketVector); | |
particles.add(new Particle( | |
position, | |
vel, | |
new PVector(0, GRAVITY), | |
time, | |
time + 1.0 + random(0.2) + random(0.2) + random(0.2), | |
color(255, 100, 100), | |
image | |
)); | |
} | |
} | |
void drawSpark(PImage image) { | |
//triangle(-14, 14, 14, 14, 0, -20); | |
image(image, -32, -32); | |
//ellipse(0, 0, 10, 10); | |
} | |
class Particle { | |
color baseColor; | |
Motion x; | |
Motion y; | |
float initial_t; | |
float end_t; | |
PImage image; | |
Particle(PVector position, PVector velocity, PVector accel, float initial_t, float end_t, color baseColor, PImage image){ | |
this.x = new Motion(position.x, velocity.x, accel.x, initial_t); | |
this.y = new Motion(position.y, velocity.y, accel.y, initial_t); | |
this.initial_t = initial_t; | |
this.end_t = end_t; | |
this.baseColor = baseColor; | |
this.image = image; | |
} | |
PVector pos(float t){ | |
return new PVector(this.x.pos(t), this.y.pos(t)); | |
} | |
PVector vel(float t){ | |
return new PVector(this.x.vel(t), this.y.vel(t)); | |
} | |
PVector accel(){ | |
return new PVector(this.x.accel, this.y.accel); | |
} | |
} | |
class Motion { | |
float initial_pos; | |
float initial_vel; | |
float accel; | |
float initial_t; | |
float t0_pos; | |
float t0_vel; | |
Motion(float initial_pos, float initial_vel, float accel, float initial_t) { | |
this.accel = accel; | |
this.t0_vel = 0; | |
this.t0_vel = initial_vel - vel(initial_t); | |
this.t0_pos = 0; | |
this.t0_pos = initial_pos - pos(initial_t); | |
} | |
public float vel(float t){ | |
return this.accel * t + this.t0_vel; | |
} | |
public float pos(float t){ | |
return 0.5 * this.accel * pow(t, 2) + this.t0_vel * t + this.t0_pos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well organized