# converting 2D screen coordinates to a 3D world vector

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

normally, gluUnProject can do that, and there may be other things.

JD

Yes, you can use ReadPixels to get the
depthcomponent ( z ) & use that with
glUnproject to get the x,y,z worldcoords.

cheers

Hello

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

Osku

You could just use the selection buffer, thats what its there for…

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).]

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

Fantastic!

thank you.

bleach