Camera bug

Well I have this camera issue, it happened once I made my player class, abstract of the actor class. The black borders around the map is filled with pixels of the map, my guess is that the maps are overlapping each other.


package mon.str.players;

import mon.str.maps.MapHandler;
import mon.str.players.movement.MovementHandler;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;

public class PlayerRenderer extends Actor {
	
	private Texture texture;
	private Stage stage;
	private MoveToAction moveAction;
	private int columns = 4, rows = 4;
	private TextureRegion[][] textureRegion;
	private TextureRegion[] frames = new TextureRegion[rows * columns];
	private TextureRegion currentFrame;
	private Animation animation;
	private String player;
	private float speed = .50f;
	private int tileSize = MapHandler.getTileSize();
	private MapHandler map;
	
	public PlayerRenderer(String player) {
		stage = new Stage();
		this.player = player;
		Gdx.input.setInputProcessor(stage);
		texture = new Texture(Gdx.files.internal("players/"+ this.player).toString());
		textureRegion = TextureRegion.split(texture, texture.getWidth()/columns, texture.getHeight()/rows);
		int index = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				frames[index++] = textureRegion[i][j];
			}
		}
		animation = new Animation(1, frames);
		currentFrame = animation.getKeyFrame(0);
		moveAction = new MoveToAction();
		moveAction.setPosition(Gdx.graphics.getWidth()/2-MapHandler.getTileSize(), Gdx.graphics.getHeight()/2-MapHandler.getTileSize());
		addAction(moveAction);
		stage.addActor(this);
	}
		
	public void draw(Batch batch, float alpha) {
		stage.act(Gdx.graphics.getDeltaTime());
		movement();
		map.getCamera().position.set(getX()+tileSize, getY()+tileSize, 0);
		map.getCamera().update();
		batch.setProjectionMatrix(map.getCamera().combined);
		batch.draw(currentFrame, getX(), getY());
	}
	
	public Stage getStage() {
		return stage;
	}
	
	public void getMap(MapHandler map) {
		this.map = map;
	}
	
	public void movement() { // move this to movement handler
		if (Gdx.input.isKeyPressed(Keys.D)) {
			if (!getActions().contains(moveAction, true)) {
				moveAction.reset();
				currentFrame = animation.getKeyFrame(MovementHandler.goRight());
				moveAction.setPosition(getX()+tileSize*2, getY());
				moveAction.setDuration(speed);
				addAction(moveAction);
			}
		} else if (Gdx.input.isKeyPressed(Keys.S)) {
			if (!getActions().contains(moveAction, true)) {
				moveAction.reset();
				currentFrame = animation.getKeyFrame(MovementHandler.goDown());
				moveAction.setPosition(getX(), getY()-tileSize*2);
				moveAction.setDuration(speed);
				addAction(moveAction);	
			}
		} else if (Gdx.input.isKeyPressed(Keys.A)) {
			if (!getActions().contains(moveAction, true)) {
				moveAction.reset();
				currentFrame = animation.getKeyFrame(MovementHandler.goLeft());
				moveAction.setPosition(getX()-tileSize*2, getY());
				moveAction.setDuration(speed);
				addAction(moveAction);	
			}
		} else if (Gdx.input.isKeyPressed(Keys.W)) {
			if (!getActions().contains(moveAction, true)) {
				moveAction.reset();
				currentFrame = animation.getKeyFrame(MovementHandler.goUp());
				moveAction.setPosition(getX(), getY()+tileSize*2);
				moveAction.setDuration(speed);
				addAction(moveAction);	
			}
		}
	}
	
	public void dispose() {
		stage.dispose();
	}
}


And my map rendering class.


package mon.str.maps;

import mon.str.handlers.AbstractHandlers;
import mon.str.handlers.ExceptionHandler;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class MapHandler extends AbstractHandlers {
	
	private TiledMapRenderer renderMap;
	private TiledMap map;
	private String name;
	private MapProperties mapProps;
	private boolean[][] grid;
	private static int tileSize = 16;
	private OrthographicCamera camera;
	
	public MapHandler(String name) {
		this.name = name;
		try {
			map = new TmxMapLoader().load(Gdx.files.internal("maps/"+ this.name).toString());
		} catch(Exception e) {
			new ExceptionHandler(this.getClass().getName(), e);
		}
		mapProps = map.getProperties();
		renderMap = new OrthogonalTiledMapRenderer(map);
		grid = new boolean[getWidth()/tileSize][getHeight()/tileSize];
	}

	public void load() {
		renderMap.setView(camera);
		renderMap.render();
	}
	
	public void setCamera(Camera camera) {
		this.camera = (OrthographicCamera) camera;
		this.camera.setToOrtho(false);
		this.camera.update();
		
	}
	
	public static int getTileSize() {
		return tileSize;
	}
	
	public OrthographicCamera getCamera() {
		return camera;
	}
	
	public TiledMapRenderer getRender() {
		return renderMap;
	}
		
	public boolean[][] getGrid() {
		return grid;
	}
		
	public void setMapName(String name) {
		this.name = name;
	}
	
	public String getMapName() {
		return name;
	}
	
	public int getWidth() {
		return (Integer) mapProps.get("width");
	}

	public int getHeight() {
		return (Integer) mapProps.get("height");
	}
		
	public void dispose() {
		((OrthogonalTiledMapRenderer) renderMap).dispose();
	}
}

Image of the camera bug.

I was wondering if anyone had any ideas of what this possibly might be, also instead of the map object being implemented inside the player class, I am going to make a map object class for overhead (right now I have my Maphandler going into the player class inside the constructor).

Can you show the code that calls

Player.draw

?
It looks like you’re not clearing the screen between frames.

Here you go.


	public void draw () {
		Camera camera = viewport.getCamera();
		camera.update();

		if (!root.isVisible()) return;

		Batch batch = this.batch;
		if (batch != null) {
			batch.setProjectionMatrix(camera.combined);
			batch.begin();
			root.draw(batch, 1);
			batch.end();
		}

		if (debug) drawDebug();
	}


package mon.str.game;

import mon.str.maps.MapHandler;
import mon.str.players.PlayerRenderer;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;

public class Main extends ApplicationAdapter {
	private MapHandler map;
	private PlayerRenderer player;
	
	@Override
	public void create () {
		map = new MapHandler("test2.tmx");
		player = new PlayerRenderer("Red.png");
		map.setCamera(player.getStage().getCamera());
		player.getMap(map);
	}

	@Override	
	public void render () {
		Gdx.graphics.setTitle("Game FPS: " + Gdx.graphics.getFramesPerSecond());	
		Gdx.gl.glClearColor(1, 0, 0, 1);
		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
		Gdx.gl.glEnable(GL20.GL_BLEND);
		map.load();
		player.getStage().draw();
	}
}

try calling gl_clear();

Nah that wont do anything (I did try it though, because I had weirder stuff happen to me before).

I think you are misunderstanding the difference between glClear and glClearColor. The former one clears the screen with the specified clear color, while the latter one species which color to use as the clear color.

Add this code after the call to glClearColor.


Gdx.gl.glClear(GL10.COLOR_BUFFER_BIT);

That should solve the issue in my opinion.

Sorry, yeah I managed to fix it earlier. It was that I was missing to clear the color buffer.