Math Problem: Placing Camera In World (Screen Res)

Hello,

I am designing a game for mobile devices (iPhone and Android). I am trying to come up with a formula that will allow me to place the main camera in the world so that what it sees will look the same on different mobile devices with different screen resolutions.

Essentially, I have a cube which has a fixed width, height and depth. I need the camera to look on one side of the cube which is actually a room in my game. There is no rotation on the camera and it looks straight ahead at the cube. At the moment I set the Y axis of the camera so that it is half the height of the cube (i.e. 25.0f). I need to know how to calculate how far back to place it on the Z axis so that the face of the cube fills the screen at different screen resolutions.

Please see the linked picture below for a rough visual attempt at the problem:

http://i1213.photobucket.com/albums/cc466/rwilson1982/CameraProblem.jpg

I would like to minimise any black borders down the left and right of the screen if possible.

Is someone mathmatically inclined able to help me out :slight_smile: ?

1 Answer

1

I find, the easiest way to keep things the same across resolutions is to think of it in percentages,

50% of 1024 will look the same as 50% of 1280. This can be achieved a few different ways, for example to get 50%, you (brace yourself) need to /2. However, when you start getting to some more complex positioning, you will have to find a way to divide it so that YOU know the percentage. This site should help you:

http://www.mathsisfun.com/converting-fractions-percents.html

The only problem with this, is when you start changing aspect ratios. The only way i have found to solve this is to check what resolution the player has picked and run code for that resolution(of course, you can have the same bit of code for several resolutions). Or just simply change the supported resolutions in the project settings, or don't provide a way for the player to change to an unsupported resolution.

On a note, when i was just starting out designing menus. I came across this problem and i actually found myself measuring my screen with a ruler, then measuring from one edge to the point where i wanted the GUI element ;).