# Calculating 2D (screen) coordinates from 3D positions in XNA 4.0

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.

-

You can get the screen-space co-ordinates using Viewport.Project:

``````Vector3 screenSpace = Viewport.Project(Point, ProjectionMatrix, ViewMatrix, WorldMatrix);
``````

There is also Viewport.Unproject if you want to go the other way.

-
Yes, I already have a picking algorithm in my project, but that works the other way (I pass the mouse coordinates and get the Vector3 value). Thanks for your answer I'll try to implement it – NDraskovic Jul 11 '12 at 10:27
Unfortunately this didn't help me. I have a problem that I'm not able to solve (this is one different option I tried). What happens is that I have a several items on my scene and only one is movable. I place the BoundingSpheres (and they are placed correctly) but when I move the one movable item, all BoundingSpheres move with it (of course then they are not where they should be, and I never get the collision I need). Do you know whats causing this and what would be a good solution? – NDraskovic Jul 11 '12 at 10:43
What matrices are applied to the bounding spheres? It sounds like you're applying the moving objects' matrix to all bounding spheres, rather than just the moving object. – Matt Kemp Jul 11 '12 at 11:03
That's what I thought, but I don't know how to change the matrix that is applied to bounding spheres. Indeed I have 2 matrices that are identical in value, but I only change the one that is applied to the moving object. How can I apply the stationary one to the bounding spheres? – NDraskovic Jul 11 '12 at 11:09
Maybe you should update your question (or start a new one) with a few more details - it would be interesting to see the code for how you're drawing the bounding spheres and why they're moving. – Matt Kemp Jul 11 '12 at 12:34