I just started playing with LibGDX, and I am already facing a camera problem.
I saw several tutons on the Internet that said, if I understood well that in a 2D game using LibGDX you need to recalculate the position of all your objects (I mean every tenth on the screen), taking a speed parameter, etc. ..
For me, this means that the camera is not moving, but the world that it is showing. I would like to do the opposite, moving the camera, but not the world, because it makes more sense to me and looks easier, because I just need to move the camera, not all the objects. Therefore, I do not want the world to move and my camera to show changes, but my camera to move and show changes. Therefore, even if some objects do not move (their x and y coordinates do not change), it will look like the camera is moving.
So, I have a Camera class that inherits from OrthographicalCamera and Entity class. I also have 2 PositionListener and PositionListenable => Observer interfaces.
So what I tried to do:
- My camera only listens to one of my entities (for example, a player)
- When my object moves, it updates the camera with its coordinate (x, y), and my camera changes its position to x and y to follow my essence.
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 the magazines, I see that position.x and position.y have changed, as I expected to follow my essence, but visually the camera does not move? Everything looks the same as if my camera didnβt translate!
My camera does not move at all, only my essence!
What am I missing? I read something about "viewPort", but didnβt really understand how I am starting with LibGDX.