Skip to content

Instantly share code, notes, and snippets.

@ThomasLengeling
Created February 18, 2015 00:41
Show Gist options
  • Save ThomasLengeling/28346e529780b4e467ba to your computer and use it in GitHub Desktop.
Save ThomasLengeling/28346e529780b4e467ba to your computer and use it in GitHub Desktop.
/*
Simple explotion particles in Processing
by
Thomas Sanchez Lengeling
*/
import java.util.Vector;
Vector mParticles;
//number of particles created when mouse is pressed
int min = 30;
void setup() {
size(1024, 768);
smooth(8);
mParticles = new Vector<Particle>();
}
void draw() {
crear(color(0, 5));
//draw all the particles
for (int i = 0; i < mParticles.size (); ++i) {
Particle p = (Particle)mParticles.get(i);
p.draw();
p.update();
}
//clean the particles that are dead
for (int i = 0; i < mParticles.size (); ++i) {
Particle p = (Particle)mParticles.get(i);
if(p.getLife() < 0){
mParticles.remove(i);
}
}
//check if the particles are actualy dying
//println(mParticles.size ());
}
void mousePressed() {
for (int i = 0; i < min; i++) {
mParticles.add(new Particle(mouseX, mouseY));
}
}
void crear(color col) {
noStroke();
fill(col);
rect(0, 0, width, height);
}
//PARTICLE CLASS
class Particle {
private float posX;
private float posY;
private float velX;
private float velY;
private float dirX;
private float dirY;
private float life;
private float lifeInc;
Particle(float x, float y) {
this.posX = x;
this.posY = y;
velX = random(0.5, 1.8);
velY = random(0.5, 1.8);
dirX = random(-1, 1);
dirY = random(-1, 1);
life = random(30, 85);
lifeInc = random(0.2, 0.5);
}
public float getLife() {
return life;
}
public void draw() {
if (life > 0) {
noStroke();
fill(life, 120 + life*2, 150 + life*1.5, 85 - life);
float lifeTam = 25 - life/2.0;
ellipse(posX, posY, lifeTam, lifeTam);
}
}
public void update() {
life -= lifeInc;
//update direction of the particles
posX += velX * dirX;
posY += velY * dirY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment