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 & HelpOpenGL and 3D Libraries › Converting mouse position to 3d space
Page Index Toggle Pages: 1
Converting mouse position to 3d space (Read 1494 times)
Converting mouse position to 3d space
Jul 20th, 2009, 4:58am
 
Tricky issue here. I have a 3d plane with 4 vertices. These vertices, in a 2D array, get manually rotated (using the custom trig functions), loaded into a final 2D array, and then draw the QUAD to the screen.

I'm able to trigger events with the mouse when it's over a vertex by using  screenX and a boundary if() statement (e.g. when the mouse is within 20 pixels of the screen coordinates of a given vertex). All well and good, but I need to be able to assign that vertex new coordinates based on mouse position.

I can't seem to change the values of the final array elements. When I try with original array values, I can change them, but I can't come up with a way to translate the mouse position based on the transformations that are happening to the vertices.

In a nutshell: While the 'picking' works fine, I need a way to reassign the coordinates of a given vertex to those of the mouse position on the screen. Obviously, the given mouseX/Y coordinates won't correspond to the translations the plane has undergone, so I need a way to get those values.

Any thoughts? Let me know if you need code (it's ugly at this stage).
Re: Converting mouse position to 3d space
Reply #1 - Jul 20th, 2009, 5:35am
 
Sounds complicated. In OpenGL you could simply use the gluUnProject function for getting 3d positions from the z-buffer, but I guess what you're doing requires some nasty math.
Re: Converting mouse position to 3d space
Reply #2 - Jul 20th, 2009, 12:44pm
 
Complicated is right. If it helps, I have the trig tansformations handy in another function, and if I know them, there's gotta be a way to apply them to the mouse coordinates, right?

Still, I've been having a look at gluUnproject, and it looks like exactly the kind of thing I need (the 3d version of screenX). Will working in OpenGL rendering screw up the rest of my code? (I was using OPENGL as my 3d space anyway).
Re: Converting mouse position to 3d space
Reply #3 - Jul 20th, 2009, 3:03pm
 
let me see if i understand. you need to find the point you clicked on which you already do, and you need to change its position to where you clicked.. if that is correct you just need to project mouse position back to 3d and re-position the current picked vertex to a known point which is the mouse position.

so find your nearest vertex when you click, set it as active for re-position and based on your mouse position assign a new position to the active vertex.

basically you use unproject and project


have fun.
Page Index Toggle Pages: 1