• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
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:
[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]

Share this post

Link to post
Share on other sites

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

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]

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...)


Share this post

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

Share this post

Link to post
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:
D3DXMATRIX view_matrix, proj_matrix, world_matrix;
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 );
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;

[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]

Share this post

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

Share this post

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

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));

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0