4
\$\begingroup\$

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

image

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?

\$\endgroup\$
1
\$\begingroup\$

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) {
        super();
        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);
        fixBounds();
    }

    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.
cam.update();

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

spriteBatch.setProjectionMatrix(cam.combined);
player.render(spriteBatch);

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.

\$\endgroup\$
0
0
\$\begingroup\$

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 ;)

\$\endgroup\$
0
\$\begingroup\$

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

    static final float BOX_TO_WORLD = 100f;
        //onShow()

        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();
        //onRender(delta)

        batch.setProjectionMatrix(camera.combined);
        camera.update();
        World_Update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();

        p1.draw(batch);
        p2.draw(batch);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.