class Particle { float x, y; float vx, vy; float radius; float gravity = 0.01; // Constructor for Particle class Particle(int xpos, int ypos, float velx, float vely, float r) { x = xpos; y = ypos; vx = velx; vy = vely; radius = r; } void update() { vy = vy + gravity; y += vy; x += vx; } void display() { fill(x, y, x+y, 20); ellipse(x, y, radius*2, radius*2); } } class GenParticle extends Particle { float originX, originY; float friction = 0.99; // Constructor for GenParticle class GenParticle(int xIn, int yIn, float vxIn, float vyIn, float r, float ox, float oy) { super(xIn, yIn, vxIn, vyIn, r); originX = ox; originY = oy; } void regenerate() { if ((x > width+radius) || (x < -radius) || (y > height+radius) || (y < -radius)) { // Original values were originX and originY // however by changing this to mouseX and mouseY // the sketch becomes an interactive piece x = mouseX; y = mouseY; vx = random(-1.0, 1.0); vy = random(-4.0, -2.0); vy *= friction; vx *= friction; super.update(); } if (y > height-radius) { vy = -vy; y = constrain(y, -height*height, height-radius); } if ((x < radius) || (x > width-radius)) { vx = -vx; x = constrain(x, radius, width-radius); } } } float radius = 1.2; int numParticles = 300; // Stores an array to create 200 particles at a time GenParticle[] p = new GenParticle[numParticles]; void setup() { colorMode(RGB); size(800, 600); noStroke(); smooth(); for (int i = 0; i < p.length; i++) { float velX = random(-1, 1); float velY = -i; // Inputs: x, y, x-velocity, y-velocity, radius, origin x, origin y p[i] = new GenParticle(width/2, height/2, velX, velY, 5.0, width/2, height/4); } } void draw() { fill(0, 36); rect(0, 0, width, height); for (int i = 0; i < p.length; i++) { p[i].update(); p[i].regenerate(); p[i].display(); } }