• fullscreen
  • ParticleEngine.pde
  • Spring.pde
  • networks2.pde
  • particle.pde
  • public class ParticleEngine {
      // alist to keep track of particles
      private ArrayList particles;
      private ArrayList springs;
      
      //the class constructor
      public ParticleEngine() {
        //create the list
        particles = new ArrayList();
        springs = new ArrayList();
      }
      
      public void addParticle(Particle particle) {
        //public function to add a particle
        particle.setEngine(this);
        particles.add(particle);
      }
      
      public void addSpring(Spring spring) {
        //public function to add a particle
        spring.setEngine(this);
        springs.add(spring);
      }
      
      //update function to update all springs and particles
      public void update() {
        //use a while loop to check all springs
        int i = 0;
        while (i<springs.size()) {
          //update it
          Spring s = (Spring) springs.get(i);
          s.update();
          if (!s.destroyed) {
            //if the particle is still alive, update it
            i++;
          } else {
            //if the particle is not remove it from the list
            springs.remove(i);
          }
        }
        
        i = 0;
        while (i<particles.size()) {
          //update it
          Particle p = (Particle) particles.get(i);
          p.update();
          if (!p.destroyed) {
            //if the particle is still alive, update it
            i++;
          } else {
            //if the particle is not remove it from the list
            particles.remove(i);
          }
        }    
      }
      
      //draw all particles
      public void draw() {
        for (int i=0; i<springs.size(); i++) {
          Spring s = (Spring) springs.get(i);
          s.draw();
        }
        for (int i=0; i<particles.size(); i++) {
          Particle p = (Particle) particles.get(i);
          p.draw();
        }
      }
      
      public Particle particleAt(float x, float y) {
        int i = particles.size();
        while(i>0) {
          i--;
          Particle p = (Particle) particles.get(i);
          float dx = p.x - x;
          float dy = p.y - y;
          if (dx * dx + dy * dy < p.size * p.size) {
            return p;
          }
        }
        return null;
      }
      
      public Particle findParticle(String label) {
        int i = particles.size();
        while(i>0) {
          i--;
          Particle p = (Particle) particles.get(i);
          if (p.label == label) {
            return p;
          }
        }
        return null;
      }
      
      public void connectParticles(String label1, String label2) {
        Particle particle1 = findParticle(label1);
        Particle particle2 = findParticle(label2);
        if (particle1 != null && particle2 != null) {
          addSpring(new Spring(particle1, particle2, 60, 0.02));
        }
      } 
      
      
    }
    
    public class Spring {
      // a reference to the engine
      private ParticleEngine engine;
      
      //the two particles the spring is connected too
      public Particle start;
      public Particle end;
      
      //spring length and constant
      public float springLength;
      public float springConstant;
       // flag to indicate whether or not the spring is destroyed
      public boolean destroyed;
      
      public Spring(Particle start, Particle end, float springLength, float springConstant) {
        this.start = start;
        this.end = end;
        this.springLength = springLength;
        this.springConstant = springConstant;
      }
      
      public void setEngine(ParticleEngine engine) {
        this.engine = engine;
      }
      
      public void update() {
        //use pythagoras to calculate distance between the particles
        float dx = end.x - start.x;
        float dy = end.y - start.y;
        float d = sqrt(dx * dx + dy * dy);
        //normalize the distance vector
        if (d>0){
          dx /= d;
          dy /= d;
        }
        //apply spring length and constant
        d -= springLength;
        d *= springConstant;
        
        //apply force on the particles
        start.velocityX += d*dx;
        start.velocityY += d*dy;
        end.velocityX -= d*dx;
        end.velocityY -= d*dy;
      }
      
      public void draw() {
        noFill();
        stroke(#029834);
        //strokeWeight(2);
        line(start.x, start.y, end.x, end.y);
      }
    }
    
    //variable to hold one object
    ParticleEngine engine;
    Particle lastParticle;
    Particle dragging;
    
    
    PFont font;
    PImage mapImages2;
    
    void setup() {
      size(700, 400);
      mapImages2 = loadImage("voetbalveld_480.gif");
      //create a particle engine
      engine = new ParticleEngine();
      smooth();
      font = createFont("SansSerif", 11);
      textFont(font);
      
      engine.addParticle(new Particle("Stekelenburg", random(0, 10), random(0, 10), 0, 0));
      engine.addParticle(new Particle("Van der Wiel", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Heitinga", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Mathijsen", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Van Bronckhorst", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Van Bommel", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("De Jong", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Sneijder", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Robben", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Elia", random(0, 400), random(0, 400), 0, 0));
      engine.addParticle(new Particle("Van Persie", random(0, 400), random(0, 400), 0, 0));
      
      
      
      //engine.addParticle(new Particle("Heitinga", random(0, 400), random(0, 400), 0, 0));
      
    
      
      engine.findParticle("Stekelenburg").pin(320, 30);
      engine.findParticle("Van der Wiel").pin(120, 100);
      engine.findParticle("Van Bronckhorst").pin(550, 100);
      
      engine.findParticle("Robben").pin(120, 320);
      engine.findParticle("Elia").pin(550, 320);
    
       engine.connectParticles("Stekelenburg", "Heitinga");
       engine.connectParticles("Stekelenburg", "Van der Wiel");
       engine.connectParticles("Stekelenburg", "Mathijsen");
       engine.connectParticles("Stekelenburg", "Van Bronckhorst");
       
       engine.connectParticles("Heitinga", "Van der Wiel");
       engine.connectParticles("Heitinga", "Mathijsen");
       engine.connectParticles("Van Bronckhorst", "Mathijsen");
       engine.connectParticles("Van Bronckhorst", "De Jong");
       engine.connectParticles("De Jong", "Mathijsen");
       
        engine.connectParticles("De Jong", "Van Bommel");
        engine.connectParticles("Van der Wiel", "Van Bommel");
        engine.connectParticles("Heitinga", "Van Bommel");
        
        engine.connectParticles("Van Bommel", "Sneijder");
        engine.connectParticles("De Jong", "Sneijder");
    
        engine.connectParticles("Robben", "Van Bommel");  
        engine.connectParticles("Robben", "Sneijder");
        engine.connectParticles("Elia", "Sneijder");
        engine.connectParticles("Elia", "De Jong");
        
        engine.connectParticles("Elia", "Van Persie");
        engine.connectParticles("Robben", "Van Persie");
        engine.connectParticles("Van Persie", "Sneijder");
        engine.connectParticles("Van Bronckhorst", "Elia");
        engine.connectParticles("Robben", "Van der Wiel");
    }
    
    void draw() {
      image(mapImages2, 0, 0, 700, 400);
      //update the particles
      engine.update();
      //clear the background
     
      //draw the particles
      engine.draw();
      fill(#000000);
      strokeWeight(2);
      
      fill(#FFFFFF);
      text("Opstelling voor vanavond :-) ", 10, 30);
      
    }
    
    void mousePressed() {
      dragging = engine.particleAt(mouseX, mouseY);
      if (dragging!=null) {
        dragging.dragged = true;
        lastParticle = dragging;
      }
    }
    
    void mouseReleased() {
      if (dragging!=null) {
        dragging.dragged=false;
        dragging = null;
      }
    }
      
    
    public class Particle {
      //a reference to the engine
      protected ParticleEngine engine;
      //a position
      public float x;
      public float y;
      // a velocity
      public float velocityX;
      public float velocityY;
      // a gravity
      protected float gravityX;
      protected float gravityY;
      // damping to simulate friction
      protected float damping;
      // color
      protected color col;
      //size
      protected float size;
      // variables to keep track of how long the particle is alive and how long it will live
      protected int life;
      protected int living;
      
      PImage mapImages;
      
      // flag to indicate whether or not the particle is destroyed
      public boolean destroyed;
      
      //flag to indicate the particle is being dragged
      public boolean dragged;
    
      public boolean pinned ;
      
      public String label; 
      
      
      
      //constructor, set initial variables
      public Particle(String label, float x, float y, float velocityX, float velocityY) {
        engine = null;
        this.label = label; 
        this.x = x;
        this.y = y;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
        gravityX = random(-0.5,0.5);
        gravityY = random(-0.5,0.5);
        damping = 0.9;
        //randomize the color
        col = color(255, 255, 255);
        //randomize the size
        size = 30;
        //set life to -1 to live forever
        life = -1;
        living = 0;
        
        //set flags
        destroyed = false;
        dragged = false;
        pinned = false;
        mapImages = loadImage("shirt.png");
      }
      
      public void setEngine(ParticleEngine engine) {
        this.engine = engine;
      }
      
      //a method to set the particles position and velocity
      public void setPos(float x, float y, float velocityX, float velocityY) {
        this.x = x;
        this.y = y;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
      }
      
      //a method to update the particle (should be called avery frame
      public void update() {
        if (dragged) {
          x = mouseX;
          y = mouseY;
          velocityX =0;
          velocityY =0;
          return;
        }
        if (pinned) {
          velocityX =0;
          velocityY =0;
          return;
        }
        
        //apply gravity
         velocityX += gravityX;
         velocityY += gravityY;
         //apply damping
         velocityX *= damping;
         velocityY *= damping;
         //apply speed
         x += velocityX;
         y += velocityY;
         if (life>0) {
           life--;
           if (life<=0) destroyed = true;
         }
      } 
      
      public void draw() {
       if (destroyed) return;
        //draw the particle
        //noStroke();
        // use alpha to fade away when life gets low
        fill(col);
        image(mapImages, x, y, size, size);
       
        fill(0);
        text(label, x-textWidth(label)*0.2, y+50);
      }
      
      public void pin(float x, float y) {
        this.x = x;
        this.y = y;
        pinned = true;
      }
    }
      
    
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Timo van Tilburg

    Nederlands Elftal

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

    Nederlands Elftal

    You need to login/register to comment.