Followers 0

# 3d position to 2d position

## 8 posts in this topic

I want to convert a 3d position to a 2d screen coordinate. But i'm not really sure how to do that.

I know i need the view, projection and world matrix for the calculations. So i managed to get those. But what next? How can i calculate the 3d position of an object to a 2d screen position?

This is how i get the matrices:
[code]
[size="2"]D3DXVECTOR3 vector_2d, vector_3d(0,0,0);[/size]
[size="2"]D3DXMATRIX view_matrix, proj_matrix, world_matrix;[/size]
[size="2"]D3DVIEWPORT9 d3dvp;[/size]

[size="2"]dev->GetTransform( D3DTS_VIEW, &view_matrix );[/size]
[size="2"]dev->GetTransform( D3DTS_PROJECTION, &proj_matrix );[/size]
[size="2"]dev->GetTransform( D3DTS_WORLD, &world_matrix );[/size]
[size="2"]dev->GetViewport( &d3dvp );[/size]
[size="2"]D3DXVec3Project( &vector_2d, &vector_3d, &d3dvp, &proj_matrix, &view_matrix, &world_matrix );[/size]
[size="2"]D3DXVec3Unproject( &vector_3d, &vector_2d, &d3dvp, &proj_matrix, &view_matrix, &world_matrix );[/size]
[size="2"][/code][/size]
0

##### Share on other sites
Aloha,

[quote name='iedoc' timestamp='1325737900' post='4899844']
Finally, after you have all three matrices, you can create the WVP, or world view projection matrix:

[code]D3DXMATRIX WVP;
WVP = World * View * Projection;[/code]

The WVP matrix is the objects position in screen space. As i mentioned,above, screen space's x and y are between -1 and 1, and z is 0 to 1. If your looking to turn this into 0 to the width and height of your screen, you will have to do an extra calculation, which i will show you in a minute. Now that we have the WVP, we can get the objects screen space position like this:

[code]D3DXVECTOR3 screenCoord = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // Position of object in screen space
D3DVec3TransformCoord(&screenCoord, &screenCoord, &WVP);[/code]

So now how do we turn the screen space from -1 to 1, to 0 to the width and height of your window? easy, just add 1 to x and y, divide it by two, and multiply it by the screen width or height, like this:

[code]screenCoord.x = ((screenCoord.x + 1.0f) / 2.0f) * Width;
screenCoord.y = ((screenCoord.y + 1.0f) / 2.0f) * Height;[/code]
[/quote]

This is exactly what D3DXVec3Project does. But for starters it is nice to see what is actually going on behind the scenes, so thumbs up for the time you spend. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Besides, if you do those transformations in shaders you have to do it manually anyway.

Oh, and don't forget the flipping of the y coordinate, since the origin of the window is in a different corner:
[code]screenCoord.y = ((-screenCoord.y + 1.0f) / 2.0f) * Height;[/code]
(Only necessary in D3D, not in GL as a side note...)

Greetings!
2

##### Share on other sites
oh my bad! thanks for mentioning the flipping of y in screenspace!

i'm not going to lie, i wasn't even aware of D3DXVec3Project, haha, so thanks for that too, i'm pretty sure that's exactly what he's looking for, no sense recreating the wheel ;)
0

##### Share on other sites
Thanks alot guys. Thanks for the indept explanation, very much appreciated!
0

##### Share on other sites
Hi i still have a question. I though i had everything working but for some reason the screen coordinates are wrong.

This is my function:
[code]
D3DXMATRIX view_matrix, proj_matrix, world_matrix;
D3DVIEWPORT9 d3dvp;
D3DXVECTOR2 screen;

m_pD3Ddev->GetTransform( D3DTS_VIEW, &view_matrix );
m_pD3Ddev->GetTransform( D3DTS_PROJECTION, &proj_matrix );
m_pD3Ddev->GetTransform( D3DTS_WORLD, &world_matrix );
m_pD3Ddev->GetViewport( &d3dvp );
D3DXMATRIX WVP;
WVP = world_matrix * proj_matrix * view_matrix;

// Position of object in world
D3DXVECTOR3 screenCoord = D3DXVECTOR3(pos.x, pos.y, pos.z);

// Position of object in screen space
D3DXVec3TransformCoord(&screenCoord, &screenCoord, &WVP);

screenCoord.x = ((screenCoord.x + 1.0f) / 2.0f) * d3dvp.Width;
//screenCoord.y = ((screenCoord.y + 1.0f) / 2.0f) * d3dvp.Height;s
creenCoord.y = ((-screenCoord.y + 1.0f) / 2.0f) * d3dvp.Height;

// Put coords back in vector2s
creen.x = screenCoord.x;
screen.y = screenCoord.y;

return screen;
[/code]

[size=4]I position in my game is:
x:1211.037354 y:-411.112854 z:232.125015

The screen result is:
620563.125000 y:158251.343750[/size]

[size=2]Which i weird, because the object was exactly infront of me. Any idea where it goes wrong?[/size]
0

##### Share on other sites
The transformation order should be:
WVP = world_matrix * view_matrix * proj_matrix;

You can compare the result to what D3DXVec3Project is giving you. It should yield the same.
0

##### Share on other sites
need to divide by the w coordinate also, as in:

[code]
vec4f v4Position(v3ObjectPos.x, v3ObjectPos.y, v3ObjectPos.z, 1.f);
v4ScreenPosition = v4Position * WorldViewProjectionMatrix;
v4ScreenPosition.x /= v4ScreenPosition.w;
v4ScreenPosition.y /= v4ScreenPosition.w;
v4ScreenPosition.x = fScreenWidth * (v4ScreenPosition.x+1.0f)/2.f;
v4ScreenPosition.y = fSreenHeight * (1.0f-((v4ScreenPosition.y+1.f)/2.f));
[/code]
0

##### Share on other sites
try setting the world matrix to the indentity
0

## Create an account

Register a new account