Created
May 21, 2016 13:18
-
-
Save crummy/f405551e1e0460b8beb7ac1a0c8affe5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import processing.core.PApplet; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
public class Fireburst extends PApplet { | |
private Set<FlameGenerator> generators; | |
public static void main(String args[]) { | |
PApplet.main("Fireburst"); | |
} | |
@Override | |
public void settings() { | |
size(800, 600); | |
} | |
@Override | |
public void setup() { | |
clear(); | |
generators = new HashSet<>(); | |
generators.add(new FlameGenerator(width*0.33f, height*0.2f)); | |
generators.add(new FlameGenerator(width*0.5f, height*0.3f)); | |
generators.add(new FlameGenerator(width*0.66f, height*0.4f)); | |
colorMode(HSB); | |
} | |
@Override | |
public void draw() { | |
fill(50, 0, 0, 60); | |
rect(0, 0, width, height); | |
generators.forEach(FlameGenerator::update); | |
generators.forEach( | |
generator -> generator | |
.particles | |
.stream() | |
.sorted((p1, p2) -> Float.compare(p1.position.z, p2.position.y)) | |
.forEach(this::drawParticle) | |
); | |
} | |
private void drawParticle(Particle particle) { | |
noStroke(); | |
fill(map(particle.lifetime(), 0, 1, 0, 50), 255, 255, map(particle.lifetime(), 0, 1, 255, 0)); | |
float size = particle.size + cos(particle.age * 0.04f) * particle.position.x * 0.1f * (1-particle.lifetime()); | |
ellipse(particle.spawnPosition.x + sin(particle.age * 0.04f) * particle.position.x, particle.position.y + particle.spawnPosition.y, size, size); | |
} | |
private static class Vector3 { | |
float x; | |
float y; | |
float z; | |
Vector3(float x, float y, float z) { | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
} | |
} | |
private class Particle { | |
Vector3 position; | |
Vector3 velocity; | |
Vector3 spawnPosition; | |
int age; | |
float size; | |
int maxAge; | |
Particle(Vector3 position, Vector3 velocity) { | |
this.spawnPosition = position; | |
this.position = new Vector3(0,0,0); | |
this.velocity = velocity; | |
this.age = 0; | |
this.size = 8; | |
this.maxAge = (int)random(80, 200); | |
} | |
void update() { | |
position.x += velocity.x; | |
position.y += velocity.y; | |
position.z += velocity.z; | |
age += 1; | |
position.x += random(-1f, 1f); | |
velocity.y *= 0.99f; | |
velocity.x *= 0.99f; | |
velocity.z *= 0.99f; | |
} | |
float lifetime() { | |
return map(age, 0, maxAge, 0, 1); | |
} | |
boolean isAlive() { | |
return age < maxAge; | |
} | |
} | |
private class FlameGenerator { | |
final float x; | |
final float y; | |
Set<Particle> particles; | |
private FlameGenerator(float x, float y) { | |
this.x = x; | |
this.y = y; | |
particles = new HashSet<>(); | |
} | |
void update() { | |
Vector3 particleVelocity = new Vector3(random(-0.5f, 0.5f), random(0.5f, 4f), random(-0.01f, 0.01f)); | |
particles.add(new Particle(new Vector3(x + random(8), y + random(4), 0), particleVelocity)); | |
particles.forEach(Particle::update); | |
particles = particles.stream().filter(Particle::isAlive).collect(Collectors.toSet()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment