LibGDX - Camera.update() call does nothing

2d, android, camera, java, libgdx

Solution

Ok here how I got it to work. Here 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);

and in the main loop, the `render()` method of `ApplicationListener` :

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

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

The draw method() also make my entity move, and notify its observer which is the camera. Then I update the camera (not from its own method like I did before) and just update the projectionmatrix.

A bit laggy, but works like I wanted it to !

Problem

I just began playing with LibGDX, and am already facing a problem with the camera. I saw some tutos on Internet which told, if I understood well, that in a 2D game using LibGDX you have to recalculate the position of all of your entities (I mean everyinthg on the screen) taking in parameter the velocity etc... For me it means that the camera doesn't move, but the world that it shows. I would like to do opposite, moving the camera but not the world, as it makes more sense to me, and looks to be easier as I just have 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. So even if some entities doesn't move (their coordinates x & y doesn't change), it will look like as the camera moves. So I have a `Camera` class that inherit from `OrthographicalCamera`, and an `Entity` class. I have also 2 interfaces `PositionListener` & `PositionListenable` => Pattern Observer. So what I tried to do is: - My camera "listens" to only one of my Entity (e.g. the player) - When my entity moves, it updates the camera with its coordinate (x, y) and my camera changes its position to x and y to follow my entity. Here 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 logs, I see that `position.x` and `position.y` changed as I expected to do to follow my entity, but visually the camera doesn't move ?? Everything looks the same as if my camera doesn't translate ! My camera is not moving at all, only my entity ! What am I missing ? I read something about "viewPort" but really didn't get the thing as I am a beginner with LibGDX.

Original source