We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello all,
I am trying to be able to click on a sphere in a 3D sketch with the mouse to select a sphere.
Since I use rotate and translate when placing the sphere, I wanted to use modelX,modelY
to get the model position. But to have a match with the mouse I need ScreenX,ScreenY
. Or so I am guessing.
Not sure how to handle this though:
this is part of class that in turn is stored in ArrayList to hold all the spheres:
pushMatrix();
translate(x, y, z);
fill(col);
noStroke();
if (showBox||true) {
//box(55);
//ellipse(0, 0, 7, 7);
sphere(7);
// the sphere / box was drawn at (0, 0, 0), store that location
float x1 = modelX(0, 0, 0); // this takes care of translate and rotate
float y1 = modelY(0, 0, 0);
float z1 = modelZ(0, 0, 0);
scX = screenX(x1, y1, z1); // this takes care of the projection into 2D space for the mouse
scY = screenX(x1, y1, z1);
}
popMatrix();
I now want to match scX and scY with mouseX and mouseY:
void mousePressed() {
PVectorInt result = new PVectorInt(0, 0, 0);
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
for (int k = 0; k < boxes[i][j].length; k++) {
if (dist(mouseX, mouseY, boxes[i][j][k].scX, boxes[i][j][k].scY) < 40) {
result = new PVectorInt( i, j, k);
listShape.add ( new PVectorInt( i, j, k));
println(listShape.size());
return;
}
}
}
}
}
but it doesn't work....
please tell me how I handle modelX and screenX
thank you...!
Chrisir
Answers
the first post describes the problem.
the 2nd is mcve if anyone needs a running version.
lines 338-352 are relevant
(and line 273)
please help.
thank you
Chrisir ;-)
solved.
;-)
for anyone who is interested
I had a typo above
screenX and screenY themself do take into account the matrix (translate / rotate) so no need to use modelX... first
this is working version with screenX,screenY in case anyone is interested
Sometimes when the corner of the cube is in the middle position (no facing the face of the cube but between two faces) the ball picking action doesn't seem to work in certain spheres in that rotation. However if you rotate 5 or 10 degrees, you will notice that the algorithm was indeed working but it was picking a sphere behind it.
You think the algorithm is bias to picking objects at certain depth? @Chrisir, could you explain how do you project from a 2D into a 3D?
Kf
True. I use dist() to figure out which ball has been clicked. And it stops at the first one. Which is sometimes wrong. Alternatively, we could store all distances and then keep the smallest distance. Might be slower but more precise.
This is referring to the same issue, right? It is biased in that it uses a triple for-loop in mousePressed, and the order of that determines which depth is checked first. The above idea should solve this. We could try to run the depth from behind to the front so that we have the smallest balls (more depth) first.
You can't but there are workarounds.
See my sketch here (and also good discussion):
With my sketch you can drag a ball in 3D space with the mouse:
when you just drag it, you change x,y
when you hold a key and drag, you change Z with mouse up and down (y)
https://forum.processing.org/two/discussion/20755/how-to-map-a-2d-coordinate-into-3d#latest