Sign in to follow this  
SeraphLance

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

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 this post


Link to post
Share on other sites
Do you mean the perspective divide?

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


int main()
{
//Screen dimensions
int screenWidth = 640;
int screenHeight = 480;
int angle = 40 ;

//Just some 3d point
int xCoord = 34 ;
int yCoord = 57 ;
int zCoord = 300;

//The 2d point we wish to find
float xPix;
float yPix;

//Pi
float 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 screen
xPix = xCoord * scaleWidth / zCoord;
yPix = yCoord * scaleHeight / zCoord;

//Draw Pixel
pset(xPix, yPix);
}

Share this post


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

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