I'm trying to build a simple game where TileMapRenderer renders a labyrinth, a ball moves in it and camera follows it. I'm currently having the following bug:
For some reason, the game renders the world incorrectly. The gamescreen is cleared with the following method:
private void clearScreen() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
Which is basically red, and the camera seems to show it incorrectly while moving.
Camera movement method:
public void moveCamera() {
GameInfo.camera.position.set(player.body.getPosition().x,
player.body.getPosition().y, 0);
// LEFT
if(GameInfo.camera.position.x < GameInfo.WINDOW_WIDTH / 2){
GameInfo.camera.position.x = GameInfo.WINDOW_WIDTH / 2;
}
// UP
if(GameInfo.camera.position.y > GameInfo.WORLD_HEIGHT_PIXELS / 100.0f - GameInfo.WINDOW_HEIGHT / 2) {
GameInfo.camera.position.y = GameInfo.WORLD_HEIGHT_PIXELS / 100.0f - GameInfo.WINDOW_HEIGHT / 2;
}
// DOWN
if(GameInfo.camera.position.y < GameInfo.WINDOW_HEIGHT / 2) {
GameInfo.camera.position.y = GameInfo.WINDOW_HEIGHT / 2;
}
// RIGHT
if(GameInfo.camera.position.x > GameInfo.WORLD_WIDTH_PIXELS / 100.0f - GameInfo.WINDOW_WIDTH / 2f) {
GameInfo.camera.position.x = GameInfo.WORLD_WIDTH_PIXELS / 100.0f - GameInfo.WINDOW_WIDTH / 2f;
}
}
As you can see, I'm using Box2D physics for the ball.
Setting up the camera:
public static OrthographicCamera camera = new OrthographicCamera();
public static void setCamera() {
camera.setToOrtho(false, 8.0f, 4.5f);
}
Rendering method:
public void render () {
batch.setProjectionMatrix(GameInfo.camera.combined);
clearScreen();
moveCamera();
GameInfo.camera.update();
Game.tiledMapRenderer.setView(GameInfo.camera);
Game.tiledMapRenderer.render();
batch.begin();
batch.draw(player.texture,
player.body.getPosition().x - player.radius,
player.body.getPosition().y - player.radius,
player.radius, // originX
player.radius, // originY
player.radius * 2, // width
player.radius * 2, // height
1.0f, // scaleX
1.0f, // scaleY
player.body.getTransform().getRotation() * MathUtils.radiansToDegrees,
0, // Start drawing from x = 0
0, // Start drawing from y = 0
player.texture.getWidth(), // End drawing x
player.texture.getHeight(), // End drawing y
false, // flipX
false); // flipY
player.updatePlayer();
Game.checkStateOfGame(player);
batch.end();
//GameInfo.doPhysicsStep(Gdx.graphics.getDeltaTime());
Game.world.step(1/60f, 8, 3);
}
It's not only while moving either. As soon as the game starts, the red block is visible like seen in the first picture.
Also the red blocks seem to always be coming from left and down, never from up or right...
What could be wrong in here?