Advertisement Jump to content
Sign in to follow this  

2D objects in a 3D world - How to position them?

This topic is 3186 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

Hey there. I am building a 2D game (where the camera is in the top looking down) in 3D. Currently there is a problem though. I want to make sure the objects I place stay between X 0 - 1024, and Y 0 - 768 (in pixel coordinates). How can I convert matrix position coordinates into pixel coordinates? [Edited by - Mathy on May 2, 2010 4:10:39 PM]

Share this post

Link to post
Share on other sites
Does your game run at a fixed resolution? Is there any particular reason that the pixel coordinates need to exactly match the world coordinates?

Either way though, you should be able to set up your camera so that the area of interest (whatever it might be) exactly fills the viewport.

The camera aspect ratio should match that of the viewport, of course, and the near and far plane distances should be set as appropriate. That leaves field of view (vertical, presumably), and the camera transform. Presumably the camera is oriented so that it is looking straight down at the play area with the world axes aligned rectilinearly in view space, which leaves distance to the play area as the only variable.

So now you just need to get the field of view and the camera distance to match. For field of view, just set it to whatever value you prefer. The camera distance can then be computed from the field of view using some simple trig (off the top of my head, so no guarantee of correctness):
float theta = fov * .5;
float halfHeight = desiredPlayAreaHeight * .5; // desiredPlayAreaHeight would be 768 in your example

// A little trig...
// tan(theta) = halfHeight / cameraDistance
// cameraDistance*tan(theta) = halfHeight
// cameraDistance = halfHeight / tan(theta)

// And we're done:
cameraDistance = halfHeight / tan(theta);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!