Skip to main content
GameDev.net gamedev.net
🔒 Locked

world space to screen space co-ordinates

Started by ThunderSoul Jun 20, 2010 at 5:02 PM 4 replies 3.8k views
Original Post
ThunderSoul
ThunderSoul
Hi guys!

I'm looking for some help on transforming world space co-ordinates to screen space co-ordinates as opposed to the usual screen space to world space. Does anyone know how to do this?

Thank you,
-TS...
Erik Rufelt
Erik Rufelt
Multiply the point by the world, view, and projection matrices. What API are you using?
You could use the D3DXVec3Project function in D3D, or Viewport.Project in XNA, or just multiply the point by the matrices yourself using the appropriate functions.
DensitY
DensitY
if you have your Camera's viewprojection matrix its simply

LocalSpace = viewproj * worldcoord (vertex by matrix multiply)

screenX = ((LocalSpace.x / LocalSpace.z) * (screenWidth *0.5f)) + (screenwidth * 0.5f)
screenY = -((LocalSpace.y / LocalSpace.z)* (screenHeight *0.5f)) + (screenHeight * 0.5f)

that'll get it to pixel position.
ThunderSoul
ThunderSoul
Ah, thank you very much both... I'll use both techniques to see which one I'll be most comfortable with... To answer Erik's question, I'm using C# to code it in... I browsed the forum prior to posting my question, but I saw solutions for C/C++...
Thanks again! :)
ThunderSoul
ThunderSoul
So another question...
I noticed XNA's Viewport.Project returns a vector 3... That's rather strange because screen co-ordinates have only 2 attributes, namely the X and Y co-ordinates. Needless to say Viewport.Project fails to accomplish what I'm looking for... I'm new to XNA, so perhaps I'm missing something... Does anyone know?

Thanks...
-TS...
SiCrane
SiCrane
It's a Vector3, but it's still in screen coordinates. X and Y will still make sense to you. The Z coordinate is the depth value in case you need it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.