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

Started by
1 comment, last by Mathy 13 years, 11 months ago
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]
Advertisement
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);
Nice! Thanks! Rating for you!

This topic is closed to new replies.

Advertisement