Can someone help me with this bug?
in
Contributed Library Questions
•
1 year ago
This is my code and the bug i am having is that whenever i click the mouse peasy cam seems to reset to 0,0,0. I have scoured the code 20 some times and i cannot find the problem.
//TODO::: freeze camera
//TOFDO: move to selected particle
//TODO::REdo all the tabs and update to current standards.
import processing.opengl.*;
import controlP5.*;
import peasy.*;
PGraphics buffer; // buffer
Cube[] cubes; // cubes
PeasyCam cam;
ControlP5 controlP5;
PGraphics3D g3;
PMatrix3D currCameraMatrix;
int Xi;
int Yi;
int Zi;
int idSave;
void setup() {
size(1400, 700, OPENGL);
frameRate(25);
frame.setTitle("Variable Geoetry Neo-Fluid Prototype 1.2.1");
g3 = (PGraphics3D)g;
buffer = createGraphics(width, height, P3D);
cam = new PeasyCam(this, 10);
cam.setResetOnDoubleClick(false);
cam.setMinimumDistance(50);
controlP5 = new ControlP5(this);
controlP5.addButton("button",10,100,60,80,20).setId(1);
controlP5.addButton("buttonValue",4,100,90,80,20).setId(2);
controlP5.addButton("Create Geometry",4,10,500,100,30);
controlP5.addButton("Hotfix1",40,100,60,80,20);
controlP5.addButton("Hotfix2",40,100,80,80,20);
controlP5.setAutoDraw(false);
controlP5.addSlider("sliderValue",0,255,128,100,200,10,100);
controlP5.addSlider("slider",100,200,128,100,160,100,10);
controlP5.addTextfield("Serial Output",100,160,200,20);
controlP5.addTextfield("Number of Particles",10,300,200,20);
controlP5.addToggle("Static or Dynamic?",false,10,350,80,20).setMode(ControlP5.SWITCH);
controlP5.addTextlabel("label1","Company",300,630);
controlP5.addTextlabel("label2","Unit",150,630);
controlP5.addTextlabel("label3","Identifier",10,630);
// put cubes randomly in the scene
cubes = new Cube[75];
for (int i = 0; i < cubes.length; i++) {
cubes[i] = new Cube(
i, // identifiant
Xi = -15 + (int)random(300), // position x
Yi = -15 + (int)random(300), // position y
Zi = -15 + (int)random(300), // position z
5 + (int)random(150) // taille
);
}
}
void draw() {
hint(ENABLE_DEPTH_TEST);
background(255);
lights();
for (int i = 0; i < cubes.length; i++) {
cubes[i].display(this.g);
}
cubes[4].moveUp();
hint(DISABLE_DEPTH_TEST);
gui();
}
void mouseClicked() {
// draw the scene in the buffer
buffer.beginDraw();
buffer.background(getColor(-1)); // since background is not an object, its id is -1
buffer.noStroke();
buffer.setMatrix(g3.camera);
for (int i = 0; i < cubes.length; i++) {
cubes[i].drawBuffer(buffer);
}
buffer.endDraw();
// get the pixel color under the mouse
color pick = buffer.get(mouseX, mouseY);
// get object id
int id = getId(pick);
// if id > 0 (background id = -1)
if (id >= 0) {
// change the cube color
cubes[id].changeColor();
}
println(id);
idSave = id;
}
// id 0 gives color -2, etc.
color getColor(int id) {
return -(id + 2);
}
// color -2 gives 0, etc.
int getId(color c) {
return -(c + 2);
}
class Cube {
// variables
int id; // id
int x, y, z, w; // position (x, y, z) and width (w)
color c; // color (in scene, not buffer)
// constructor
public Cube(int id, int x, int y, int z, int w) {
this.id = id;
this.x = x;
this.y = y;
this.z = z;
this.w = w;
c = color(random(255),200, random(255));
}
// make the color change
public void changeColor() {
int r = (int)red(c);
int g = (int)green(c);
int b = (int)blue(c);
c = color(r, 255 - g, b);
}
// display the cube on screen
public void display(PGraphics ecran) {
ecran.fill(c);
drawCube(ecran);
}
// draw the cube in the buffer
public void drawBuffer(PGraphics buffer) {
color idColor = getColor(id);
buffer.fill(idColor);
drawCube(buffer);
}
private void drawCube(PGraphics g) {
g.pushMatrix();
g.translate(x, y, z);
g.box(5);
g.popMatrix();
}
private void moveLeft() {
g.translate(x++,y,z);}
private void moveRight() {
g.translate(x--,y,z);}
private void moveUp() {
g.translate(x,y++,z);}
private void moveDown() {
g.translate(x,y--,z);}
private void moveStop() {
g.translate(0,0,0);}
private void ToParticle(){
println(idSave);
}
}
void gui() {
currCameraMatrix = new PMatrix3D(g3.camera);
camera();
controlP5.draw();
controlP5.addTab(this,"Serial");
controlP5.addTab(this,"New Geometry...");
controlP5.tab("Serial").activateEvent(true);
controlP5.tab("New Geometry...").setColorBackground(180);
controlP5.tab("Particles").setColorBackground(180);
controlP5.controller("slider").moveTo("Particles");
controlP5.controller("sliderValue").moveTo("Particles");
controlP5.controller("Serial Output").moveTo("Serial");
controlP5.controller("Number of Particles").moveTo("New Geometry...");
controlP5.controller("Static or Dynamic?").moveTo("New Geometry...");
controlP5.controller("Create Geometry").moveTo("New Geometry...");
controlP5.controller("label1").moveTo("Particles");
controlP5.controller("label2").moveTo("Particles");
controlP5.controller("label3").moveTo("Particles");
controlP5.controller("Hotfix1").moveTo("New Geometry...");
controlP5.controller("Hotfix2").moveTo("New Geometry...");
}
//TOFDO: move to selected particle
//TODO::REdo all the tabs and update to current standards.
import processing.opengl.*;
import controlP5.*;
import peasy.*;
PGraphics buffer; // buffer
Cube[] cubes; // cubes
PeasyCam cam;
ControlP5 controlP5;
PGraphics3D g3;
PMatrix3D currCameraMatrix;
int Xi;
int Yi;
int Zi;
int idSave;
void setup() {
size(1400, 700, OPENGL);
frameRate(25);
frame.setTitle("Variable Geoetry Neo-Fluid Prototype 1.2.1");
g3 = (PGraphics3D)g;
buffer = createGraphics(width, height, P3D);
cam = new PeasyCam(this, 10);
cam.setResetOnDoubleClick(false);
cam.setMinimumDistance(50);
controlP5 = new ControlP5(this);
controlP5.addButton("button",10,100,60,80,20).setId(1);
controlP5.addButton("buttonValue",4,100,90,80,20).setId(2);
controlP5.addButton("Create Geometry",4,10,500,100,30);
controlP5.addButton("Hotfix1",40,100,60,80,20);
controlP5.addButton("Hotfix2",40,100,80,80,20);
controlP5.setAutoDraw(false);
controlP5.addSlider("sliderValue",0,255,128,100,200,10,100);
controlP5.addSlider("slider",100,200,128,100,160,100,10);
controlP5.addTextfield("Serial Output",100,160,200,20);
controlP5.addTextfield("Number of Particles",10,300,200,20);
controlP5.addToggle("Static or Dynamic?",false,10,350,80,20).setMode(ControlP5.SWITCH);
controlP5.addTextlabel("label1","Company",300,630);
controlP5.addTextlabel("label2","Unit",150,630);
controlP5.addTextlabel("label3","Identifier",10,630);
// put cubes randomly in the scene
cubes = new Cube[75];
for (int i = 0; i < cubes.length; i++) {
cubes[i] = new Cube(
i, // identifiant
Xi = -15 + (int)random(300), // position x
Yi = -15 + (int)random(300), // position y
Zi = -15 + (int)random(300), // position z
5 + (int)random(150) // taille
);
}
}
void draw() {
hint(ENABLE_DEPTH_TEST);
background(255);
lights();
for (int i = 0; i < cubes.length; i++) {
cubes[i].display(this.g);
}
cubes[4].moveUp();
hint(DISABLE_DEPTH_TEST);
gui();
}
void mouseClicked() {
// draw the scene in the buffer
buffer.beginDraw();
buffer.background(getColor(-1)); // since background is not an object, its id is -1
buffer.noStroke();
buffer.setMatrix(g3.camera);
for (int i = 0; i < cubes.length; i++) {
cubes[i].drawBuffer(buffer);
}
buffer.endDraw();
// get the pixel color under the mouse
color pick = buffer.get(mouseX, mouseY);
// get object id
int id = getId(pick);
// if id > 0 (background id = -1)
if (id >= 0) {
// change the cube color
cubes[id].changeColor();
}
println(id);
idSave = id;
}
// id 0 gives color -2, etc.
color getColor(int id) {
return -(id + 2);
}
// color -2 gives 0, etc.
int getId(color c) {
return -(c + 2);
}
class Cube {
// variables
int id; // id
int x, y, z, w; // position (x, y, z) and width (w)
color c; // color (in scene, not buffer)
// constructor
public Cube(int id, int x, int y, int z, int w) {
this.id = id;
this.x = x;
this.y = y;
this.z = z;
this.w = w;
c = color(random(255),200, random(255));
}
// make the color change
public void changeColor() {
int r = (int)red(c);
int g = (int)green(c);
int b = (int)blue(c);
c = color(r, 255 - g, b);
}
// display the cube on screen
public void display(PGraphics ecran) {
ecran.fill(c);
drawCube(ecran);
}
// draw the cube in the buffer
public void drawBuffer(PGraphics buffer) {
color idColor = getColor(id);
buffer.fill(idColor);
drawCube(buffer);
}
private void drawCube(PGraphics g) {
g.pushMatrix();
g.translate(x, y, z);
g.box(5);
g.popMatrix();
}
private void moveLeft() {
g.translate(x++,y,z);}
private void moveRight() {
g.translate(x--,y,z);}
private void moveUp() {
g.translate(x,y++,z);}
private void moveDown() {
g.translate(x,y--,z);}
private void moveStop() {
g.translate(0,0,0);}
private void ToParticle(){
println(idSave);
}
}
void gui() {
currCameraMatrix = new PMatrix3D(g3.camera);
camera();
controlP5.draw();
controlP5.addTab(this,"Serial");
controlP5.addTab(this,"New Geometry...");
controlP5.tab("Serial").activateEvent(true);
controlP5.tab("New Geometry...").setColorBackground(180);
controlP5.tab("Particles").setColorBackground(180);
controlP5.controller("slider").moveTo("Particles");
controlP5.controller("sliderValue").moveTo("Particles");
controlP5.controller("Serial Output").moveTo("Serial");
controlP5.controller("Number of Particles").moveTo("New Geometry...");
controlP5.controller("Static or Dynamic?").moveTo("New Geometry...");
controlP5.controller("Create Geometry").moveTo("New Geometry...");
controlP5.controller("label1").moveTo("Particles");
controlP5.controller("label2").moveTo("Particles");
controlP5.controller("label3").moveTo("Particles");
controlP5.controller("Hotfix1").moveTo("New Geometry...");
controlP5.controller("Hotfix2").moveTo("New Geometry...");
}
1