Jump to content
  • Advertisement
Sign in to follow this  
NDraskovic

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!