Public Group

# [XNA 4.0] Calculating 2D (screen) coordinates from 3D coordinates

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

I have a program that draws some items to the scene by loading their positions from a file. Now I want to place a Ray on the same location where the items are drawn. So my question is how can I calculate the position of the ray (it's 2D components) by using 3D coordinates of each particular item? The items don't move anywhere, so once they are placed they stay until the end of the programs execution.
Thanks.

##### Share on other sites
What do you mean by 2D components? A ray is 3D too.

##### Share on other sites
uh... just use x and z... that should be 2d.. viewed from above

##### Share on other sites

uh... just use x and z... that should be 2d.. viewed from above

X and Z are in no way the screen coordinates of that object's world-space coordinates. The full 3D point would need to be multiplied by the view * projection matrix to be converted to screen-space, which would then need to be compared to the viewport's width and height to get actual viewport coords.

NDraskovic, are you talking about picking? Viewport.Unproject would let you convert the mouse coords in your viewport into an origin for a 3d ray, but if you want to do the reverse you're essentially following the matrix multiplication that happens in the first place (transforming world-space to screen-space).

Edit: this forum thread might be of interest. Edited by BCullis

##### Share on other sites
sorry didn't got the question right then... my bad..

this is how its done for screen coords

 Dim screenSpace As Vector3 = Device.Viewport.Project(pos, camera.projectionMatrix, camera.viewMatrix, Matrix.Identity) Dim Pos3DOnScreen As Vector2 = New Vector2(screenSpace.X, screenSpace.Y) 

where pos is your 3d position of the object. code is in vb.net

##### Share on other sites
the only way i know how to do it is through linear alebra /graphics theory, putting the world space into the camera's coordinates (if you have a matrix of the cameras frame, inverse it and mulitply it onto any world coordinates, this case the objects world coordinates.)

Now it depends on the camera's projection; if its orthographic you can just use the x, y coordinates ( in cameras frame); else you will have to divide x y by the distance (z in camera coordinates)

This is a more overview of it, i think winsrp described it in some kind of library i am unaware of

##### Share on other sites
I managed to solve this particular problem with Viewport.Project method (although it didn't solve my original problem). Thanks anyway guys

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×