In my game, I use Box2D. Now Box2D doesn’t use pixels as units, so instead, it uses meters.
I render two things in my game to keep the physics and the graphics separate. Graphics is in normal libgdx coordinates (lower-left origin) because I’m used to having it as that. Physics is in Box2D coordinates (center origin) because that’s what Box2D needs.
Everything seems fine, except that Box2D renders everything twice as big:
White line is the graphics renderer while the green lines are from the Box2D debug renderer
(Yeah I know it’s not centered but it’s on purpose)
//I set 60 pixels per meter
private static final int PIXELS_PER_METER = 60;
private Box2DDebugRenderer debug;
private OrthographicCamera camera;
//...
public ...() {
debug = new Box2DDebugRenderer();
//Convert window width and height to meters because that's what Box2D needs
camera = new OrthographicCamera(Maths.pixelsToMeters(Gdx.graphics.getWidth()), Maths.pixelsToMeters(Gdx.graphics.getHeight()));
//Just offset the camera, because in-game center isn't the window center; not sure if this is relevant
camera.position.sub(0f, Maths.pixelsToMeters(Gdx.graphics.getHeight() - Maths.IN_GAME_HEIGHT / 2f - Gdx.graphics.getHeight() / 2f), 0f);
camera.update();
}
//...
public static float pixelsToMeters(float p) {
return p / (float)(PIXELS_PER_METER);
}
//...
public void render(float delta, SpriteBatch batch) {
camera.update();
//...
debug.render(world, camera.combined);
}
Given that the cube above is 60 by 60 pixels, that would be 1 meter in Box2D units (println’s also say this). But every time it is rendered it shows up twice as big (120 by 120 pixels). It also said in the javadocs that OrthographicCamera takes in half a viewport, but that still doesn’t work.