Handmade Hero » Forums » Code » Questions around moving from world space to camera space to screen/backbuffer space and aspect ratio.
swerves
Casey Irvine
2 posts
#12167 Questions around moving from world space to camera space to screen/backbuffer space and aspect ratio.
3 months, 2 weeks ago

Let's say I have a 2D game world that is 1000 meters x 1000 meters and I want to display a 100 meter x 100 meter portion of the world on the screen in 1920x1080 resolution. I'm having trouble understanding how aspect ratio factors into changing between world, camera and screen/backbuffer coordinate systems. I'm also confused on the calculations necessary to "zoom out" the camera to see a larger portion of the world.

Here is my thought process:

My basis for the world would be u = [1, 0], v = [0, 1], origin = [0, 0].
Let's say that the camera is currently centered at the world origin and it has the same basis as the world.
This means that I have different values for pixels/meter for the width and the height, but I don't think that is correct.
Specifically, widthPixelsPerMeter = 1920/100 and heightPixelsPerMeter = 1080/100
The basis for the screen would be u = [widthPixelsPerMeter, 0], v = [0, -heightPixelsPerMeter], origin = [-50, 50].

Do I need to do something with the aspect ratio here so that pixels/meter is consistent for the width and the height?

If I wanted to "zoom out" the camera so that the player could see a 200 meter x 200 meter portion of the world, would I change the camera basis to be u = [1/2, 0], v=[0, 1/2], origin = [0, 0]. Is that correct?

Thanks in advance for the help. I've only gotten through the first 130 days and I had particular trouble following how this part of rendering was set up.
swerves
Casey Irvine
2 posts
#12168 Questions around moving from world space to camera space to screen/backbuffer space and aspect ratio.
3 months, 2 weeks ago

After thinking about this I think I can answer my own question.

Trying to render dimensions with one aspect ratio in another aspect ratio will stretch or squish the image in some way. So, when my camera views a portion of the world that is 100 x 100 the aspect ratio is 1:1 and that view has to change to fit a 16:9 aspect ratio. So my dimensions for what my camera can view could be 160 x 90 or some other dimensions that are 16:9.

When I think about it, "zooming out" the camera really just means the camera can see more of the world. So changing the camera basis is definitely not the way to go conceptually, but I think you could still get the desired result. Instead I should increase the dimensions that my camera can see while maintaining the right aspect ratio.

Now it makes sense to me that if we want to have smooth zooming, we need to calculate the dimensions the camera can see based on the distance the camera is from the world/scene. I'll have to re-watch some videos to remember how to calculate the initial camera distance. Unless someone is nice enough to post the formula for me :)