Sign in to follow this  
Mathy

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

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

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