# [solved]Projecting 3D coordinates onto a 2D screen

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

## Recommended Posts

Is there any standard procedure of doing this? I need to figure out how because I want my camera to zoom in and out to keep all objects in the screen at the highest possible magnification. However, my game isn't top-down, so different parts of the plane in which the objects are ordered are different distances from the screen. I honestly have no idea where to even start in this case. My gut tells me it has something to do with the view and/or projection matrices, but I have no idea how to utilize them for this, or even if that's the correct direction to go at all. I'm using XNA if it matters, but as far as I know the method should at least be the same on all DirectX-derived platforms (and possibly OGL platforms). [Edited by - SeraphLance on July 7, 2008 9:21:57 PM]

##### Share on other sites
Do you mean the perspective divide?

Like this? : (Pardon my C++, I don't use it often)

int main(){//Screen dimensionsint screenWidth  = 640;int screenHeight = 480;int angle        = 40 ;//Just some 3d pointint xCoord = 34 ;int yCoord = 57 ;int zCoord = 300;//The 2d point we wish to findfloat xPix;float yPix;//Pifloat pi = 3.14159;/*Scaling for a good projection, otherwise    we'd get weird results */float scaleWidth  = screenWidth  / tan(angle * pi / 180);float scaleHeight = screenHeight / tan(angle * pi / 180);//calculate projection to screenxPix = xCoord * scaleWidth  / zCoord;yPix = yCoord * scaleHeight / zCoord;//Draw Pixelpset(xPix, yPix);}

##### Share on other sites
That's just what I was looking for. I was making it harder than it had to be by not transforming the world according to my matrices.

Turns out that XNA has a helper function for just this (matrix inputs included) but it helps to understand the math behind it.