• fullscreen
  • Particle.pde
  • oct22a_2011.pde
  • sketch_oct22a_2011.pde
  • class Particle {
      float xCurr, yCurr;
      float xInit, yInit;
      float xo,yo;
      float pushForce;
      float recoverForce;
      Particle(float xo, float yo) {
        this.xo = xo;
        this.yo = yo;
        
        float degreeTemp = random(360);
        float rTemp = random(10, 180);
        xInit = cos(radians(degreeTemp))*rTemp+xo;
        yInit = sin(radians(degreeTemp))*rTemp+yo;
        xCurr = xInit;
        yCurr = yInit;
        pushForce = random(10,300);
        recoverForce = random(10,100);
      }
      void update() {
        float x0 = xCurr;
        float y0 = yCurr;
        float a = mouseX-x0;
        float b = mouseY-y0;
        float r = pushForce/(a*a+b*b);
        float quer_fugir_x = xCurr-a*r;
        float quer_fugir_y = yCurr-b*r;
        float quer_voltar_x = (xInit-x0)/recoverForce;
        float quer_voltar_y = (yInit-y0)/recoverForce;
        xCurr = quer_fugir_x+quer_voltar_x;
        yCurr = quer_fugir_y+quer_voltar_y;
      }
      void display() {
        pushMatrix();
        translate(xCurr,yCurr);
        rotate(radians(360*noise(xCurr*0.01,yCurr*0.01)));
        float diam = (pushForce/recoverForce)/dist(xCurr,yCurr,mouseX,mouseY)*100;
        strokeWeight(1);
        stroke(0,100);
        fill(255);
        rect(0, 0,diam,diam);
        popMatrix();
      }
      void reset() {
        float degreeTemp = random(360);
        float rTemp = random(10, 180);
        pushForce = random(10,300);
        recoverForce = random(10,100);
        xInit = cos(radians(degreeTemp))*rTemp+xo;
        yInit = sin(radians(degreeTemp))*rTemp+yo;
      }
    }
    
    
    //Raven Kwok (aka Guo Ruiwen)
    //oct22a_2011
    /*
     
    raystain@gmail.com
    twitter.com/ravenkwok
    flickr.com/ravenkwok
    weibo.com/ravenkwok
    the-moor.blogbus.com
     
    note: Update on Sep.03, 2012. 
    _1. Some modification on the motion of each particle, thanks to Ale( http://www.openprocessing.org/portal/?userID=12899 )'s advice :)
    _2. Each particle now has it own "tensile force". This "force" is indicated by size of the block.
    */
    
    import megamu.mesh.*;
    
    int count;
    float xOffset, yOffset;
    float [][] pos;
    Particle [] particles;
    Delaunay delaunay;
    
    void setup() {
      size(600, 600, P2D);
      smooth();
      rectMode(CENTER);
      count = 1000;
      xOffset = width/2;
      yOffset = height/2;
      particles = new Particle[count];
      for (int i=0;i<count;i++) {
        particles[i] = new Particle(xOffset, yOffset);
      }
    }
    void draw() {
      background(255);
      for (int i=0;i<count;i++) {
        particles[i].update();
      }
      pos = new float[count][2];
      for ( int j=0; j<pos.length;j++) {
        pos[j][0] = particles[j].xCurr;
        pos[j][1] = particles[j].yCurr;
      }
      delaunay = new Delaunay(pos);
      float[][] edges = delaunay.getEdges();
      for (int i=0; i<edges.length; i++)
      {
        float startX = edges[i][0];
        float startY = edges[i][1];
        float endX = edges[i][2];
        float endY = edges[i][3];
        float distance = dist(startX, startY, endX, endY);
        float trans = 255-map(distance,0,60,0,255);
        float sw = 2/sqrt(distance+1);
        strokeWeight(sw);
        stroke(0, trans);
        line(startX, startY, endX, endY);
      }
      
      for (int i=0;i<count;i++) {
        particles[i].display();
      }
    }
    
    void keyPressed() {
      if (key =='r') {
        for (int i=0;i<count;i++) {
          particles[i].reset();
        }
      }
    }
    
    //Raven Kwok | 郭锐文
    //Email: raystain@gmail.com
    //Blog: the-moor.blogbus.com
    //Vimeo: vimeo.com/ravenkwok
    //Weibo: weibo.com/ravenkwok
     
    import megamu.mesh.*;
    
    int total;
    float xOffset, yOffset;
    float [][] pos;
    Particle [] particles;
    Delaunay delaunay;
    
    void setup() {
      size(500, 500);
      smooth();
      background(255);
      total = 300;
      xOffset = width/2;
      yOffset = height/2;
      particles = new Particle[total];
      for (int i=0;i<total;i++) {
        particles[i] = new Particle(xOffset, yOffset);
      }
    }
    void draw() {
      background(255);
      for (int i=0;i<total;i++) {
        particles[i].update();
        particles[i].display();
      }
      pos = new float[total][2];
      for ( int j=0; j<pos.length;j++) {
        pos[j][0] = particles[j].xCurr;
        pos[j][1] = particles[j].yCurr;
      }
      delaunay = new Delaunay(pos);
      float[][] edges = delaunay.getEdges();
      for (int i=0; i<edges.length; i++)
      {
        float startX = edges[i][0];
        float startY = edges[i][1];
        float endX = edges[i][2];
        float endY = edges[i][3];
        float trans = 255-dist(startX, startY, endX, endY)*4;
        float sw = 5/(dist(startX, startY, endX, endY)+1);
        strokeWeight(sw);
        stroke(0, trans);
        line(startX, startY, endX, endY);
      }
    }
    
    void keyPressed() {
      if (key =='r') {
        for (int i=0;i<total;i++) {
          particles[i].reset();
        }
      }
    }
    
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Raven Kwok plus+

    oct22a_2011

    Add to Faves Me Likey@! 92
    You must login/register to add this sketch to your favorites.

    oct22a_2011 is small practice. The particles, which form this grid, can dodge your mouse track, while the grid is generated by Lee Byron's Mesh Lib. You may press "r" to reset the position of each point. The algorithm for the movement of each particle is the same as my former "Colloid Text". Hope you like it : )

    ale plus+
    25 Oct 2011
    It's as nice as the one with type (really nice then!)... ^-^

    I realized you can optimize the code a bit, maybe that's useful for a larger set of points (maybe irrelevant... ^-^):
    float r = sqrt(a*a+b*b);
    float quer_fugir_x = xCurr-(a/r)*100/r;
    float quer_fugir_y = yCurr-(b/r)*100/r;
    to:
    float r= 100/(a*a+b*b);
    float quer_fugir_x = xCurr-a*r;
    float quer_fugir_y = yCurr-b*r;

    Regards!
    ^-^
    Raven Kwok plus+
    31 Oct 2011
    :) Thanks for your advice.
    Wow! Good! Thaks!
    HanByul
    23 Mar 2012
    Great!
    Great dynamics Raven, as Ale have mentioned.. square root is quite expensive to compute.. I have tried your implementation and there is wonderful hack from ID software (and Silicon Graphics) to do really fast sqrt..

    http://en.wikipedia.org/wiki/Fast_inverse_square_root

    It is bit *punk code*, but is is working suprisingly well.

    Thanks for sharing!
    ale plus+
    26 Mar 2012
    Really useful hack!
    Pd.: i = 0x5f3759df - (i >> 1)... And lovely punk code, indeed! :-D
    uncertainT
    26 Mar 2012
    cool!
    Raven Kwok plus+
    10 Apr 2012
    Hi Kof, great help indeed. Thanks a lot, I'll update my code later XD
    Rafael Rinaldi
    9 May 2012
    Loved this.
    redregar
    15 May 2012
    It Makes alot fun on touch :). Would be cool as menu and information blubbling up
    You need to login/register to comment.