PDA

View Full Version : converting 2D screen coordinates to a 3D world vector



bleach
02-11-2002, 01:25 AM
Hi,

I am writing a 3D tic tac toe game where I want the user to be able to click on a square in the grid, the only thing is that the grid is not static ie: it can move around, rotate etc... I'm also using perspective which I think complicates things.

Does anyone know how to convert 2D screen coords into 3D world coords at say a fixed z value and give it a vector so I can project a line through the grid to find which square it intersects first?

cheers

Lawrence

jide
02-11-2002, 02:10 AM
normally, gluUnProject can do that, and there may be other things.

JD

fritzlang
02-13-2002, 04:35 AM
Yes, you can use ReadPixels to get the
depthcomponent ( z ) & use that with
glUnproject to get the x,y,z worldcoords.

cheers

Osku
02-13-2002, 04:43 AM
Hello

You can also use slightly different colors on each square and use glReadPixels to determine which square the user clicks.

Osku

harryx
02-13-2002, 04:50 AM
You could just use the selection buffer, thats what its there for...

bleach
02-14-2002, 05:06 AM
Hi,

the problem with using read pixels is that the squares are empty, they are just bounded by cylinders, so you can see the lower levels through each square should you rotate the grid that way.

cheers

bleach



[This message has been edited by bleach (edited 02-14-2002).]

Eric
02-14-2002, 05:29 AM
bleach,

If I understand you correctly, you have the correct approach. Here is how to do what you are looking for:

1) Get X,Y user input
2) Use gluUnproject with Z=0 (gives X1,Y1,Z1)
3) Use gluUnproject with Z=1 (gives X2,Y2,Z2)
4) Build Vector V=P2-P1
5) Now you have a vector that you can use to see which block has been clicked by the user.

For what you want to do, you don't need to read Z from the z-buffer.

Regards.

Eric

bleach
02-14-2002, 05:35 AM
Fantastic!

thank you.


bleach