Hello,
I've been playing with Daniel Shiffman Multiple Particle System,
trying to build with Processing a
mockup animation I did in After effect.
Firstly, I can't figure out how to give a color to one particle at birth
and make it fade to another. (e.g. A triangle shape that starts red and finish yellow).
RIght now what I try keeps changing the colors at each frame..
Secondly, I don't know how to make them scale down before "dying". I've look on the
scale() methods, without success.
Any idea will be much appreciated :)
- ArrayList psystems;
- void setup() {
- size(800, 600);
- colorMode(RGB, 255, 255, 255, 100);
- psystems = new ArrayList();
- smooth();
- }
- void draw() {
- background(0);
- for (int i = psystems.size()-1; i >= 0; i--) {
- ParticleSystem psys = (ParticleSystem) psystems.get(i);
- psys.run();
- if (psys.dead()) {
- psystems.remove(i);
- }
- }
- psystems.add(new ParticleSystem(int(random(1,3)), 0.1, new PVector(random(350,450),700)));
- }
- color[] colors = {
- color(0xC94053, 255), color(0x993559, 255), color(0x6B274E, 255), color(0x421F3E, 255), color(0xFFAA68, 255)
- };
- // A simple Particle class
- class Particle {
- PVector loc;
- PVector vel;
- PVector acc;
- float r;
- float timer;
- // Particle Constructor
- Particle(PVector l) {
- acc = new PVector(0,0,0);
- vel = new PVector(random(-0.05,0.05),random(-3,0),0);
- loc = l.get();
- r = 10.0;
- //durée de vie
- timer = 150.0;
- }
- void run() {
- update();
- render();
- }
- // Method to update location
- void update() {
- vel.add(acc);
- loc.add(vel);
- timer -= 1.0;
- // println(timer);
- }
- // Method to display
- void render() {
- noStroke();
- fill(colors[(int)random(colors.length)],timer);
- triangle(loc.x, loc.y, loc.x+40, loc.y-90, loc.x+80, loc.y);
- }
- // Is the particle still useful?
- boolean dead() {
- if (timer <= 0.0) {
- return true;
- } else {
- return false;
- }
- }
- }
- // An ArrayList is used to manage the list of Particles
- class ParticleSystem {
- ArrayList particles; // An arraylist for all the particles
- PVector origin; // An origin point for where particles are birthed
- ParticleSystem(int pNum,float pChance, PVector v) {
- particles = new ArrayList(); // Initialize the arraylist
- origin = v.get(); // Store the origin point
- for (int i = 0; i < pNum; i++) {
- if (random(1) < pChance) {
- particles.add(new Particle(origin));
- }
- }
- }
- void run() {
- // Cycle through the ArrayList backwards b/c we are deleting
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = (Particle) particles.get(i);
- p.run();
- if (p.dead()) {
- particles.remove(i);
- }
- }
- }
- // A method to test if the particle system still has particles
- boolean dead() {
- if (particles.isEmpty()) {
- return true;
- }
- else {
- return false;
- }
- }
- }
1