• fullscreen
  • p0112b_2BCD.pde
  • //Raven Kwok (aka Guo Ruiwen)
    //ravenkwok.com
    /*
    Inspired by Ale's(http://openprocessing.org/user/12899) drawingMachine_10(http://openprocessing.org/sketch/34320).
    */
    
    ParticleController pc;
    
    void setup(){
      size(400,400);
      smooth();
      background(255);
      pc = new ParticleController();
      pc.pAddCircle(1000,100);
      pc.iAddCircle(100,100);
    }
    
    void draw(){
      pc.update(10);
      pc.display();
    }
    
    void keyPressed(){
      if(key == 'r'){
        background(255);
        pc = new ParticleController();
        pc.pAddCircle(1000,100);
        pc.iAddCircle(100,100);
      }
    }
    class Interference{
      
      String mode; 
      PVector mLoc;
      float range;
      
      Interference(float x, float y){
        
        mLoc = new PVector(x,y);
        
        float randFloat = random(1);
        if(randFloat<0.5) mode = "ATTRACT";
        else mode = "REJECT";
        
        range = random(0,300);
      }
    }
    class Particle {
    
      PVector mLoc;
      PVector dir;
    
      Particle(float x, float y) {
        mLoc = new PVector(x, y);
      }
    
      void update(Interference itf, float force) {
        float distance = dist(itf.mLoc.x, itf.mLoc.y, mLoc.x, mLoc.y);
        if (distance<itf.range) {
          if (itf.mode.equals("ATTRACT")) {
            dir = PVector.sub(itf.mLoc, mLoc);
          }
          else {
            dir = PVector.sub(mLoc, itf.mLoc);
          }
          dir.normalize();
          dir.mult(force/distance);
          mLoc.add(dir);
        }
      }
    
      void display() {
        stroke(20, 20);
        point(mLoc.x, mLoc.y);
      }
    }
    class ParticleController{
      
      Interference [] interferences;
      Particle [] particles;
      
      ParticleController(){
      }
      
      void pAddCircle(int amt, float rRange){
        particles = new Particle[amt];
        for(int i=0;i<amt;i++){
          float r = rRange;
          float randDegree = random(360);
          float x = cos(radians(randDegree))*r+width/2;
          float y = sin(radians(randDegree))*r+height/2;
          particles[i] = new Particle(x,y);
        }
      }
      
      void iAddCircle(int amt, float rRange){
        interferences = new Interference[amt];
        for(int i=0;i<amt;i++){
          float r = rRange;
          float randDegree = random(360);
          float x = cos(radians(randDegree))*r+width/2;
          float y = sin(radians(randDegree))*r+height/2;
          interferences[i] = new Interference(x,y);
        }
      }
      
      void update(float force){
        for(int i=0;i<interferences.length;i++){
          for(int j=0;j<particles.length;j++){
            particles[j].update(interferences[i],force);
          }
        }
      }
      
      void display(){
        for(int i=0;i<particles.length;i++){
          particles[i].display();
        }
      }
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running in HTML5 using Processingjs.

    license

    advertisement

    Raven Kwok plus+
    ale plus+
    8 Feb 2013
    Quite nice and really nice screenshots. : )
    Regards!
    Raven Kwok plus+
    8 Feb 2013
    Thanks Ale! I'm using Processing 2.0b7, which is only allowed to upload as js package. So I have to reduce the particles for its web performance.
    insu maurya
    14 Feb 2013
    gud 1..........:)
    guillermo garcia
    14 Mar 2013
    hi, Raven!
    i really like the bunny one but there´s some problems for me (cos i´m not a programmer) could you please rewrite that for processing or is not that problem? it´s allways showing me an error screen and only happens with that file

    cheers
    123456
    25 Oct 2013
    it looks nice!
    Raven Kwok plus+
    26 Oct 2013
    Thx.
    You need to login/register to comment.