# Scaling and new coordinates based off screen resolution

I'm trying to deal with different resolutions on Android devices.

Say I have a sprite at X,Y and dimensions W,H.

I understand how to properly scale the width and heigh dimensions depending on the screen size, but I am not sure how to properly reposition this sprite so that it exists in the same area that it should.

If I simply resize the width and heigh and keep X,Y at the same coordinates, things don't scale well.

How do I properly reposition? Multiply the coordinates by the scale as well?

-

## 1 Answer

You are looking for something along the lines of Resolution Independant Rendering. There is a similar question in that link, as well as a great article on the subject.

The article and question is using XNA, but the concept is sound. Essentially, you have a "Real" resolution and a "Virtual" resolution. The Real resolution is the one you are working with to create your game. You pick it once, and don't change it. Your Virtual resolution is your "final" resolution. It's the one you need to scale to achieve the "perfect fit". You can either have your device scale it (by stretching, which may look ugly) or add letterboxing and/or pillarboxing to adjust to the new resolution without stretching.

Alternatively, you need to figure out an offset base on your size. Something like "if I increase the scale by 20, I need to move it 10 pixels to the left, and 10 pixels down". That version involves a bit more trial and error, but it may work better, depending on your game.

I'm not sure if there is a concrete equation out there for "if you increase by this, move by this". I've only done something that way once, and it involved me working with the numbers and offsets until I got something that worked.

(For the record, my magic numbers were "if I decrease width by 10, I increase position by 5. The opposite for increasing width. Works with both height and width.)

Further, you may want to check out Adaptive Resolution or this question regarding how to develop for different resolution on the Android platform.

-
newX += (originalWidth - newWidth)/2 ? – Atticus Nov 29 '11 at 0:01
@Atticus That equation definitely fits the data I have, and appears like it will work for everything (perhaps bar negative numbers). – electroflame Nov 29 '11 at 2:03