• fullscreen
  • Particle.pde
  • sketch_mar27a.pde
  • class Particle {
      PVector loc;
      PVector vel;
      PVector acc;
      float ms;
      float distance;
      
      Particle(PVector a, PVector v, PVector l, float ms_) {
        acc = a;
        vel = v;
        loc = l;
        ms = ms_;
      }
     
      void run() {
        update();
        render();
      }
    
      void update() {
        vel.add(acc);
        loc.add(vel);
        acc = new PVector();   
      }
     
      void render() {
        float cl = map(vel.mag(), 0, 5.0, 0, 60);  
        float al = map(vel.mag(), 0, 1.2, 0, 30); 
        float m = map(millis(), 0, 20000, 0, 30);   
        if(millis() < 20000){            
            stroke(cl, 99, 99, m);
        }
        else stroke(cl, 99, 99, al);   
        point(loc.x,loc.y);
      }
     
     void add_force(PVector force) { 
        force.div(ms);
        vel.add(force);
      }  
      PVector getLocation() {
        return loc;
      } 
      void move(PVector target) {
        acc.add(steer(target));
      }  
    
      PVector steer(PVector target) {
        PVector steer;
        PVector desired = PVector.sub(target,loc);
        float d = desired.mag();
        desired.normalize();
        desired.mult(10.0);
        steer = PVector.sub(desired,vel);
        steer.limit(4.0f);
        steer.div(ms);
        return steer;
      }
    }
    
    
    
    float numPart = 2000;
    float r = 40.0;
    float[] th = {0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f, 5.5f, 6.0f, 6.5f};
    float[] xx = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
    float[] yy = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; 
    float x, y;
    float inx, iny;
    PVector centerLoc;
    
    
    ArrayList particles1 = new ArrayList();
    ArrayList particles2 = new ArrayList();
    ArrayList particles3 = new ArrayList();
    ArrayList particles4 = new ArrayList();
    ArrayList particles5 = new ArrayList();
    ArrayList particles6 = new ArrayList();
    ArrayList particles7 = new ArrayList();
    ArrayList particles8 = new ArrayList();
    ArrayList particles9 = new ArrayList();
    ArrayList particles10 = new ArrayList();
    ArrayList particles11 = new ArrayList();
    ArrayList particles12 = new ArrayList();
    ArrayList particles13 = new ArrayList();
    
    void setup()
    {
      size(640, 480, P2D);
      colorMode(HSB, 360, 100, 100, 100);
      smooth();
      inx = width/2;
      iny = height/2;
      for(int i=0; i<numPart; i++){
        x = r * cos(th[0]);
        y = r * sin(th[0]);
        x += inx;
        y += iny;
        particles1.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles2.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles3.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles4.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles5.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles6.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles7.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles8.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles9.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles10.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles11.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        particles12.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,50.0f)));
        particles13.add(new Particle(new PVector(), new PVector(), new PVector(x, y, 0), random(10.0f,70.0f)));
        th[0] += 0.1;
      }
      centerLoc = new PVector(width/2, height/2, 0);
    }
    
    void draw()
    {
        background(1, 100, 0, 100);
    
        for(int z=0; z<13; z++)
        {
          xx[z] = r * cos(th[z+1]);
          yy[z] = r * sin(th[z+1]);
          xx[z] += inx;
          yy[z] += iny;
          xx[z] += random(-20.0, 20.0);
          yy[z] += random(-20.0, 20.0);   
          th[z+1] += 0.001f; 
        }
        
    
        for (int i = particles1.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles1.get(i);
          prt.run();
          prt.move(new PVector(xx[0],yy[0],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
        for (int i = particles2.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles2.get(i);
          prt.run();  
          prt.move(new PVector(xx[1],yy[1],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles3.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles3.get(i);
          prt.run();  
          prt.move(new PVector(xx[2],yy[2],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
        for (int i = particles4.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles4.get(i);
          prt.run();
          prt.move(new PVector(xx[3],yy[3],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles5.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles5.get(i);
          prt.run();  
          prt.move(new PVector(xx[4],yy[4],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);
        }
        for (int i = particles6.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles6.get(i);
          prt.run();  
          prt.move(new PVector(xx[5],yy[5],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles7.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles7.get(i);
          prt.run();
          prt.move(new PVector(xx[6],yy[6],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
        for (int i = particles8.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles8.get(i);
          prt.run();  
          prt.move(new PVector(xx[7],yy[7],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles9.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles9.get(i);
          prt.run();  
          prt.move(new PVector(xx[8],yy[8],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
        for (int i = particles10.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles10.get(i);
          prt.run();
          prt.move(new PVector(xx[9],yy[9],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles11.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles11.get(i);
          prt.run();  
          prt.move(new PVector(xx[10],yy[10],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
        for (int i = particles12.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles12.get(i);
          prt.run();  
          prt.move(new PVector(xx[11],yy[11],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(0.6f); 
          prt.add_force(diff);     
        }
        for (int i = particles13.size()-1; i >= 0; i--) {
          Particle prt = (Particle) particles13.get(i);
          prt.run();  
          prt.move(new PVector(xx[12],yy[12],0));
          PVector diff = PVector.sub(centerLoc,prt.getLocation()); 
          diff.normalize(); 
          diff.mult(1.2f); 
          prt.add_force(diff);     
        }
    
        fill(0, 0, 0);
        noStroke();
        
        ellipse(width/2, height/2, 65, 65);
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Giovanni Carlo Mingati
    Digital_Babu
    28 Mar 2009
    Gorgeous... too bad I'm just a beginner in programming to really comment on the code.

    Great use of processing! No show us the 3d version, :p.
    jaaq jorissen
    28 Mar 2009
    this is amazing!
    this thing looks pretty sweet.

    You could probably optimize slightly the code by defining variables outside of the loops.

    like:
    int i, float p = particles3.size()-1;
    for(i=p; i &gt;= 0; i--)
    Liangjie Xia
    9 Apr 2009
    this is beautiful!!
    Xiaohan Zhang
    22 May 2009
    that looks reaally nice. i love how it changes over time.
    you can save a lot of coding by using an array list of array lists. so instead of defining particles1, particles2, particles3, etc., define ArrayList particleList, and use a for loop
    Xiaohan Zhang
    22 May 2009
    Also, it looks like you've only got one particle per array list?
    Hello Jill, thank you.
    An array of arrayLists?
    Is what i was trying to do but during the development i ended up with this:
    anemone http://www.openprocessing.org/visuals/?visualID=1439

    I stopped prototyping without having the full control of the Processing IDE so i just bought the famous "Processing: A Programming Handbook for Visual Designers and Artists" book and will come back experimenting as soon as i got a solid knowledge of what could be made and how (in a much efficient way).

    ;-P
    GC
    haptiK
    4 Jan 2010
    Love this!
    Ale
    24 Aug 2011
    Nice.
    It remembers me this piece from Ars Thanea:
    http://blog.arsthanea.com/2009/01/05/black-hole/
    Regards!
    You need to login/register to comment.