color c = 256; //palette.get(cz,1); // Births particle system, based on // Daniel Shiffman class Birth { Vector3D loc; Vector3D vel; Vector3D acc; float timer = 100; // NOTE: better to let them die by location (e.g. x^2,y^2,z^2 < 10,10,10) int number = 0; //The Constructor (called when the object is first created) Birth(Vector3D a, Vector3D v, Vector3D l) { acc = a.copy(); vel = v.copy(); loc = l.copy(); } //main function to operate object) void run() { update(); dead(); render(); } //function to update location void update() { vel.add(acc); loc.add(vel); timer -= 1.0; } boolean dead() { if (timer <= 0.0) { return true; } else { return false; } } //function to display void render() { pushMatrix(); rotateX(radians(90)); rotateZ(radians(181)); rectMode(CENTER); translate(loc.x, loc.y, loc.z); //stroke(c); //c--; point(0,0,0); //sphere(1); //ellipse(1,1,0.1,0.1); popMatrix(); } } /* Particle system class */ class ParticleSystem { ArrayList particles; // An arraylist for all the particles Vector3D acceleration; // = new Vector3D(3,0,0); // The acceleration vector for particles Vector3D velocity; // The velocity of a particle Vector3D origin; // An origin point for where particles are birthed int interval = 7; // NOTE: it would be better to have this related to the birth-rate, so when you have a lower birth rate, you get a higher interval float birthrate; ParticleSystem(int num, Vector3D a, Vector3D o, Vector3D v, float br) { particles = new ArrayList(); // Initialize the arraylist acceleration = a.copy(); origin = o.copy(); // Store the origin point velocity = v.copy(); // Store the velocity vector birthrate = br; for (int i = 0; i < num; i++) { particles.add(new Birth(acceleration, velocity, origin)); // Add "num" amount of particles to the arraylist } } void run() { // Cycle through the ArrayList backwards b/c we are deleting for (int i = particles.size()-1; i >= 0; i--) { Birth b = (Birth) particles.get(i); b.run(); if (b.dead()) { particles.remove(i); } } } int iv; int particleCounter = 0; void addParticle() { if ((iv % 12) == 0) { particles.add(new Birth(acceleration, velocity, origin)); iv = interval; //(int)birthrate; particleCounter++;; } else { iv++; } } }