The Java – LibGDX – Camera.update() call does nothing

LibGDX – Camera.update() call does nothing… here is a solution to the problem.

LibGDX – Camera.update() call does nothing

I just started playing LibGDX and am already having camera issues.

I’ve seen some tutorials on the Internet telling me that, if I understand well, in a 2D game with LibGDX, you have to recalculate the position of all entities (I mean every entity on the screen) and take into account parameters like speed

For me, this means that the camera doesn’t move, but the world it shows.
I wanted to do the opposite, move the camera instead of the world, because it makes more sense to me and it looks easier because I just need to move the camera, not all entities. So I don’t want the world to move and my camera to show changes, but my camera to move and show changes. Therefore, even if some entity does not move (their coordinates x and y do not change), it will appear as if the camera is moving.

So I have a Camera class that inherits from OrthographicalCamera and an Entity class.
I also have 2 interfaces: PositionListener & PositionListenable => Pattern Observer

So what I’m trying to do is:
– My camera only “listens” to one of my entities (e.g. the player).
– When my entity moves, it updates the camera with its coordinates (x, y) and my camera changes its position to x and y to follow my entity.

This is the code in Camera.java:

public class Camera extends OrthographicCamera implements PositionListener {

public Camera(float width, float height) {
        super(width, height);
    }

@Override
    public void entityMoved(float newX, float newY) {
        translate(newX - position.x, newY - position.y);
        update();
        Gdx.app.log("entityMoved()", "newX=" + newX + " - newY=" + newY);
        Gdx.app.log("entityMoved()", "position.x=" + position.x + " - position.y=" + position.y);
}

When I read the logs, I

see that position.x and position.y change the way I expected to follow my entity, but visually the camera doesn’t move ?? Everything looks the same, as if my camera didn’t have a translation!

My camera didn’t move at all, only my entity!
What am I missing? I read a bit about “viewPort”, but since I’m a beginner to LibGDX, I don’t really understand it.

Solution

Ok, here’s how I got it to work.
This is the entityMoved() method:

@Override
public void entityMoved(float newX, float newY) {
    translate(newX - position.x, newY - position.y);
    Gdx.app.log("entityMoved()", "newX=" + newX + " - newY=" + newY);
    Gdx.app.log("entityMoved()", "position.x=" + position.x + " - position.y=" + position.y);

In the main loop, the render() method of the ApplicationListener:

sprite.begin();
world.draw(sprite);
sprite.end();

camera.update();
sprite.setProjectionMatrix(camera.combined);

The draw method(s) also moves my entity and notifies its observer that the camera is.
Then I update the camera (not using its own method as before) and update only the projection matrix.

A bit sluggish but worked just as I wanted!

Related Problems and Solutions