Jump to content
  • Advertisement
Sign in to follow this  
SeraphLance

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

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!