Gifs made up of Particles + Flock Behavior

edited September 2015 in Using Processing

Hello! I've been creeping around the forums for about a month trying to see if I could answer this on my own... but I can't.

I want to create a gif that is made up of tiny particles. Like these wizards did: http://www.iamnop.com/particles/ ^:)^ (I know this isn't processing, but you get the idea)

But then I want each gif to interact with each other using Flocking behaviors, including having the mouse be a Predator.

Is this ridiculous?

I have been able to do bits and pieces of what I want... but I can't for the life of me figure out how to bring it together. Creating gifs from particles seems impossible. :(

Any advice would be so greatly appreciated.

Thank you! Allison

Tagged:

Answers

  • Well, what code do you have so far? Have you got a Particle class?

  • I came across this sketch that uses particles to create a still image... and I have been trying to modify it:

    ` /* OpenProcessing Tweak of @http://www.openprocessing.org/sketch/6147@ / / !do not delete the line above, required for linking your tweak if you upload again / / SYSTEM 1 by krqluld / 18th november 2009

    */

        ArrayList particles;
        ArrayList forces;
        
        PImage buffer;
        PImage loadedImg;
        
        float G = 1;
        
        boolean clear = false;
        boolean renderForces = true;
        
        Force mouseForce; //special Force for the mouse
        boolean mouseAttract = false;
        
        void setup(){
          size(680, 720);
          frameRate(999);
          background(0);
          
          particles = new ArrayList();
          forces = new ArrayList();
          
          buffer = createImage(width, height, RGB);
          
          loadedImg = loadImage("swallows.png");
          loadedImg.resize(900, 0);
          loadedImg.loadPixels();
          for(int y = 0; y < loadedImg.height; y+= 3){
            for(int x = 0; x < loadedImg.width; x+= 3){
              color c = loadedImg.pixels[y*loadedImg.width+x];
              Particle p = new Particle(x+(width/2)-loadedImg.width/2, y+(height/2)-loadedImg.height/2, c);
              p.place((c >> 16 & 0xFF)/255.0*500, (c & 0xFF)/255.0*500);
              particles.add(p);
            }
          }
          
          //initiating forces
          mouseForce = new Force(new PVector(0, 0), 300, false, false);
        }
        
        void draw(){
          background(0);
          
          buffer.loadPixels();
          if(clear){
            color black = color(0);
            for(int i = 0; i < buffer.pixels.length; i++){
              buffer.pixels[i] = 0;
            }
          }
          for(int i = particles.size()-1; i>=0; i--){
            Particle p = (Particle) particles.get(i);
            p.run();
          }
          buffer.updatePixels();
          image(buffer, 0, 0);  
          
          if(renderForces){
            for(int i = forces.size()-1; i>=0; i--){
              Force f = (Force) forces.get(i);
              f.render();
            }
          }
        }
        
        void mousePressed(){
          if(keyPressed){
            if(key == BACKSPACE){
              PVector mousePos = new PVector(mouseX, mouseY);
              for(int i = forces.size()-1; i>=0; i--){
                Force f = (Force) forces.get(i);
                if(circleIntercept(mousePos, f.pos, 5)){
                  forces.remove(i);
                  break;
                }
              }
            } else if(key == CODED){
              if(keyCode == CONTROL){ 
                PVector mousePos = new PVector(mouseX, mouseY);
                for(int i = forces.size()-1; i>=0; i--){
                  Force f = (Force) forces.get(i);
                  if(circleIntercept(mousePos, f.pos, 5)){
                    f.forceOn = !f.forceOn;
                    break;
                  }
                }
              }
            }    
          } else {
            mouseForce.pos.set(mouseX, mouseY, 0);
            mouseForce.forceOn = true;
          }    
        }
        
        void mouseDragged(){
          mouseForce.pos.set(mouseX, mouseY, 0);
          mouseForce.forceOn = true;
        }
        
        void mouseReleased(){
          mouseForce.forceOn = false;
        }
        
        void addForce(PVector pos, float mass, boolean attract, boolean on){
          forces.add(new Force(pos, mass, attract, on));
        }
        
        boolean circleIntercept(PVector pos, PVector circlePos, float radius){
          float dis = dist(pos.x, pos.y, circlePos.x, circlePos.y);
          if(dis 
        class Force {
          PVector pos;
          float mass, multiplier;
          boolean forceOn;
         
         //amount of force used to displace particles 
          Force(PVector p, float ms, boolean attract, boolean on){
            pos = p;
            mass = ms;
            if(attract){
              multiplier = 5.0;  
            } else {
              multiplier = -5.0;
            }
            forceOn = on;
          }
          
          PVector calculateForce(PVector pPos, float pMass){
            PVector f = PVector.sub(pos, pPos);
            float d = f.mag();
            d = constrain(d, 20.0, 5000.0);
            f.normalize();
            float force = (G * mass * pMass) / (d * d);
            f.mult(force * multiplier);
            return f;
          }
        
          void setAttract(boolean attract){
            if(attract){
              multiplier = 1.0;
            } else {
              multiplier = -1.0;
            }
          }
          
          void render(){
            if(forceOn){
              stroke(255);      
              noFill();
              ellipse(pos.x, pos.y, mass/2, mass/2);
            }
        
            if(forceOn){    
              stroke(255);
              fill(200);
            } else {
              stroke(200);
              fill(255, 0, 0);
            }
            ellipse(pos.x, pos.y, 10, 10);
          }
            
        }
    
    
              class Particle {
              PVector originalPos;
              PVector currentPos;
              PVector velocity;
              PVector acceleration;
              
              float mass;
              
              color particleColor;
              
              Particle(float xPos, float yPos, color c){
                originalPos = new PVector(xPos, yPos);
                currentPos = new PVector(xPos, yPos);
                velocity = new PVector(0.0, 0.0);
                acceleration = new PVector(0.0, 0.0);
                
                mass = 1;
                
                particleColor = c;
              }
              
              void place(float xPos, float yPos){
                currentPos = new PVector(xPos, yPos);
              }    
              
              void move(){
                acceleration = PVector.sub(originalPos, currentPos);
                acceleration.mult(0.001);
                
                if(mouseForce.forceOn){
                  acceleration.add(mouseForce.calculateForce(currentPos, mass));
                }
                
                for(int i = forces.size()-1; i>=0; i--){
                  Force f = (Force) forces.get(i);
                  if(f.forceOn)
                    acceleration.add(f.calculateForce(currentPos, mass));
                }   
                velocity.add(acceleration);
                velocity.mult(0.99);
                currentPos.add(velocity);
                if(currentPos.x > width-1){
                  currentPos.x = width-1;
                  velocity.x *= -1;
                } else if(currentPos.x < 0){
                  currentPos.x = 0;
                  velocity.x *= -1;
                }
                if(currentPos.y > height-1){
                  currentPos.y = height-1;
                  velocity.y *= -1;
                } else if(currentPos.y < 0){
                  currentPos.y = 0;
                  velocity.y *= -1;
                }      
              }
              
              void render(){
                int xPos = constrain(floor(currentPos.x), 0, width-1);
                int yPos = constrain(floor(currentPos.y), 0, height-1);
                buffer.pixels[(yPos*width)+xPos] = particleColor;
              }
              
              void run(){
                move();
                render();
              }
            }
    
Sign In or Register to comment.