# world to screen dimensions

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

## Recommended Posts

Hi all, I'm having a little bit of trouble working out how to find the relationship between world co-ordinates and screen dimensions. Basically what I'm trying to do is make a sprite demo where an alien ship flies across one edge of the screen and ends up at the opposite edge. (For example, the ship flies across the top edge of the screen, and ends up at the bottom of the screen) The way I was going about doing this is trying to convert world coordinates into screen dimensions, testing whether y < 0 and then converting the bottom coordinate (say 600) back into world coordinates with the same z value, and giving the ship the new position at the bottom of the screen (the demo is in 3d but looking overhead like 2d.) I'm wondering if there's any easy way of doing this? 1. Does GetClientRect(hMainWnd, &rect) produce the screen dimensions? 2. Are there D3D matrices that can convert between world coordinates and screen coordinates and vice versa? 3. Is there a better way of doing this? edit: I was looking through the directx documentation and found D3DXVec3Project. Maybe this is what I need? Thanks for any input! Much appreciated!

##### Share on other sites
Your current approach won't really work in any robust way so yes, you want to be using D3DXVec3Project() - it's pretty much designed for this [cool]

Alternatively, you could look into using projection space for your geometry - you get all the benefits of the pipeline (that is, you don't disable transformations like you would for pre-transformed data) and it's resolution-independent 2D. Simply set your matrices to identity and define your geometry in the [-1,+1] coordinate range.

hth
Jack

##### Share on other sites
I'm trying to use D3DXVec3Project now with the following code:

D3DXMATRIX proj, view, world;
D3DVIEWPORT9 vp;
D3DXVECTOR3 result;

gd3dDevice->GetViewport(&vp);
gd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);
gd3dDevice->GetTransform(D3DTS_VIEW, &view);
gd3dDevice->GetTransform(D3DTS_WORLD, &world);

D3DXVec3Project(&result, &mShipPos, &vp, &proj, &view, &world);

mShipPos is a D3DXVECTOR3 holding the ship's x, y and z world coordinates. I get strange results:

result.x = 796, result.y = 1.69873e-005 and result.z = 1

x never seems to change from 796 regardless of the ships position, and y sometimes turns into 0.

Am I using this function correctly? I've read that it wont work with a pure device, and if that is the problem, is there any way around it? Why wouldn't it work with a pure device?

##### Share on other sites
Quote:
 Original post by KMDAm I using this function correctly? I've read that it wont work with a pure device, and if that is the problem, is there any way around it? Why wouldn't it work with a pure device?
Because GetTransform() will fail on a pure device, so your matrices will be garbage.

Make sure you're using the Debug Runtimes to help catch errors like this.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 12
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015657
×