I'm playing around with Box2D and TiledMaps and I've come across this minor annoyance.


As you can see, both Box2D and TiledMap render at the center of the screen. This forces me to compensate by adding .5 to every item I spawn in the Box2D world, giving me some problems down the road.

How do I change the origin of these two things? Maybe it has something to do with the camera?


3 Answers 3


I use and extended OrthographicCamera to fix this.

package ferret.util;

import com.badlogic.gdx.graphics.OrthographicCamera;

 * OrthographicCamera that cannot go beyond specified rectangle.
public class BoundedCamera extends OrthographicCamera {

    private float xmin;
    private float xmax;
    private float ymin;
    private float ymax;

    public BoundedCamera() {
        this(0, 0, 0, 0);

    public BoundedCamera(float xmin, float xmax, float ymin, float ymax) {
        setBounds(xmin, xmax, ymin, ymax);

    public void setBounds(float xmin, float xmax, float ymin, float ymax) {
        this.xmin = xmin;
        this.xmax = xmax;
        this.ymin = ymin;
        this.ymax = ymax;

    public void setPosition(float x, float y) {
        setPosition(x, y, 0);

    public void setPosition(float x, float y, float z) {
        position.set(x, y, z);

    private void fixBounds() {
        if(position.x < xmin + viewportWidth / 2) {
            position.x = xmin + viewportWidth / 2;
        if(position.x > xmax - viewportWidth / 2) {
            position.x = xmax - viewportWidth / 2;
        if(position.y < ymin + viewportHeight / 2) {
            position.y = ymin + viewportHeight / 2;
        if(position.y > ymax - viewportHeight / 2) {
            position.y = ymax - viewportHeight / 2;


then in the Game Class or what ever you call it.

You do:

BoundedCamera cam = new BoundedCamera();
cam.setBounds(0, tileMapWidth * tileSize, 0, tileMapWidth * tileSize);

Then in your render method:

cam.setPosition(player.getPosition().x * SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2);//Set this to your own liking.

tileMapRenderer.setView(cam);//Assuming your are using a tmxloader


One thing you gotta watch out for is, wether or not you are using "Pixels Per Meter" scaling. Then you should adjust accordingly.

Requested EDIT: This should work because you use the camera as draw origin. Everything x-y related which involves the SpriteBatch will be calculated from the camera if you do this properly.


I had similar problem with zoomed in rendering. I assume by "Box2D" render you mean Box2D debug renderer. Well, here is your problem. Before rendering debug data of Box2D, transform camera accordingly (add 0.5 or whatever, as you said) and after rendering Box2D debug stuff do the inverse transformation (subtract 0.5).

I hope I understood your problem and my suggestion will solve that ;)


I edited my camera matrix to convert the origin of libgdx and debugRenderer to lower left..

    static final float BOX_TO_WORLD = 100f;

        camera.position.set(SCR_WIDTH / 2, SCR_HEIGHT / 2, 0);
        debugMatrix = new Matrix4(camera.combined);
        debugMatrix.translate((-SCR_WIDTH / 2), (-SCR_HEIGHT / 2), 0);
        debugMatrix.scale(BOX_TO_WORLD, BOX_TO_WORLD, 1f);
        debugRenderer = new Box2DDebugRenderer();

        Gdx.gl.glClearColor(0, 0, 0, 1);



        debugRenderer.render(world, debugMatrix);
  • 1
    \$\begingroup\$ This might work, but doesn't explain why. \$\endgroup\$
    – Anko
    Jul 28, 2014 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .