Particle Systems

I created a system where the particles move through the screen using forces. To achieve this, I used one of my previous assignments and modified it to create a particle behavior, adding some new functionality to make it look like growing roots. When the used presses the mouse, the sketch will use its location to emit random mover lines through the screen.




Processing code:

ParticleSystem ps; void setup() { size(700, 800); background(50, 25, 0); ps = new ParticleSystem(new PVector(mouseX,mouseY)); } void draw() { ps.addParticle(); ps.run(); } class Particle { float radius, topspeed, resize; PVector location, velocity, acceleration; float lifespan; Particle(PVector _location) { radius=3; topspeed=2; location = _location.get(); velocity=new PVector(0, 0); lifespan = random(0.0, 100.0); } void display() { noStroke(); fill(250, 200, 50, lifespan); ellipse(location.x, location.y, radius, radius); } void move() { acceleration=new PVector(random(-5, 5), random(-5, 5)); velocity.limit(topspeed); acceleration.normalize(); acceleration.mult(.25); velocity.add(acceleration); location.add(velocity); lifespan -= 0.2; } boolean isDead() { if (lifespan < 0.0) { return true; } else { return false; } } } class ParticleSystem { ArrayList particles; ParticleSystem(PVector location) { particles = new ArrayList(); } void addParticle() { if (mousePressed) { particles.add(new Particle(new PVector(mouseX, mouseY))); } } void run() { for (int i=0; i< particles.size (); i++) { Particle particle = (Particle) particles.get(i); particle.move(); particle.display(); if (particle.isDead()) { particles.remove(i); } } } }

Leave a Reply