# Screen To World Coordinates

5 replies to this topic

### #1SpreeTree

Valued Member

• Members
• 265 posts

Posted 02 February 2005 - 12:22 PM

Im have been looking for information on coverting screen coordinates to world coordinates (in DirectX, but I'm mainly looking for API independant info). I have found a tutorial for converting them in DirectX (Clicky) but it seems to not work. (The code produces points in world space, but they dont line up with where im clicking at all... unless I'm directly in the centre of the world!)

Does anyone have any pointers/advice on this, as it seems to be a topic thats a bit hard to come by via Google...

Thanks
Spree

DevMaster Staff

• Members
• 1057 posts

Posted 02 February 2005 - 03:15 PM

this seems pretty straight forward -> http://www.toymaker....ml/picking.html

### #3anubis

Senior Member

• Members
• 2225 posts

Posted 02 February 2005 - 03:21 PM

basically you just need to transform your screen space coordinates to normalized coordinates (-1 to 1 with the origin at the center of the viewport).

half_width = width * 0.5f
half_height = height * 0.5f
aspect_ratio = width / height;

dx = (x / half_width - 1.0f) / aspect_ratio;
dy = 1.0f - y / half_height

then you need to find the rotation axis (axi ? axen ?) (this is just for clarity... one can obviously do better than this)

m = camera_matrix;

xr = m * Vector3(1.0, 0.0, 0.0);
yr = m * Vector3(0.0, 1.0, 0.0);
zr = m * Vector3(0.0, 0.0, 1.0);

xr *= aspect_ratio;
zr *= -(0.5 * aspect_ratio * tan(0.5 * field_of_view));

finally you can compute a ray through the normalized coordinates computed earlier :

ray = (m.GetTranslation(), dx * xr + dy * yr + zr);
If Prolog is the answer, what is the question ?

### #4SpreeTree

Valued Member

• Members
• 265 posts

Posted 02 February 2005 - 08:23 PM

OK, I think I am finallly there...

I took someones advice and used the D3DX Unproject functions to get a head start, and now I have it workign without these functions so thanks guys :)

Its not API free yet, as my Matrix Inverse function is a bit faulty at the mo, so I'm still having some trouble!

Thanks for the help guys

Spree

### #5Revolver

New Member

• Members
• 16 posts

Posted 10 February 2005 - 08:26 AM

Just reverse the process that takes the point from world to screen space... not too hard eh?

I.e., instead of World->View->Clip->Screen go Screen->Clip->View->World.

DevMaster Staff

• Members
• 1057 posts

Posted 10 February 2005 - 10:08 AM

its not as simple as that. You also have to take into account that when you go the reverse process, you're missing the z-coordinate.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users