We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › cursor location with translate and rotation
Page Index Toggle Pages: 1
cursor location with translate and rotation (Read 676 times)
cursor location with translate and rotation
Apr 15th, 2008, 8:39pm
 
hello,
i'm working on a project with a large amount of words displayed inside a 3d scene. i'm trying to make a simple selection of a word when the user clicks on it. it sounds easy but when i rotate or translate my scene, the mouse position (mouseX and mouseY) doesn't match with the word position (x y, z), wich is normal, but i just don't know how to do it.
actually, i'd need a method to convert the words positions in 2d and inside the display (like the mouse position)...

the basic mousePressed function:

void mousePressed() {
 if (mouseButton == LEFT){
   float distance=10;
   for (int i = 0; i < wordNum; i++) {
     Word w = words[i];
     float d = dist(mouseX, mouseY, w.x, w.y);
     if (d < distance) {
       selection = w;
       distance = d;
     }
   }
   if (selection != null) print(w.label);
 }
}

it works if i don't translate anything but when i do:

translate(width/2,height/2,zoom);

before displaying my words inside the draw loop, it doesn't work anymore...

so i tried this:

void mousePressed() {
 if (mouseButton == LEFT){
   float distance=10;
   float newx=map(mouseX,0,width,-width/2,width/2);
   float newy=map(mouseY,0,height,-height/2,height/2);
   for (int i = 0; i < wordNum; i++) {
     Word w = words[i];
     float d = dist(newx, newy, w.x, w.y);
     if (d < distance) {
       selection = w;
       distance = d;
     }
   }
   if (selection != null) print(w.label);
 }
}

if there is no z translation, it works, but i need some z translations and some rotations...
help is welcome!!!!!
Re: cursor location with translate and rotation
Reply #1 - Apr 16th, 2008, 11:10am
 
Check this in Reference > Extended > Coordinates :

http://processing.org/reference/screenX_.html
and / or
http://processing.org/reference/modelX_.html

and this in Processing hacks :

http://www.processinghacks.com/hacks/picking
Re: cursor location with translate and rotation
Reply #2 - Apr 16th, 2008, 4:25pm
 
hello! thanks a lot, it works!!
so with modelX/Y/Z, i record my words coordinates based on the current set of transformations and i use screenX and screenY to map those coordinates inside the mousePressed function.
Re: cursor location with translate and rotation
Reply #3 - Apr 16th, 2008, 4:52pm
 
Which method you use depends on how exactly you're doing your translates and rotates etc.

If every word on screen has the same basic rotate/translate, and then just a single translate to offset from a global "center" then you should use worldX/Y on the mouseX/Y to get their in-world position, and then compare that with the x/y of each word.

If however each word is rotated and translated arbitrarily, then you'll have to store the screenX and screenY of each word after all the translations, and use that location in your distance check against the original mouseX/Y values.
Page Index Toggle Pages: 1