Why my controlP5 is blurry and how to adjust my peasycam to the center of my object?
in
Contributed Library Questions
•
3 years ago
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:
- import processing.opengl.*;
import peasy.*;
import controlP5.*; - kWorld world1;
PeasyCam cam;
ControlP5 controlP5;
PMatrix3D currCameraMatrix;
PGraphics3D g3; - int population = 500;
- boolean alignment_enabled = false;
boolean coh_enabled = false;
boolean sep_enabled = false;
boolean wander_enabled = false;
boolean steer_enabled = false; - void setup(){
- 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);
}- void population(){
- 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)));
}
} - 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(); - gui();
}- void gui() {
currCameraMatrix = new PMatrix3D(g3.camera);
camera();
controlP5.draw();
g3.camera = currCameraMatrix;
} - 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");
- }
- 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;
}
} - class kAgent{
- PVector vel;
PVector pos;
float maxVel;
float maxForce;
float wandertheta; - // constructor
kAgent(
PVector _pos,
PVector _vel,
float _maxVel,
float _maxForce){ - pos = _pos;
vel = _vel;
maxVel = _maxVel;
maxForce = _maxForce; - }
// calculates new location
void update(ArrayList pop){
//set acc to 0
PVector acc = new PVector(0,0);- // Alignment
if(alignment_enabled){
PVector ali = align(pop);
ali.mult(1.0);
acc.add(ali);
} - // Cohesion
if(coh_enabled){
PVector coh = cohesion(pop);
coh.mult(1.0);
acc.add(coh);
} - // Separation
if(sep_enabled){
PVector sep = separate(pop);
sep.mult(5.0);
acc.add(sep);
} - // wander
if(wander_enabled){
PVector wander = wander();
wander.mult(0.5);
acc.add(wander);
} - // steer
if(steer_enabled){
PVector seek = steer(new PVector(mouseX,mouseY));
seek.mult(1);
acc.add(seek);
} - // 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 - borders();
render(); - }
// seek
/*
void seek(PVector target) {
acc.add(steer(target));
}
*/- // steer
PVector steer(PVector target) {
PVector steer; // The steering vector
target.sub(pos);
float distance = target.mag(); - 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;
} - // wander
PVector wander() {
float wanderR = 16;
float wanderD = 60;
float change = 0.25;
wandertheta += random(-change,change); - PVector circleloc = new PVector(vel.x, vel.y);
circleloc.normalize();
circleloc.mult(wanderD);
circleloc.add(pos); - PVector circleOffSet = new PVector(wanderR*cos(wandertheta),wanderR*sin(wandertheta));
circleOffSet.add(circleloc);
//acc.add(steer(circleOffSet));
PVector vec = steer(circleOffSet);
return vec;
}
// separation
PVector separate (ArrayList pop) {
float desiredseparation = 25.0;
PVector sum = new PVector(0,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 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;
}
// 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;
}
// 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;
}
void render() {
fill(200);
stroke(255);
ellipse(pos.x,pos.y,10,10);
}
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;
}
}- }
- class kWorld {
ArrayList population;
kWorld() {
population = new ArrayList(); // initialize the arraylist
} - // 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);
}
} - // add agent
void addAgent(kAgent a) {
population.add(a);
} - }
2