# From 3D World to Screen Space

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

## Recommended Posts

I try to get screen coordinates from 3d.This code gives me big numbers like x=29892,y=2000.Where is the error in this code ?
	//Multiply the vertex by the world matrix:
//Vtx is of type D3DXVECTOR3
D3DXVECTOR4 vMinWorldTmp;
D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &WorldMatrix);

//Multiply it by the view matrix
D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &ViewMatrix);

//Multiply by the projection matrix:
D3DXVec3Transform(&vMinWorldTmp, &vMinWorld, &ProjectionMatrix);

//Convert it:
vMinWorldTmp.x = vMinWorldTmp.x * SCREEN_WIDTH/2 + Position.x + SCREEN_WIDTH/2;
vMinWorldTmp.y = -vMinWorldTmp.y * SCREEN_HEIGHT/2 + Position.y + SCREEN_HEIGHT/2;

//Multiply the vertex by the world matrix:
//Vtx is of type D3DXVECTOR3
D3DXVECTOR4 vMaxWorldTmp;
D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &WorldMatrix);

//Multiply it by the view matrix
D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &ViewMatrix);

//Multiply by the projection matrix:
D3DXVec3Transform(&vMaxWorldTmp, &vMaxWorld, &ProjectionMatrix);

//Convert it:
vMaxWorldTmp.x = vMaxWorldTmp.x * SCREEN_WIDTH/2 + Position.x + SCREEN_WIDTH/2;
vMaxWorldTmp.y = -vMaxWorldTmp.y * SCREEN_HEIGHT/2 + Position.y + SCREEN_HEIGHT/2;
sprintf(buffer,"X= %f , Y= %f ,X2= %f , Y2= %f ",vMinWorldTmp.x,vMinWorldTmp.y,vMaxWorldTmp.x,vMaxWorldTmp.y);



##### Share on other sites
Hi there Quixotic,
How are you doing?

[The Spaces]
To better equip ourselves with a decision we have to see where these spaces fit in. Think of them as steps.

Object Space : this is where vertices are defined
World Space : We go to World space from object space with a world transformation matrix
Camera (View) Space : We go here from world space, through a view transformation matrix
Clip (Projection) Space : We go here from view space through a projection
matrix
Screen Space : We go here from the Clip space.

[Where does Screen Space come in?]
Screen space is represented by your display, by default it is set to the entire display. Whenever you want to move back up the steps you use inverse matrices.

D3DXMatrixInverse Function
Calculates the inverse of a matrix
You use this function on the matrix and it returns a matrix that you can use as a step back and just transform your vertices with it :)

Hope this helps.

##### Share on other sites
so where is the error?

##### Share on other sites
Quote:
 Original post by TheQuixotiCso where is the error?

He is just trying to make sure that you have performed all the transformations correctly. There are quite a few steps and many places for something to go wrong.

• ### What is your GameDev Story?

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

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×