Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing my app on one device and want it to display at the same ratio on all other devices.

Currently what I have is 'image 1' in my image below - the game fits perfectly onto my development device's screen.

If I keep the viewport at the same ratio then I get the results seen in 'image 2'.

This is great, but now I need to center the viewport , and I'm not sure how to do that.

What I'm aiming for is what is in 'image 3' - just not sure how to get there.

This is how I'm keeping the viewport at the same ratio:

the ratio is width / height (this is 1.702127659574468 and we will use this explicitly)

//Set viewport size based on screen dimensions      
GLES20.glViewport(XVal, YVal, (int) (height * 1.702127659574468), height);

At the moment, XVal and YVal are both 0 - I need to change XVal to move the image along.

How do I work out the amount to move the viewport along by? (I know it will be equal to half of the space to the right of the viewport ('image 2') - just not sure how to work this out.......)

enter image description here

share|improve this question
up vote 2 down vote accepted
xoffset = (targetScreenWidth - image1Width)/2;
share|improve this answer
of course - it's so simple! I was thinking that I didn't know the width of the viewport (in pixels) but of course I'm setting it so I must know it lol. Thanks!!!! :-) – BungleBonce Apr 30 '13 at 21:05
It's in there somewhere I'm sure :) – UnderscoreZero Apr 30 '13 at 21:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.