• fullscreen
  • AI4G_Ex_3.pde
  • AI_Flocking.pde
  • CustomGUI.pde
  • boidpic.pde
  • gui.pde
  • import guicomponents.*;
    
    import game2dai.utils.*;
    import game2dai.steering.*;
    import game2dai.maths.*;
    import game2dai.fsm.*;
    import game2dai.entityshapes.*;
    import game2dai.entities.*;
    import game2dai.graph.*;
    import game2dai.*;
    
    final int NBR_BOIDS = 200;
    
    World w;
    Domain wd;
    StopWatch sw = new StopWatch();
    
    Vehicle boid;
    Vehicle[] boids;
    SB sb;
    SB[] sbarray;
    BoidPic view;
    float deltaTime;
    int backcol, boidcol;
    long count = 0;
    
    void setup() {
      size(800, 600);
      createGUI();
      customGUI();
    
      w = new World(width, height, 67, 0);
      wd = new Domain(0, 0, width - 200, height);
      w.setNoOverlap(true);
    
      view = new BoidPic(8, color(220, 220, 255));	
      setColors(1);
    
    
      sb = new SB();
      sb.enableBehaviours(SBF.WANDER | SBF.FLOCK);
      sb.setWanderDetails(6, 30, 100);
      sb.setFlockRadius(50);
      sb.setWeight(SBF.WANDER, 1);
      sb.setWeight(SBF.ALIGNMENT, 8);
      sb.setWeight(SBF.COHESION, 2.2);
      sb.setWeight(SBF.SEPARATION, 1.3);
      sb.setWeight(SBF.FLOCK, 16.0);
    
      boids = new Vehicle[NBR_BOIDS];
      sbarray = new SB[NBR_BOIDS];
      for (int i = 0; i < NBR_BOIDS; i++) {
        float dirX = (rnd(0, 1) < 0.5) ? -1 : 1;
        float dirY = (rnd(0, 1) < 0.5) ? -1 : 1;
        float x = dirX * rnd(5, 10);
        float y = dirY * rnd(5, 10);
        boid = new Vehicle(new Vector2D(), // position
        5, // collision radius
        new Vector2D(), // velocity
        60, // maximum speed
        new Vector2D(), // heading
        1, // mass
        1, // turning rate
        400 // max force
        ); 
        boid.setRenderer(view);
        boids[i] = boid;
        sbarray[i] = (SB) sb.clone();
        boid.setSB(sbarray[i]);
        boid.setWorldDomain(wd);
        w.addMover(boid);
      }
      resetBoids();
      frameRate(1000); // as fast as possible
      sw.reset();
    }
    
    
    void draw() {
      deltaTime = (float) sw.getElapsedTime();
    
      background(backcol);
      fill(0, 64, 0);
      noStroke();
    
      w.update(deltaTime);
      w.draw();
      fill(180, 180, 255);
      noStroke();
      rect(600, 0, 200, 600);
      if (++count % 100 == 0) {
        lblFPSvalue.setText(""+frameRate);
        lblCalcTimeValue.setText(""+ (float)w.worldUpdateTime + " s");
      }
    }
    
    void changeWeighting(int type, float value) {
      for (int i = 0; i < NBR_BOIDS; i++) 
        sbarray[i].setWeight(type, value);
    }
    
    void changeWander() {
      for (int i = 0; i < NBR_BOIDS; i++) 
        sbarray[i].setWanderDetails(wanderJitter, wanderRadius, wanderDist);
    }
    
    void changeNeighbourhood(float radius) {
      for (int i = 0; i < NBR_BOIDS; i++) 
        sbarray[i].setFlockRadius(radius);
    }
    
    void changeMaxSpeed(float radius) {
      for (int i = 0; i < NBR_BOIDS; i++) 
        boids[i].setMaxSpeed(radius);
    }
    
    void resetBoids() {
      for (int i = 0; i < NBR_BOIDS; i++) {
        w.removeMover(boids[i]);
        float x = (width - 200)/2 + random(-150, 150);
        float y = height/2 + random(-150, 150);
        float dirX = (rnd(0, 1) < 0.5) ? -1 : 1;
        float dirY = (rnd(0, 1) < 0.5) ? -1 : 1;
        float vx = dirX * rnd(5, 10);
        float vy = dirY * rnd(5, 10);
        boids[i].setPos(x, y);
        boids[i].setHeading(vx, vy);
        boids[i].setVelocity(vx, vy);
        w.addMover(boids[i]);
      }
    }
    
    float rnd(float low, float high) {
      return (float)Math.random()*(high - low) + low;
    }
    
    void setColors(int cols) {
      switch(cols) {
      case 2:
        backcol = color(32, 32, 0);
        boidcol = color(0, 255, 0); 
        break;
      case 3:
        backcol = color(200, 255, 200);
        boidcol = color(0); 
        break;
      case 4:
        backcol = color(255, 255, 200);
        boidcol = color(255, 0, 0); 
        break;
      default:
        backcol = color(0, 0, 64);
        boidcol = color(220, 220, 255);
      };
      view.setFill(boidcol);
    }
    
    
    /**
    Flocking explorer <br>
    
    Demonstrates flocking behaviour and allows the user to experiment
    with changing the behaviour weights.
    */
    
    import game2dai.entities.*;
    import game2dai.entityshapes.ps.*;
    import game2dai.maths.*;
    import game2dai.*;
    import game2dai.entityshapes.*;
    import game2dai.fsm.*;
    import game2dai.steering.*;
    import game2dai.utils.*;
    import game2dai.graph.*;
    
    import g4p_controls.*;
    
    float wanderDist = 100.0, wanderRadius = 30.0, wanderJitter = 6.0;
    float initWander = 1.0, initFlock = 16.0;
    float initAlign = 8.0, initCohesion = 2.2, initSeparation = 1.3;
    float initWanderJitter = 6.0, initWanderRadius = 30, initWanderDist = 100;
    float initMaxSpeed = 70.0, initNeighbourhood = 50.0;
    
    final int NBR_BOIDS = 200;
    
    World w;
    Domain wd;
    StopWatch sw = new StopWatch();
    
    Vehicle boid;
    Vehicle[] boids;
    SB sb;
    SB[] sbarray;
    BoidPic view;
    float deltaTime;
    int backcol, boidcol;
    long count = 0;
    
    public void setup() {
      size(620, 540);
      createGUI();
    
      w = new World(width, height);
      wd = new Domain(0, 0, 400, 440);
      w.noOverlap(true);
    
      view = new BoidPic(this, 8, color(220, 220, 255));	
      setColors(1);
    
    
    
      sb = new SB();
      sb.wanderOn().flockOn();
      sb.wanderFactors(initWanderDist, initWanderRadius, initWanderJitter);
      sb.flockFactors(50);
      sb.wanderWeight(initWander);
      sb.alignmentWeight(initAlign);
      sb.cohesionWeight(initCohesion);
      sb.separationWeight(initSeparation);
      sb.flockWeight(initFlock);
    
    
      boids = new Vehicle[NBR_BOIDS];
      sbarray = new SB[NBR_BOIDS];
      for (int i = 0; i < NBR_BOIDS; i++) {
        boid = new Vehicle(new Vector2D(), // position
        5, // collision radius
        new Vector2D(), // velocity
        initMaxSpeed, // maximum speed (was 60)
        new Vector2D(), // heading
        1, // mass
        1, // turning rate
        400 // max force
        ); 
        boid.renderer(view);
        boids[i] = boid;
        sbarray[i] = (SB) sb.clone();
        boid.SB(sbarray[i]);
        boid.worldDomain(wd);
        w.addMover(boid);
      }
      restartBoids();
      frameRate(1000);
      sw.reset();
    }
    
    
    public void draw() {
      deltaTime = (float) sw.getElapsedTime();
      background(backcol);
    
      w.update(deltaTime);
      w.draw();
    
      fill(201, 206, 255);
      noStroke();
      rect(0, 440, width, height-440);
    
      if (++count % 100 == 0) {
        lblFPSvalue.setText(""+frameRate);
        lblCalcTimeValue.setText(""+ (float)w.worldUpdateTime + " s");
      }
    }
    
    public void changeWander() {
      for (int i = 0; i < NBR_BOIDS; i++) 
        sbarray[i].wanderFactors(wanderDist, wanderRadius, wanderJitter);
    }
    
    public void changeNeighbourhood(float radius) {
      for (int i = 0; i < NBR_BOIDS; i++) 
        sbarray[i].flockFactors(radius);
    }
    
    public void changeMaxSpeed(float radius) {
      for (int i = 0; i < NBR_BOIDS; i++) 
        boids[i].maxSpeed(radius);
    }
    
    public void restartBoids() {
      for (int i = 0; i < NBR_BOIDS; i++) {
        w.removeMover(boids[i]);
        float x = (width - 200)/2 + random(-150, 150);
        float y = height/2 + random(-150, 150);
        float dirX = (rnd(0, 1) < 0.5) ? -1 : 1;
        float dirY = (rnd(0, 1) < 0.5) ? -1 : 1;
        float vx = dirX * rnd(5, 10);
        float vy = dirY * rnd(5, 10);
        boids[i].moveTo(x, y);
        boids[i].heading(vx, vy);
        boids[i].velocity(vx, vy);
        w.addMover(boids[i]);
      }
    }
    
    public float rnd(float low, float high) {
      return (float)Math.random()*(high - low) + low;
    }
    
    public void setColors(int cols) {
      switch(cols) {
      case 2:
        backcol = color(32, 32, 0);
        boidcol = color(0, 255, 0); 
        break;
      case 3:
        backcol = color(200, 255, 200);
        boidcol = color(0); 
        break;
      case 4:
        backcol = color(255, 255, 200);
        boidcol = color(255, 0, 0); 
        break;
      default:
        backcol = color(0, 0, 64);
        boidcol = color(220, 220, 255);
      };
      view.setFill(boidcol);
    }
    
    float wanderDist, wanderRadius, wanderJitter;
    float initWander, initFlock, initAlign, initCohesion, initSeparation;
    float initWanderJitter, initWanderRadius, initWanderDist;
    float initMaxSpeed, initFlockSize;
    
    void customGUI(){
      lblTitle1.setTextAlign(GAlign.CENTER);
      lblTitle2.setTextAlign(GAlign.CENTER);
      lblTitle3.setTextAlign(GAlign.CENTER);
      lblTitle4.setTextAlign(GAlign.CENTER);
      
      initWander = sdrWander.getValue() / 100.0;
      lblWanderValue.setText("" + initWander);
      lblWanderValue.setTextAlign(GAlign.RIGHT);
      
      initFlock = sdrFlocking.getValue()/ 100.0;
      lblFlockingValue.setText("" + initFlock);
      lblFlockingValue.setTextAlign(GAlign.RIGHT);
      
      initAlign = sdrAlign.getValue()/100.0;
      lblAlignValue.setText("" + initAlign);
      lblAlignValue.setTextAlign(GAlign.RIGHT);
      
      initCohesion = sdrCohesion.getValue()/ 100.0;
      lblCohesionValue.setText("" + initCohesion);
      lblCohesionValue.setTextAlign(GAlign.RIGHT);
      
      initSeparation =  sdrSeparation.getValue()/100.0;
      lblSeparationValue.setText("" + initSeparation);
      lblSeparationValue.setTextAlign(GAlign.RIGHT);
      
      initFlockSize = sdrFlockSize.getValue()/100.0;
      lblFlockSizeValue.setText("" + initFlockSize);
      lblFlockSizeValue.setTextAlign(GAlign.RIGHT);
      
      initWanderJitter = wanderJitter = sdrJitter.getValue()/100.0;
      lblJitterValue.setText("" + wanderJitter);
      lblJitterValue.setTextAlign(GAlign.RIGHT);
      
      initWanderRadius = wanderRadius = sdrWanderRadius.getValue()/100.0;
      lblWanderRadiusValue.setText("" + wanderRadius);
      lblWanderRadiusValue.setTextAlign(GAlign.RIGHT);
      
      initWanderDist = wanderDist = sdrWanderDist.getValue()/100.0;
      lblWanderDistValue.setText("" + wanderDist);
      lblWanderDistValue.setTextAlign(GAlign.RIGHT);
      
      initMaxSpeed = sdrMaxSpeed.getValue()/100.0;
      lblMaxSpeedValue.setText("" + initMaxSpeed);
      lblMaxSpeedValue.setTextAlign(GAlign.RIGHT);
    }
    
    
    /**
     Demonstrates how to create a your own entity renderer for
     your own applications. <br>
     
     The class must extend the Picture class and the constructor
     must have the PApplet as a parameter so the first statement 
     can be a call to the super constructor, passing the PApplet 
     object.
     */
    class BoidPic extends PicturePS {
      float arrowLength = 20;
      float[] x = new float[] {
        0.5f * arrowLength, -0.4f * arrowLength, -0.4f * arrowLength
      };
      float[] y = new float[] {
        0, 0.3f * arrowLength, -0.3f * arrowLength
      };
    
      int fillCol;
    
      BoidPic(PApplet app) {
        super(app);
      }
    
      BoidPic(PApplet app, float length, int fill) {
        super(app);
        fillCol = fill;
        arrowLength = length;
        x = new float[] {
          0.5f * arrowLength, -0.4f * arrowLength, -0.4f * arrowLength
        };
        y = new float[] {
          0, 0.3f * arrowLength, -0.3f * arrowLength
        };
      }
    
      public void draw( BaseEntity owner, 
      float posX, float posY, 
      float velX, float velY, 
      float headX, float headY) {
        // First calculate the angle the entity is facing
        float angle = PApplet.atan2(headY, headX);
    
        // Can remove the next 4 lines if you never want to show hints
        if (hints != 0) {
          Hints.hintFlags = hints;
          Hints.draw(app, owner, velX, velY, headX, headY);
        }
    
        pushMatrix();
        translate(posX, posY);
        rotate(angle);
    
        // Draw arrow head
        fill(fillCol);
        beginShape(PApplet.TRIANGLES);
        vertex(x[0], y[0]);
        vertex(x[1], y[1]);
        vertex(x[2], y[2]);
        endShape(PApplet.CLOSE);
        popMatrix();
      }
    
      void setFill(int col) {
        fillCol = col;
      }
    }
    
    /* =========================================================
     * ====                   WARNING                        ===
     * =========================================================
     * The code in this tab has been generated from the GUI form
     * designer and care should be taken when editing this file.
     * Only add/edit code inside the event handlers i.e. only
     * use lines between the matching comment tags. e.g.
     
     void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
     } //_CODE_:button1:12356:
     
     * Do not rename this tab!
     * =========================================================
     */
    
    public void btnSteering_click(GImageButton source, GEvent event) { //_CODE_:btnSteering:692200:
      sdrWander.setValue(initWander);
      sdrFlocking.setValue(initFlock);
    } //_CODE_:btnSteering:692200:
    
    public void sdrWander_change(GSlider source, GEvent event) { //_CODE_:sdrWander:352006:
      lblWander.setText(source.getValueS());
      for (SB sb : sbarray) 
        sb.wanderWeight(source.getValueF());
    } //_CODE_:sdrWander:352006:
    
    public void sdrFlocking_change(GSlider source, GEvent event) { //_CODE_:sdrFlocking:544667:
      lblFlocking.setText(source.getValueS());
      for (SB sb : sbarray) 
        sb.flockWeight(source.getValueF());
    } //_CODE_:sdrFlocking:544667:
    
    public void btnFlocking_click(GImageButton source, GEvent event) { //_CODE_:btnFlocking:827562:
      sdrAlign.setValue(initAlign);
      sdrCohesion.setValue(initCohesion);
      sdrSeparation.setValue(initSeparation);
    } //_CODE_:btnFlocking:827562:
    
    public void sdrAlign_change(GSlider source, GEvent event) { //_CODE_:sdrAlign:866362:
      lblAlign.setText(source.getValueS());
      for (SB sb : sbarray) 
        sb.alignmentWeight(source.getValueF());
    } //_CODE_:sdrAlign:866362:
    
    public void sdrCohesion_change(GSlider source, GEvent event) { //_CODE_:sdrCohesion:698280:
      lblCohesion.setText(source.getValueS());
      for (SB sb : sbarray) 
        sb.cohesionWeight(source.getValueF());
    } //_CODE_:sdrCohesion:698280:
    
    public void sdrSeparation_change(GSlider source, GEvent event) { //_CODE_:sdrSeparation:228893:
      lblSeparation.setText(source.getValueS());
      for (SB sb : sbarray) 
        sb.separationWeight(source.getValueF());
    } //_CODE_:sdrSeparation:228893:
    
    public void btnWander_click(GImageButton source, GEvent event) { //_CODE_:btnWander:698063:
      sdrJitter.setValue(initWanderJitter);
      sdrRadius.setValue(initWanderRadius);
      sdrDistance.setValue(initWanderDist);
    } //_CODE_:btnWander:698063:
    
    public void btnBoid_click(GImageButton source, GEvent event) { //_CODE_:btnBoid:543089:
      sdrSpeed.setValue(initMaxSpeed);
      sdrNeighbourhood.setValue(initNeighbourhood);
    } //_CODE_:btnBoid:543089:
    
    public void sdrJitter_change(GSlider source, GEvent event) { //_CODE_:sdrJitter:766954:
      wanderJitter = source.getValueF();
      lblJitter.setText(source.getValueS());
      changeWander();
    } //_CODE_:sdrJitter:766954:
    
    public void sdrRadius_change(GSlider source, GEvent event) { //_CODE_:sdrRadius:629877:
      wanderRadius = source.getValueF();
      lblRadius.setText(source.getValueS());
      changeWander();
    } //_CODE_:sdrRadius:629877:
    
    public void sdrDistance_change(GSlider source, GEvent event) { //_CODE_:sdrDistance:934854:
      wanderDist = source.getValueF();
      lblDistance.setText(source.getValueS());
      changeWander();
    } //_CODE_:sdrDistance:934854:
    
    public void sdrSpeed_change(GSlider source, GEvent event) { //_CODE_:sdrSpeed:489772:
      lblSpeed.setText(source.getValueS());
      changeMaxSpeed(source.getValueF());
    } //_CODE_:sdrSpeed:489772:
    
    public void sdrNeighbourhood_change(GSlider source, GEvent event) { //_CODE_:sdrNeighbourhood:934178:
      lblNeighbourhood.setText(source.getValueS());
      changeNeighbourhood(source.getValueF());
    } //_CODE_:sdrNeighbourhood:934178:
    
    public void btnColour1_click(GImageButton source, GEvent event) { //_CODE_:btnColour1:327664:
      setColors(1);
    } //_CODE_:btnColour1:327664:
    
    public void btnColour2_click(GImageButton source, GEvent event) { //_CODE_:btnColour2:494670:
      setColors(2);
    } //_CODE_:btnColour2:494670:
    
    public void btnColour3_click(GImageButton source, GEvent event) { //_CODE_:btnColour3:303208:
      setColors(3);
    } //_CODE_:btnColour3:303208:
    
    public void btnColour4_click(GImageButton source, GEvent event) { //_CODE_:btnColour4:638495:
      setColors(4);
    } //_CODE_:btnColour4:638495:
    
    public void btnRestart_click(GImageButton source, GEvent event) { //_CODE_:btnRestart:448021:
      restartBoids();
    } //_CODE_:btnRestart:448021:
    
    
    
    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if (frame != null)
        frame.setTitle("Sketch Window");
      pnlTweaks = new GPanel(this, 400, 0, 220, 540, " Tweak Options      ");
      pnlTweaks.setCollapsible(false);
      pnlTweaks.setDraggable(false);
      pnlTweaks.setText(" Tweak Options      ");
      pnlTweaks.setTextBold();
      pnlTweaks.setOpaque(true);
      label1 = new GLabel(this, 10, 20, 180, 20);
      label1.setText("Steering Weights");
      label1.setTextBold();
      label1.setOpaque(false);
      btnSteering = new GImageButton(this, 190, 20, 20, 20, new String[] { 
        "reset.png", "reset.png", "reset.png"
      } 
      );
      btnSteering.addEventHandler(this, "btnSteering_click");
      label2 = new GLabel(this, 10, 40, 150, 20);
      label2.setText("Wander");
      label2.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label2.setOpaque(false);
      lblWander = new GLabel(this, 160, 40, 50, 20);
      lblWander.setText("1.0");
      lblWander.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblWander.setOpaque(false);
      sdrWander = new GSlider(this, 10, 60, 200, 20, 10.0);
      sdrWander.setLimits(1.0, 0.5, 3.0);
      sdrWander.setEasing(8.0);
      sdrWander.setNumberFormat(G4P.DECIMAL, 1);
      sdrWander.setOpaque(false);
      sdrWander.addEventHandler(this, "sdrWander_change");
      label4 = new GLabel(this, 10, 80, 150, 20);
      label4.setText("Flocking");
      label4.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label4.setOpaque(false);
      lblFlocking = new GLabel(this, 160, 80, 50, 20);
      lblFlocking.setText("16.0");
      lblFlocking.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblFlocking.setOpaque(false);
      sdrFlocking = new GSlider(this, 9, 100, 200, 20, 10.0);
      sdrFlocking.setLimits(16.0, 1.0, 30.0);
      sdrFlocking.setEasing(8.0);
      sdrFlocking.setNumberFormat(G4P.DECIMAL, 1);
      sdrFlocking.setOpaque(false);
      sdrFlocking.addEventHandler(this, "sdrFlocking_change");
      label6 = new GLabel(this, 10, 130, 180, 20);
      label6.setText("Flocking Weights");
      label6.setTextBold();
      label6.setOpaque(false);
      btnFlocking = new GImageButton(this, 190, 130, 20, 20, new String[] { 
        "reset.png", "reset.png", "reset.png"
      } 
      );
      btnFlocking.addEventHandler(this, "btnFlocking_click");
      label7 = new GLabel(this, 10, 150, 150, 20);
      label7.setText("Alignment");
      label7.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label7.setOpaque(false);
      lblAlign = new GLabel(this, 160, 150, 50, 20);
      lblAlign.setText("8.0");
      lblAlign.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblAlign.setOpaque(false);
      sdrAlign = new GSlider(this, 10, 170, 200, 20, 10.0);
      sdrAlign.setLimits(8.0, 1.0, 30.0);
      sdrAlign.setEasing(8.0);
      sdrAlign.setNumberFormat(G4P.DECIMAL, 1);
      sdrAlign.setOpaque(false);
      sdrAlign.addEventHandler(this, "sdrAlign_change");
      label9 = new GLabel(this, 10, 190, 150, 20);
      label9.setText("Cohesion");
      label9.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label9.setOpaque(false);
      lblCohesion = new GLabel(this, 160, 190, 50, 20);
      lblCohesion.setText("2.2");
      lblCohesion.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblCohesion.setOpaque(false);
      sdrCohesion = new GSlider(this, 10, 210, 200, 20, 10.0);
      sdrCohesion.setLimits(2.2, 0.5, 10.0);
      sdrCohesion.setEasing(8.0);
      sdrCohesion.setNumberFormat(G4P.DECIMAL, 1);
      sdrCohesion.setOpaque(false);
      sdrCohesion.addEventHandler(this, "sdrCohesion_change");
      label11 = new GLabel(this, 10, 230, 150, 20);
      label11.setText("Separation");
      label11.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label11.setOpaque(false);
      lblSeparation = new GLabel(this, 160, 230, 50, 20);
      lblSeparation.setText("1.3");
      lblSeparation.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblSeparation.setOpaque(false);
      sdrSeparation = new GSlider(this, 10, 250, 200, 20, 10.0);
      sdrSeparation.setLimits(1.3, 0.5, 10.0);
      sdrSeparation.setEasing(8.0);
      sdrSeparation.setNumberFormat(G4P.DECIMAL, 1);
      sdrSeparation.setOpaque(false);
      sdrSeparation.addEventHandler(this, "sdrSeparation_change");
      label13 = new GLabel(this, 10, 280, 180, 20);
      label13.setText("Wander Variables");
      label13.setTextBold();
      label13.setOpaque(false);
      label14 = new GLabel(this, 10, 300, 150, 20);
      label14.setText("Max. Jitter (rad/s)");
      label14.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label14.setOpaque(false);
      lblJitter = new GLabel(this, 160, 300, 50, 20);
      lblJitter.setText("6.0");
      lblJitter.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblJitter.setOpaque(false);
      btnWander = new GImageButton(this, 190, 280, 20, 20, new String[] { 
        "reset.png", "reset.png", "reset.png"
      } 
      );
      btnWander.addEventHandler(this, "btnWander_click");
      btnBoid = new GImageButton(this, 190, 430, 20, 20, new String[] { 
        "reset.png", "reset.png", "reset.png"
      } 
      );
      btnBoid.addEventHandler(this, "btnBoid_click");
      sdrJitter = new GSlider(this, 10, 320, 200, 20, 10.0);
      sdrJitter.setLimits(6.0, 1.0, 20.0);
      sdrJitter.setEasing(8.0);
      sdrJitter.setNumberFormat(G4P.DECIMAL, 1);
      sdrJitter.setOpaque(false);
      sdrJitter.addEventHandler(this, "sdrJitter_change");
      lable1 = new GLabel(this, 10, 340, 150, 20);
      lable1.setText("Radius");
      lable1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      lable1.setOpaque(false);
      lblRadius = new GLabel(this, 160, 340, 50, 20);
      lblRadius.setText("30.0");
      lblRadius.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblRadius.setOpaque(false);
      sdrRadius = new GSlider(this, 10, 360, 200, 20, 10.0);
      sdrRadius.setLimits(30.0, 10.0, 60.0);
      sdrRadius.setEasing(8.0);
      sdrRadius.setNumberFormat(G4P.DECIMAL, 1);
      sdrRadius.setOpaque(false);
      sdrRadius.addEventHandler(this, "sdrRadius_change");
      label18 = new GLabel(this, 10, 380, 150, 20);
      label18.setText("Distance");
      label18.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label18.setOpaque(false);
      lblDistance = new GLabel(this, 160, 380, 50, 20);
      lblDistance.setText("100.0");
      lblDistance.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblDistance.setOpaque(false);
      sdrDistance = new GSlider(this, 10, 400, 200, 20, 10.0);
      sdrDistance.setLimits(100.0, 20.0, 200.0);
      sdrDistance.setEasing(8.0);
      sdrDistance.setNumberFormat(G4P.DECIMAL, 1);
      sdrDistance.setOpaque(false);
      sdrDistance.addEventHandler(this, "sdrDistance_change");
      label20 = new GLabel(this, 10, 430, 180, 20);
      label20.setText("Boid Variables");
      label20.setTextBold();
      label20.setOpaque(false);
      label21 = new GLabel(this, 10, 450, 150, 20);
      label21.setText("Maximum speed");
      label21.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label21.setOpaque(false);
      lblSpeed = new GLabel(this, 160, 450, 50, 20);
      lblSpeed.setText("70.0");
      lblSpeed.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblSpeed.setOpaque(false);
      sdrSpeed = new GSlider(this, 10, 470, 200, 20, 10.0);
      sdrSpeed.setLimits(70.0, 20.0, 150.0);
      sdrSpeed.setEasing(8.0);
      sdrSpeed.setNumberFormat(G4P.DECIMAL, 1);
      sdrSpeed.setOpaque(false);
      sdrSpeed.addEventHandler(this, "sdrSpeed_change");
      label23 = new GLabel(this, 10, 490, 150, 20);
      label23.setText("Neighbourhood radius");
      label23.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      label23.setOpaque(false);
      lblNeighbourhood = new GLabel(this, 160, 490, 50, 20);
      lblNeighbourhood.setText("50.0");
      lblNeighbourhood.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblNeighbourhood.setOpaque(false);
      sdrNeighbourhood = new GSlider(this, 10, 510, 200, 20, 10.0);
      sdrNeighbourhood.setLimits(50.0, 30.0, 150.0);
      sdrNeighbourhood.setEasing(8.0);
      sdrNeighbourhood.setNumberFormat(G4P.DECIMAL, 1);
      sdrNeighbourhood.setOpaque(false);
      sdrNeighbourhood.addEventHandler(this, "sdrNeighbourhood_change");
      pnlTweaks.addControl(label1);
      pnlTweaks.addControl(btnSteering);
      pnlTweaks.addControl(label2);
      pnlTweaks.addControl(lblWander);
      pnlTweaks.addControl(sdrWander);
      pnlTweaks.addControl(label4);
      pnlTweaks.addControl(lblFlocking);
      pnlTweaks.addControl(sdrFlocking);
      pnlTweaks.addControl(label6);
      pnlTweaks.addControl(btnFlocking);
      pnlTweaks.addControl(label7);
      pnlTweaks.addControl(lblAlign);
      pnlTweaks.addControl(sdrAlign);
      pnlTweaks.addControl(label9);
      pnlTweaks.addControl(lblCohesion);
      pnlTweaks.addControl(sdrCohesion);
      pnlTweaks.addControl(label11);
      pnlTweaks.addControl(lblSeparation);
      pnlTweaks.addControl(sdrSeparation);
      pnlTweaks.addControl(label13);
      pnlTweaks.addControl(label14);
      pnlTweaks.addControl(lblJitter);
      pnlTweaks.addControl(btnWander);
      pnlTweaks.addControl(btnBoid);
      pnlTweaks.addControl(sdrJitter);
      pnlTweaks.addControl(lable1);
      pnlTweaks.addControl(lblRadius);
      pnlTweaks.addControl(sdrRadius);
      pnlTweaks.addControl(label18);
      pnlTweaks.addControl(lblDistance);
      pnlTweaks.addControl(sdrDistance);
      pnlTweaks.addControl(label20);
      pnlTweaks.addControl(label21);
      pnlTweaks.addControl(lblSpeed);
      pnlTweaks.addControl(sdrSpeed);
      pnlTweaks.addControl(label23);
      pnlTweaks.addControl(lblNeighbourhood);
      pnlTweaks.addControl(sdrNeighbourhood);
      btnColour1 = new GImageButton(this, 20, 450, 40, 40, new String[] { 
        "boidcol1.png", "boidcol1.png", "boidcol1.png"
      } 
      );
      btnColour1.addEventHandler(this, "btnColour1_click");
      btnColour2 = new GImageButton(this, 60, 490, 40, 40, new String[] { 
        "boidcol2.png", "boidcol2.png", "boidcol2.png"
      } 
      );
      btnColour2.addEventHandler(this, "btnColour2_click");
      btnColour3 = new GImageButton(this, 20, 490, 40, 40, new String[] { 
        "boidcol3.png", "boidcol3.png", "boidcol3.png"
      } 
      );
      btnColour3.addEventHandler(this, "btnColour3_click");
      btnColour4 = new GImageButton(this, 60, 450, 40, 40, new String[] { 
        "boidcol4.png", "boidcol4.png", "boidcol4.png"
      } 
      );
      btnColour4.addEventHandler(this, "btnColour4_click");
      btnRestart = new GImageButton(this, 300, 450, 80, 80, new String[] { 
        "resetsq.png", "resetsq.png", "resetsq.png"
      } 
      );
      btnRestart.addEventHandler(this, "btnRestart_click");
      label25 = new GLabel(this, 150, 450, 110, 20);
      label25.setText("Frame Rate");
      label25.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      label25.setOpaque(false);
      label26 = new GLabel(this, 140, 490, 120, 20);
      label26.setText("Calculation time (s)");
      label26.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      label26.setOpaque(false);
      lblFPSvalue = new GLabel(this, 150, 470, 110, 20);
      lblFPSvalue.setText(" ");
      lblFPSvalue.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblFPSvalue.setOpaque(false);
      lblCalcTimeValue = new GLabel(this, 140, 510, 120, 20);
      lblCalcTimeValue.setText(" ");
      lblCalcTimeValue.setTextAlign(GAlign.RIGHT, GAlign.MIDDLE);
      lblCalcTimeValue.setOpaque(false);
    }
    
    // Variable declarations 
    // autogenerated do not edit
    GPanel pnlTweaks; 
    GLabel label1; 
    GImageButton btnSteering; 
    GLabel label2; 
    GLabel lblWander; 
    GSlider sdrWander; 
    GLabel label4; 
    GLabel lblFlocking; 
    GSlider sdrFlocking; 
    GLabel label6; 
    GImageButton btnFlocking; 
    GLabel label7; 
    GLabel lblAlign; 
    GSlider sdrAlign; 
    GLabel label9; 
    GLabel lblCohesion; 
    GSlider sdrCohesion; 
    GLabel label11; 
    GLabel lblSeparation; 
    GSlider sdrSeparation; 
    GLabel label13; 
    GLabel label14; 
    GLabel lblJitter; 
    GImageButton btnWander; 
    GImageButton btnBoid; 
    GSlider sdrJitter; 
    GLabel lable1; 
    GLabel lblRadius; 
    GSlider sdrRadius; 
    GLabel label18; 
    GLabel lblDistance; 
    GSlider sdrDistance; 
    GLabel label20; 
    GLabel label21; 
    GLabel lblSpeed; 
    GSlider sdrSpeed; 
    GLabel label23; 
    GLabel lblNeighbourhood; 
    GSlider sdrNeighbourhood; 
    GImageButton btnColour1; 
    GImageButton btnColour2; 
    GImageButton btnColour3; 
    GImageButton btnColour4; 
    GImageButton btnRestart; 
    GLabel label25; 
    GLabel label26; 
    GLabel lblFPSvalue; 
    GLabel lblCalcTimeValue; 
    

    code

    tweaks (0)

    about this sketch

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

    license

    advertisement

    Peter Lager

    Flocking Explorer

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

    Demonstration of the AI for Games Library

    This combines the wander and flocking behaviours. The sliders and buttons allow you to tweak the behaviours and see what happens.

    http://www.lagers.org.uk

    The algorithms are based on the original ideas of Craig Reynolds (http://www.red3d.com/cwr/boids/)

    urzq
    15 Feb 2012
    Nice one !

    There are some ideas you may (or not ^^) use :
    &gt; Create some interactions with the user, like creating in real time a goal, or obstacles, or a path to fallow.
    &gt; Also, I'm (as many of your viewer I guess :D) not a specialist of boids, so maybe you should also add the possibility to draw some graphical info ?
    Peter Lager
    15 Feb 2012
    This was created so that I could test the wander and flocking algorithms to be used in the game AI library I am creating. The library supports 16 different steering behaviours including obstacle and wall avoidance.
    The library is designed to hide the complexity of using AI but still allow for user to use their own graphics for the 'boids' e.g. sheep, people, aliens ...
    When the library is finished there will be guides demonstarte how to use it.
    You need to login/register to comment.