sliders in fixed positions on canvas
in
Contributed Library Questions
•
9 months ago
Hey guys! Merry Christmas to everyone btw! :)
Here's another noob question.
I have a 3D sketch and I added a
timeline slider using controlP5 configurations. However, it seems that the entire slider turns when I turn the camera...
How can I make it "lock" on a specific place on the canvas? What am I doin' wrong?
Here's part of the script with all arrangements for the slider marked in yellow:
import peasy.org.apache.commons.math.*; // imports all 3D related libraries
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;
import controlP5.*; // library for the slider - interface
ControlP5 controlP5;
Slider timeline_slider;
PeasyCam cam;
PMatrix3D currCameraMatrix;
PGraphics3D g3;
float worldSize = 300; // the space - rendered as a cube - limiting agents movement
StopWatchTimer sw; // a visual timer-clock implementation on canvas
int time; // a counter for time triggering events
int wait = 1000; // 1 second
Facilities work; // wondering agent -- represents (W)
Facilities retail; // wondering agent -- represents (R)
Facilities school; // wondering agent -- represents (S)
Facilities leisure; // wondering agent -- represents (L)
ArrayList <Capsule> agents;
float d = 25;
int counter=0; // counter inception
int temp=0;
boolean animate = true; // boolean for pausing the animation sequence
void setup() {
size(600,600,OPENGL);
cam = new PeasyCam(this, 300);
cam.lookAt(150, 150, 300);
cam.setMinimumDistance(10);
cam.setMaximumDistance(1500);
time = millis(); // store the current time
smooth(); // setup of visual time clock
println (millis());
sw = new StopWatchTimer();
sw.start();
// Interface Sliders
controlP5 = new ControlP5(this);
timeline_slider = controlP5.addSlider("timeline", 0, 180, 20, 550, 200, 10); // name, min, max, default, x, y, w, h
// creating the facilities
work = new Facilities(50,50,50); // generates a hypothetic invisible agent (W)
retail = new Facilities(30,30,30); // generates a hypothetic invisible agent (R)
school = new Facilities(240,240,240); // generates a hypothetic invisible agent (S)
leisure = new Facilities(90,90,90); // generates a hypothetic invisible agent (L)
agents = new ArrayList<Capsule>(); // creating the agents
// setting the emitter in random positions for each group
for (int i = 0; i < 115; i++) {
if (i < 20) agents.add(new MW1(150,150,150,color(255,0,0),i)); // Red
else if (i < 45) agents.add(new FW1(100,100,100,color(0,255,0),i)); // Green
else if (i < 65) agents.add(new MW2(200,200,200,color(0,255,255),i)); // Cyan
else if (i < 85) agents.add(new FnW2(80,80,80,color(0,155,0),i)); // dark Green
else if (i < 95) agents.add(new MW3(130,130,130,color(255,255,0),i)); // Yellow
else if (i < 105) agents.add(new FnW3(60,60,60,color(255,0,255),i)); // Purple
else agents.add(new CS3(10,10,10,color(0,0,255),i)); // Blue
}
}
//--------------configuring the canvas & the counter-------------
void draw() {
background(20);
time(); // reference to the visual timer
gui();
controlP5.controller("timeline").setValue(counter); // set the slider to time value
displayWorldCenter();
noLights();
cam.beginHUD();
cam.endHUD();
cam.setMouseControlled(true);
if(mouseY<80) {
cam.setMouseControlled(false);
}
// check the difference between now and the previously stored time is greater than the wait interval
if (animate) {
if(millis() - time >= wait){
println(counter);// if it is, do something
temp=counter;
time = millis();// also update the stored time
counter++; // counter augmentation
if(counter >180){ // check whether time exceeds 180 sec. aka 24:00 hours - enables looping
counter=0;
}
}
}
//-------------------------------------------------------------
work.Default(); // main-basic functions of (W) facility
retail.Default(); // main-basic functions of (R) facility
school.Default(); // main-basic functions of (S) facility
leisure.Default(); // main-basic functions of (L) facility
PVector vectorOfwork = new PVector(work.location.x, work.location.y, work.location.z); // creates a vector that connects all individual agents with the (W) facility
PVector vectorOfretail = new PVector(retail.location.x, retail.location.y, retail.location.z); // creates a vector that connects all individual agents with the (R) facility
PVector vectorOfschool = new PVector(school.location.x, school.location.y, school.location.z); // creates a vector that connects all individual agents with the (S) facility
PVector vectorOfleisure = new PVector(leisure.location.x, leisure.location.y, leisure.location.z); // creates a vector that connects all individual agents with the (L) facility
int i = 0; // i & j make sure that each agent returns to the family it belongs
int j = 0;
for (Capsule agent : agents) { // instancing - creating objects
//---------------------implementing selection------------------
float sx = screenX(agent.location.x, agent.location.y, agent.location.z);
float sy = screenY(agent.location.x, agent.location.y, agent.location.z);
float diam = map(agent.location.z, -worldSize/2, worldSize/2, 10, 20);
boolean selected = dist(mouseX, mouseY, sx, sy) < diam/2;
if (selected) { fill(255, 0, 0); }
if (selected) {
agent.advance();
agent.displayINFO();
}
//-------------------------------------------------------------
agent.display();
if (animate) {
agent.boundaries(); // basic behavior for ALL the agents
agent.update();
agent.wander();
agent.wander3D();
agent.separateSpecial(agents); // global separation that affects ALL the agents
if (counter>0 && counter<=60) {
if (agent instanceof FnW2){ // females attach&follow males - couples
agent.arrive( agents.get(i-20).location );
}
else if (agent instanceof FnW3) { // females attach&follow males - 3 member family
agent.arrive( agents.get(i-10).location );
}
else if (agent instanceof CS3) { // children attach&follow females - 3 member family
agent.arrive( agents.get(i-10).location );
}
i++;
}
if (counter>60 && counter<=120) { // time reference to 6.00-14.00 hours
agent.separate(agents);
if (agent instanceof MW1){
agent.arrive( vectorOfwork );
}
else if (agent instanceof MW2) {
agent.arrive( vectorOfwork );
}
else if (agent instanceof MW3) {
agent.arrive( vectorOfwork );
}
else if (agent instanceof FW1) {
agent.arrive( vectorOfwork );
}
else if (agent instanceof FnW2) {
agent.arrive( vectorOfretail );
}
else if (agent instanceof FnW3) {
agent.arrive( vectorOfretail );
}
else if (agent instanceof CS3) {
agent.arrive( vectorOfschool );
}
}
if (counter>120 && counter<=180) {
if (agent instanceof FnW2){ // females attach&follow males - couples
agent.arrive( agents.get(j-20).location );
}
else if (agent instanceof FnW3) { // females attach&follow males - 3 member family
agent.arrive( agents.get(j-10).location );
}
else if (agent instanceof CS3) { // children attach&follow females - 3 member family
agent.arrive( agents.get(j-10).location );
}
j ++;
}
}
}
}
void displayWorldCenter(){
pushMatrix();
translate(150,150,150);
stroke(255); noFill(); // stroke of the cube (box)
box(worldSize);
stroke(#209889); noFill(); // stroke of the ellipses(center)
ellipse(0,0,20,20);
rotateY(radians(90));
ellipse(0,0,20,20);
rotateZ(radians(90));
ellipse(0,0,20,20);
rotateX(radians(90));
rotateY(radians(90));
ellipse(0,0,20,20);
popMatrix();
}
//-------------------------------------------------------------
void time() { // visual configuration for the timer-clock
fill(#000000);
textAlign(CENTER);
text(nf(sw.hour(), 2)+":"+nf(sw.minute(), 2)+":"+nf(sw.second(), 2)+":"+nf(sw.hundrensec(), 2), 260, 290);
}
void gui() {
controlP5.draw();
controlP5.setAutoDraw(false);
}
void keyPressed(){
if (key=='a') animate = true;
else if (key=='s') animate = false;
}
1