Resolution independent physics

| | August 10, 2015

I’m making a game like Doodlejump but don’t know how to make the physics scale on multiple resolutions. I also can’t find anything related to this on Google. Right now I’m scaling the game using letterboxing and tested scaling the jump height with this code:

gravity = graphics.getHeight() * 0.001f;
jumpVel = graphics.getHeight() * -0.04f;

...

velY += gravity;
y += velY;

But if I test this on my smartphone or emulator with different resolutions, I always get a slightly different jump height. I know that Farseer is resolution independent. How can I replicate this in my game?

Thanks in advance.

One Response to “Resolution independent physics”

  1. You don’t scale your physics. Your physics should be based in an arbitrary coordinate system, completely independent of the device.

    Then when you do your drawing, you translate your “physical” location to a screen location based on the screen resolution. You need to calculate the scaling amount based on the screen resolution.

    For example, if you define your arbitrary world size as 1200 “units” wide, then on a device with a 1920 pixel wide screen, your view scale is: 1920/1200 = 1.6

    So for example, gravity and jump velocity would simply be:

    gravity = 0.001f;
    jumpVel = -0.04f;
    
    viewScale=graphics.getWidth()/WORLD_WIDTH;
    

    Then when you draw your object, you’d use:

    screenX=worldX*viewScale;
    screenY=worldY*viewScale;
    

Leave a Reply