It depends from what you want to do with the vertex coordinates.
-
Match vertex coordinates with mouse coordinates: use screenX and screenY. Those only give you the 2D coordinates on the screen surface.
-
Match vertex coordinates with other 3D coordinates or use vertex coordinates again: use modelX and modelY and modelZ. Full 3D data with all rotations. See reference.
Full example
Full example for screenX
and screenY
below - please see the two lines in the code marked with ā!!!!
ā.
- We monitor the 2D screen position of the 3D shape throughout and store it in a PVector
screenPos
and use this PVector later in mouseOver() for the check with the mouse (using dist()
).
Please tell me, if this is okay for you. I can provide a more simple example and an example for modelX and modelY and modelZ.
Chrisir
import peasy.*;
PeasyCam cam;
ArrayList<SphereClass> list = new ArrayList();
void setup() {
size(1300, 900, P3D);
cam=new PeasyCam(this, 400);
// init all spheres
SphereClass newSphere;
newSphere=new SphereClass(23, 23, -230,
5, 5, 5);
list.add(newSphere);
newSphere=new SphereClass(273, 23, -230,
10, 5, 20);
list.add(newSphere);
newSphere=new SphereClass(3, 153, 30,
3, 3, 3);
list.add(newSphere);
} // func
void draw() {
background(0);
lights();
// loop over all spheres
for (SphereClass sphere : list) {
// show it
sphere.display();
}
// HUD
cam.beginHUD();
fill(255);
text("use peasycam, click on speroids", 23, 23);
cam.endHUD();
} // func
//----------------------------------------------------------
void mousePressed() {
// loop over all spheres
for (SphereClass sphere : list) {
// select / unselect depending on mouse pos
sphere.selectWhenMouseOver();
}//for
}
// ===========================================================
class SphereClass {
PVector pos; // 3D vector
PVector sizeSphere; // 3D vector
PVector screenPos=new PVector(0, 0); // 2D vector
boolean selected=false;
// constr (pos and size)
SphereClass(float x, float y, float z,
float w, float h, float d) {
pos = new PVector(x, y, z); // 3D vector
sizeSphere = new PVector(w, h, d); // 3D vector
}// constr
void display() {
// draw sphere at pos (x, y, z) coordinate and store 2D screen pos
pushMatrix();
translate(pos.x, pos.y, pos.z);
noStroke();
// we choose the color depending on selected
if (selected)
fill(255, 0, 0); // red
else
fill(0, 0, 255); // blue
// draw the sphere
scale(sizeSphere.x, sizeSphere.y, sizeSphere.z);
sphere(11);
// we monitor the 2D screen pos throughout and store it
screenPos.set(screenX(0, 0, 0), screenY(0, 0, 0)); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
popMatrix();
// show the 2D screen pos
if (keyPressed)
drawSignX(screenPos);
}
void drawSignX( PVector pos ) {
// Draw a "X" sign
//
float sizeHalf=60;
float x=pos.x;
float y=pos.y;
float z=pos.z;
stroke(255);
line(x-sizeHalf, y-sizeHalf, z, x+sizeHalf, y+sizeHalf, z);
line(x+sizeHalf, y-sizeHalf, z, x-sizeHalf, y+sizeHalf, z);
}
boolean selectWhenMouseOver() {
// select / unselect
if (mouseOver())
selected=true;
else
selected=false;
return selected;
}
boolean mouseOver() {
// makes use of the stored positions of screenX and screenY !!!!!!!!!!!!!!!!!!!!!!!!!!
return
dist(mouseX, mouseY, screenPos.x, screenPos.y) < 50;
}
//
}//class
//