• fullscreen
  • eco_2.pde
  • eco_beta.pde
  • eco_world.pde
  • //Alpha stuff ignore
    
    /*Ecosystem with Boids and Music*/
    /*By Lee Cunnington*/
    
    
    //Establish Image
    PImage img;
    
    //Import the minim library
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    
    //Declare the minim audio library with BeatDetection
    Minim minim;
    AudioPlayer song;
    BeatDetect beat;
    
    //Sizing and positioning for the 'Heartbeat' circle
    float eRadius;
    float xpos = 400;
    float ypos = 330;
    
    
    //Size of the Living Objects
    float sizeH = 1;
    float sizeW = 1;
    
    
    //Distance, Alignment and Minimum Distance for Perception
    float perceptD = 500;
    float perceptA = PI;
    float perceptMin = 20;
    
    //Alignment, Cohesion and Separation 
    //The higher the values, the more the steering behaviour is used
    //Causes the Flocking
    float alignWeight = 0.001;
    float cohesionWeight = 0.09;
    float separateWeight = 0.0001;
    float r = 2.0;
    
    //Remembered in case I want to play around with other behaviour figures
    //alignWeight 0.001
    //cohesion 0.09
    //separate 0.001
    
      
    //Create the class called Life
    class Life {
      
      //Establish the PVectors (Various Behaviours)
     PVector location;
     PVector velocity;
     PVector accelerate;
    
     //Declaring the various behavious 
     Life() {
      location = new PVector(random(width), random(height));
      velocity = new PVector(random(-0.5,0.5), random(-0.1,0.1));
      accelerate = new PVector();
    
     } 
    
    
      void render(){  
     
        //If the aLife objects hit the edges of the sketch, 
      //this code provides the command to return to the sketch
      
      if (location.x < - r)  {
        location.x = width + r;
        //sizeH += 0.004;
        fill(255,255, 0);
      }
      if (location.y < - r)  {
        location.y = height + r;
        //sizeW -= 0.003;
        fill(255, 0, 0);
    
      }
      if (location.x > width + r) {
       location.x = location.x - r;
       //sizeH -= 0.003; 
       fill(0, 255, 0);  
    }
      
      if (location.y > height + r) {
       location.y = location.y - r; 
        //sizeW += 0.002;
       fill(0, 0, 255);
        
      }
      
    //Colour Triggers - When the boids come into contact with these locations of the sketch, each boid will change colour.
      
     if (location.x <= 300) {
       fill(255, 255, 255); 
     }
     
     if (location.x >= 300) {
       fill(255, 255, 255);
     } 
     
     if (location.y >=650) {
       fill(255, 255, 255);
     }
     
     
     if (location.x >= 550) {
       fill(255, 255, 255);
     }
     
     if (location.x <= 250) {
       fill(255, 255, 255);
     }
     
     if (location.x <= 190) {
       fill(156, 149, 255); 
       
     }
     
     if (location.x >= 650){
       fill(156, 149, 255);
       
     }
     
     if (location.y <= 150) {
        fill(255, 0, 0); 
     }
     
      if (location.y >= 450) {
       fill(255, 0, 0); 
     }
     
    
      if ((location.x < 0) || (location.x > width)) {
              velocity.x = velocity.x * - 0.1;
              
       }
       
       if ((location.y < 0) || (location.y > height)) {
              velocity.y = velocity.y * - 0.1;
              
         
       }
       
       //Introduce push and pop Matrix with the boids ellipse shape
       
       pushMatrix();
       translate(location.x, location.y);  
       noStroke();
       ellipse(0, 0, sizeH, sizeW);
       popMatrix();
    
      }
      
    
      //Get the Neighbours from to ArrayList from class Life
      ArrayList getNeighbours(ArrayList Life) {
        
        ArrayList Neighbours = new ArrayList();
        PVector locationDifference = new PVector();
      
      for(int i=0; i < objects.size(); i++) {
        Life L = (Life)objects.get(i);
        
        if(L == this) continue;
        
        locationDifference.set(L.location);
        locationDifference.sub(location);
        
        //So basically if locationDifference is higher than the perception distance,
        //the boids continue their actions
        if(locationDifference.mag() >= perceptD) continue;
        
        //If the Pvector angle between velocity and locationDifference is than perceptA, the boids
        //continue with their actions.
        if(PVector.angleBetween(velocity, locationDifference) > perceptA) continue;
     
        Neighbours.add(L);
        }
      
      return Neighbours;
      }  
      
      //Update Neighbours
      void update(ArrayList Neighbours) {
       PVector locationDifference = new PVector();
       PVector velocityDifference = new PVector();
      
      
      //Establish the 3 Steering Behaviours
      PVector align = new PVector();
      PVector cohesion = new PVector();
      PVector separate = new PVector();
      
      for(int i=1; i < Neighbours.size(); i++) {
      
        Life L = (Life)Neighbours.get(i);
        
        locationDifference.set(L.location);
        locationDifference.sub(location);
        
        velocityDifference.set(L.velocity);
        velocityDifference.sub(velocity);
        
        align.add(velocityDifference);
        cohesion.add(locationDifference);
        
        if(locationDifference.mag() < perceptMin) separate.add(locationDifference);
      }
      
      if (align.mag() > 0) align.normalize();
      if (cohesion.mag() > 0) cohesion.normalize();
      if (separate.mag() > 0) separate.normalize();
      
      //Multiply behaviours with the floated declarations   
      align.mult(alignWeight);
      cohesion.mult(cohesionWeight);
      separate.mult(separateWeight);
      
      accelerate.set(align);
      accelerate.add(cohesion);
      accelerate.sub(separate);
    
      velocity.add(accelerate);
      location.add(velocity);
      
        }
        
    }
    
    
    ArrayList objects;
    
    void setup() {
      
      size(800, 600);
      
      img = loadImage("tot.png");
      
      //This is for the 'heartbeat' Using 
      minim = new Minim(this);
      song = minim.loadFile("3.mp3", 2048);
      song.play();
      beat = new BeatDetect();
      
      ellipseMode(CENTER_RADIUS);
      eRadius = 150;
      
      
      //Declare objects as ArrayListing and carry out for loop to setup the boids on the sketch
      objects = new ArrayList();
      
      for(int i=0; i < 240; i++) {
       Life L = new Life();
       objects.add(L);
        } 
      
      }
    
    void draw() {
      background(0);
      smooth();
      
    
      beat.detect(song.mix);
      float a = map(eRadius, 100, 100, 100, 100);
      
    
      if ( beat.isOnset() ) {
        eRadius = 180; 
       fill(255, 255, 255);
      }
      
      ellipse(xpos, ypos, eRadius, eRadius);
      eRadius *= 0.98;
      
      if ( eRadius < 109 ) {   
        eRadius = 150;
      }
    
      //For loop designed to draw the boids onto the sketch.
      for (int i=0; i < objects.size(); i++) {
       Life L = (Life)objects.get(i);
       L.render();   
       L.update(L.getNeighbours(objects));
       stroke(255);
      
      }
      
      image(img,250,180);
      
      //eco.update();
    }
    
    void stop()
    {
      minim.stop();
      super.stop();
    }
    
    //Alpha Stuff Ignore
    
    //class world {
    // 
    // color c;
    // float xpos = 0;
    // float ypos = 0; 
    // 
    // 
    // world(color tempC, float x, float y){
    //  c = tempC;
    //  xpos = x;
    //  ypos = y;
    //   
    // } 
    // 
    // void update(){
    //   fill(c);
    //   ellipse(xpos, ypos, 200, 200);
    //   
    // 
    // 
    // }
    //  
    //  
    //}
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Lee Cunnington

    Plastic World

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

    Update - Slower Laptops may not handle the Boids movement

    A University Project. Plastic World is based around our Planet which has a heartbeat. The 'heartbeat' reacts to the music (thanks to minim library). I have designed the boids to co-exist with the Planet providing interaction with colours. When the boids reach an area of the sketch, their colour changes; the more boids in that area, the higher the chance the Planet will interact with them, changing colour as well.

    Known Issue: The sound goes crazy for 2 seconds or so.

    Song: Pendulum - Plastic World
    Inspiration: Daniel Schiffman

    You need to login/register to comment.