Loading...
Logo
Processing Forum
I am using control P5 and peasycam to create 3d agent based system. However, my controlP5's font is quite blurry. And I also don't know how could I move the camera to the center of my object in peasycam.
 
Here is the code:
 
Copy code
  1. import processing.opengl.*;
    import peasy.*;
    import controlP5.*;
  2. kWorld world1;
    PeasyCam cam;
    ControlP5 controlP5;
    PMatrix3D currCameraMatrix;
    PGraphics3D g3;
  3. int population = 500;
  4. boolean alignment_enabled = false;
    boolean coh_enabled = false;
    boolean sep_enabled = false;
    boolean wander_enabled = false;
    boolean steer_enabled = false;
  5. void setup(){
  6.   size(1200,800,P3D);
      //frameRate(30);
      smooth();
      world1 = new kWorld();
      population();
     
      g3 = (PGraphics3D)g;
      cam = new PeasyCam(this, 2000);
      controlP5 = new ControlP5(this);
      control();
      controlP5.setAutoDraw(false);

  7. }
  8. void population(){
  9.   world1 = new kWorld();
      for (int i = 0; i < population; i++) {
        world1.addAgent(new kAgent(new PVector(random(150,width),random(0,height)), new PVector(random(-1,1),random(-1,1)),random(3,5), random(0.1,0.3)));
      }
    }
  10. void draw(){
      background(0); 
      //fill(100);
      //rect(0,0,150,height);
      fill(255);
      PFont font;
      font = loadFont("SansSerif.bold-10.vlw");
      textFont(font,20);
      textMode(SCREEN);
      text("MODELT 2010",0,height-20);
      //stroke(128); 
      //fill(255);
      world1.run();
  11.   gui();

  12. }
  13. void gui() {
      currCameraMatrix = new PMatrix3D(g3.camera);
      camera();
      controlP5.draw();
      g3.camera = currCameraMatrix;
    }
  14. void control(){ 
      //population
      controlP5.addSlider("population",0,2000,500,0,0,80,20).setId(1);
      controlP5.controller("population").setLabel("population");
      //alignment
      controlP5.addBang("alignment",0,40,80,20).setId(2);
      controlP5.controller("alignment").setLabel("alignment");
      //cohesion
      controlP5.addBang("cohesion",0,80,80,20).setId(3);
      controlP5.controller("cohesion").setLabel("cohesion");
      //separation
      controlP5.addBang("separation",0,120,80,20).setId(4);
      controlP5.controller("separation").setLabel("separation");
      //wander
      controlP5.addBang("wander",0,160,80,20).setId(5);
      controlP5.controller("wander").setLabel("wander");
      //steer
      controlP5.addBang("steer",0,200,80,20).setId(6);
      controlP5.controller("steer").setLabel("steer");
     
  15. }
  16. void controlEvent(ControlEvent theEvent) {
      switch(theEvent.controller().id()) {
        case(1):
        population = (int)(theEvent.controller().value());
        //println("population" + population);
        population();
        break;
        case(2):
        alignment_enabled = !alignment_enabled;
        break;
        case(3):
        coh_enabled = !coh_enabled;
        break;
        case(4):
        sep_enabled = !sep_enabled;
        break;
        case(5):
        wander_enabled = !wander_enabled;
        break;
        case(6):
        steer_enabled = !steer_enabled;
        break;
      }
    }
  17. class kAgent{
  18.   PVector        vel;
      PVector        pos;
      float        maxVel;
      float        maxForce;
      float        wandertheta;
  19.   // constructor
      kAgent(
      PVector _pos,
      PVector _vel,
      float _maxVel,
      float _maxForce){
  20.     pos = _pos;
        vel = _vel;
        maxVel = _maxVel;
        maxForce = _maxForce;
  21.   }

  22.   // calculates new location
      void update(ArrayList pop){ 
        //set acc to 0
        PVector acc = new PVector(0,0);
  23.     // Alignment
        if(alignment_enabled){
          PVector ali = align(pop);    
          ali.mult(1.0);
          acc.add(ali);
        }
  24.     // Cohesion
        if(coh_enabled){
          PVector coh = cohesion(pop); 
          coh.mult(1.0);
          acc.add(coh);
        }
  25.     // Separation
        if(sep_enabled){
          PVector sep = separate(pop);
          sep.mult(5.0);
          acc.add(sep);
        }
  26.     // wander
        if(wander_enabled){
          PVector wander = wander();
          wander.mult(0.5);
          acc.add(wander);
        }
  27.     // steer
        if(steer_enabled){
          PVector seek = steer(new PVector(mouseX,mouseY));
          seek.mult(1);
          acc.add(seek);
        }
  28.     // add acc to vel
        vel.add(acc);
        // limit vel to maxVel
        vel.limit(maxVel); 
        // add vel to pos
        pos.add(vel);
        acc.set(0,0,0);  // reset acc to 0 each iteration
  29.     borders();
        render();
  30.   }

  31.   // seek
      /*
      void seek(PVector target) {
       acc.add(steer(target));
       }
       */
  32.   // steer
      PVector steer(PVector target) {
        PVector steer;  // The steering vector
        target.sub(pos);
        float distance = target.mag();
  33.     if (distance > 0) {
          target.normalize();
          target.mult(maxVel);
          target.sub(vel);
          //steer = kVec.clone(target);
          target.limit(maxForce);
        }
        else {
          target = new PVector(0,0);
        }
        return target;
      }
  34.   // wander
      PVector wander() {
        float wanderR = 16;        
        float wanderD = 60;        
        float change = 0.25;
        wandertheta += random(-change,change);    
  35.     PVector circleloc = new PVector(vel.x, vel.y);
        circleloc.normalize();           
        circleloc.mult(wanderD);         
        circleloc.add(pos);             
  36.     PVector circleOffSet = new PVector(wanderR*cos(wandertheta),wanderR*sin(wandertheta));
        circleOffSet.add(circleloc);
        //acc.add(steer(circleOffSet));
        PVector vec = steer(circleOffSet);
        return vec;
      } 

  37.   // separation
      PVector separate (ArrayList pop) {
        float desiredseparation = 25.0;
        PVector sum = new PVector(0,0,0);
        int count = 0;
  38.     for (int i = 0 ; i < pop.size(); i++) {
          kAgent other = (kAgent) pop.get(i);
          float dist = pos.dist(other.pos);
          // if the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
          if ((dist > 0) && (dist < desiredseparation)) {
            // calculate vector pointing away from neighbor
            PVector diff = new PVector(pos.x, pos.y);
            diff.sub(other.pos);
            diff.normalize();
            diff.mult(1/dist);          // weight by distance
            sum.add(diff);
            count++;                     // keep track of how many
          }
        }
        // average -- divide by how many
        if (count > 0) {
          sum.mult(1/(float)count);
        }
        return sum;
      }

  39.   // alignment
      PVector align (ArrayList pop) {
        float neighbordist = 50.0;
        PVector sum = new PVector(0,0);
        int count = 0;
        for (int i = 0 ; i < pop.size(); i++) {
          kAgent other = (kAgent) pop.get(i);
          float dist = pos.dist(other.pos);
          if ((dist > 0) && (dist < neighbordist)) {
            sum.add(other.vel);
            count++;
          }
        }
        if (count > 0) {
          sum.mult(1/(float)count);
          sum.limit(maxForce);
        }
        return sum;
      }

  40.   // cohesion
      PVector cohesion (ArrayList pop) {
        float neighbordist = 50.0;
        PVector sum = new PVector(0,0);  
        int count = 0;
        for (int i = 0 ; i < pop.size(); i++) {
          kAgent other = (kAgent) pop.get(i);
          float dist = pos.dist(other.pos);
          if ((dist > 0) && (dist < neighbordist)) {
            sum.add(other.pos); // Add location
            count++;
          }
        }
        if (count > 0) {
          sum.mult(1/(float)count);
          return steer(sum);  // steer towards the location
        }
        return sum;
      }

  41.   void render() {
        fill(200);
        stroke(255);
        ellipse(pos.x,pos.y,10,10);
      }

  42.   void borders() {
        if ((pos.x < 150 && vel.x < 0)||(pos.x > width && vel.x > 0)) {
          //pos.x = width;
          vel.x = -vel.x * 0.95;
        }
        if ((pos.y < 10 && vel.y < 0)||(pos.y > height - 10 && vel.y > 0)) {
          //pos.y = height;
          vel.y = -vel.y * 0.95;
        }
      }
  43. }
  44. class kWorld {
      ArrayList population;
     
      kWorld() {
         population = new ArrayList(); // initialize the arraylist
      }
  45.   // cycles through each agent passing the population to it
      void run(){
        for (int i = 0; i < population.size(); i++) {
          kAgent a = (kAgent) population.get(i); 
          a.update(population);
        }
      }
  46.   // add agent
      void addAgent(kAgent a) {
        population.add(a);
      }
  47. }
 
 
 
 
 
 

 
 
 
 
 
 

 

Replies(3)

the blurriness comes from the P3D renderer that has some problems with rendering bitmap fonts.
in your case i recommend this great example on how to use peasy cam with controlP5

http://code.google.com/p/controlp5/source/browse/trunk/examples/controlP5WithPeasyCam/controlP5WithPeasyCam.pde?r=6

I've actually seen this post before I start my question. I'm using part of his code in my sketch and I don't why the result is different from his...

for example, although you loaded opengl you are still using P3D