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

1. ## 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

2. ## Re: converting 2D screen coordinates to a 3D world vector

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

JD

3. ## Re: converting 2D screen coordinates to a 3D world vector

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

cheers

4. ## Re: converting 2D screen coordinates to a 3D world vector

Hello

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

Osku

5. ## Re: converting 2D screen coordinates to a 3D world vector

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

6. ## Re: converting 2D screen coordinates to a 3D world vector

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

7. ## Re: converting 2D screen coordinates to a 3D world vector

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

8. ## Re: converting 2D screen coordinates to a 3D world vector

Fantastic!

thank you.

bleach

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•