libGDX unitscale when using box2d
libGDX unitscale when using box2d
When you learn about game programming, might be hear about unitScale whats that?. UnitScale is how we scaling or identify unit a world game with a scale phsyics, in a game development commonly we used a library like box2d for performing a phsyics to our game. But we must know box2d working with a real unit system, such as meter or kilograms, assume you have an assets actor with 32 x 32 px. The box2d will represent that assets 32 meter height, and if you using a real world gravity 9.8m/s thats will be heavy object for jumping. So the solution is we always working with PPM (pixel per meter) when using box2d, but dont forget to scaling with unitScale.
The common scale in libgdx is 1/16f or 1/32f, thats mean your assets will multiple by that number (ex : 32 px * 1/16f ).
mapRenderer = new OrthogonalTiledMapRenderer(map,1/16f);
but dont forget in this screen, where you used box2d the camera, viewport and asset must devide by 16f, if not you will see a small tiled render on your screen.
public static int VIRTUAL_WIDTH = 640;
public static int VIRTUAL_HEIGHT = 360;
float worldWidthUnitScale = Constant.VIRTUAL_WIDTH/16f;
float worldHeightUnitScale = Constant.VIRTUAL_HEIGHT/16f;
main.getCamera().setToOrtho(false,worldWidthUnitScale,worldHeightUnitScale);
main.getViewport().setWorldSize(worldWidthUnitScale,worldHeightUnitScale);
Remember, not all screen you must devide by 16. apply devide when a screen will render box2d object.