Skip to content

Instantly share code, notes, and snippets.

@SLOLNE
Created March 2, 2016 19:23
Show Gist options
  • Save SLOLNE/d4d881cf05aa103c17b2 to your computer and use it in GitHub Desktop.
Save SLOLNE/d4d881cf05aa103c17b2 to your computer and use it in GitHub Desktop.
a simple processing particle sketch to accompany our hardware. Its not in conversation with the hardware, but ideally would be so that when the person walks by the sensor they make a physical and digital painting simultaneously.
This code is from the example section on Prosessing.org
It is authored by: Daniel Shiffman
I just changed the background colour from balck to white and made the window a little larger.
ArrayList<ParticleSystem> systems;
void setup() {
size(900, 660);
systems = new ArrayList<ParticleSystem>();
}
void draw() {
background(255);//made this white to better fit our concept
for (ParticleSystem ps: systems) {
ps.run();
ps.addParticle();
}
if (systems.isEmpty()) {
fill(305);//made this silver
textAlign(CENTER);
text("click mouse to add particle systems", width/2, height/2);
}
}
void mousePressed() {
systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY)));
}
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ParticleSystem(int num, PVector v) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards, because we are deleting while iterating
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
void addParticle() {
Particle p;
// Add either a Particle or CrazyParticle to the system
if (int(random(0, 2)) == 0) {
p = new Particle(origin);
}
else {
p = new CrazyParticle(origin);
}
particles.add(p);
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
return particles.isEmpty();
}
}
// A subclass of Particle
class CrazyParticle extends Particle {
// Just adding one new variable to a CrazyParticle
// It inherits all other fields from "Particle", and we don't have to retype them!
float theta;
// The CrazyParticle constructor can call the parent class (super class) constructor
CrazyParticle(PVector l) {
// "super" means do everything from the constructor in Particle
super(l);
// One more line of code to deal with the new variable, theta
theta = 0.0;
}
// Notice we don't have the method run() here; it is inherited from Particle
// This update() method overrides the parent class update() method
void update() {
super.update();
// Increment rotation based on horizontal velocity
float theta_vel = (velocity.x * velocity.mag()) / 10.0f;
theta += theta_vel;
}
// This display() method overrides the parent class display() method
void display() {
// Render the ellipse just like in a regular particle
super.display();
// Then add a rotating line
pushMatrix();
translate(location.x,location.y);
rotate(theta);
stroke(105,lifespan);//changed
line(0,0,25,0);
popMatrix();
}
}
// A simple Particle class
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(255,lifespan);
fill(255,lifespan);
ellipse(location.x,location.y,8,8);
}
// Is the particle still useful?
boolean isDead() {
return (lifespan < 0.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment