# Screen size independent movements

I am developing a simple game for Android.

It is a really simple game, with only one object - a ball.

I am using the game loop based on delta time. However I have noticed that my game runs not in the same on different devices and I have realized that this may be caused by the following code

currentPosition.x+= velocityVector.x * deltaTime


As far as all dimensions are just simple pixels, as a result it runs slower on devices that have higher screen resolution as the ball should pass more distance with the same velocity vector.

So to conclude, could you please suggest any good algorithm or approach to implement independent movement of an object ?

Thanks.

• Are you using a framework? If not you should consider using one like LibGDX since it will make your life a lot easier by, for example, handling these sort of things for you. – Charanor Oct 23 '17 at 16:17
• muktiply by the width of the screen – Bálint Oct 23 '17 at 16:27

This is usually handled by decoupling the game's simulation space from the screen pixels.

Your game physics and logic then operate in a "world coordinate system" of arbitrarily-chosen measurement units (1 world unit = 1 tile width or 1 in-game meter are common choices) which represent the ground truth of what's happening in the game's universe.

Then displaying a view of this world on the player's screen becomes a matter of transforming from world coordinates to screen or pixel coordinates. Many such schemes are possible, depending on whether your game is top-down or side-scrolling, or some form of 2.5D / oblique / isometric perspective. What's great about this decoupling is that you can even support multiple display perspectives for different situations without re-implementing your game logic.

A simple display scheme is something like this:

Vector2 ToScreenPoint(Vector2 worldPoint) {

Vector2 relativeToView = worldPoint - currentView.worldspaceCenter;

// Scale by a translation factor that maps some controllable number
// of world-space units to always match the full height of the screen.
Vector2 pixelOffset = relativeToView * Screen.pixelHeight / currentView.worldHeight;
// (You could also choose to match the width instead, or some compromise formula)

// Add (Screen.pixelWidth, Screen.pixelHeight)/2 to convert into a range
// from 0 to the screen's extents. Now we have a pixel position.
return pixelOffset + Screen.center;
}


Note that if your game uses pixel art or needs pixel-perfect display, then you may need to make additional changes to your world-to-screen scaling logic or visual asset selection to keep it looking crisp on different resolutions. Pixel art does not handle arbitrary scales cleanly.

(As an aside: I think this question has been asked and answered before, but the keywords show up in so many other questions that I haven't been able to find the match. Please feel free to suggest duplicates if you find one)

• Thank you so much for the answer !! Appreciate it, If you have any other good references, I would grateful if you share them – solderingiron Oct 23 '17 at 21:42
• I'm kind of known for spamming people with gobs of gamedev reading/watching links - a firehose that won't fit well in the comments here. ;) Your best bet is to check through previous answers here from myself and other users (a number of us are active on Twitter too if you want general gamedev musings & links), or if you don't find what you're looking for there, ask a new question specific to the issue you want to learn about. – DMGregory Oct 23 '17 at 21:49
• Thank you for the answer. One last request, could you please add an example for 2D game with the camera viewpoint ? Thanks – solderingiron Oct 24 '17 at 8:18
• I'm not sure if I understand the question. Are you looking for an example of an existing 2D game that uses a camera? Any side-scroller or vertical scrolling platformer would fit the bill (less so infinite runners, though they looks similar, since they often hold the viewpoint constant and scroll the world & obstacles instead). Or are you looking for a code example of how to achieve a particular style of viewpoint? If it's the latter, there's a good chance that question already has a more detailed answer elsewhere on this site - describe what you're looking for and we can help find it. – DMGregory Oct 24 '17 at 12:55
• I don't need a camera in my game at all. It is a simple 2D game without any camera movement. Just the screen with a bouncing ball, how can I apply your technique than? Just throw all camera related stuff ?) – solderingiron Oct 24 '17 at 17:09