Jump to content
  • Advertisement
Sign in to follow this  
stein102

[LibGdx] Keep camera centered on player?

This topic is 1918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a top-down 2D rpg in Java with LibGdx and I'm trying to set it up so that when I move my character, the camera follows him. I have that part pretty much figured out, but now I need to have it so that the map only renders in the top left corner of the screen so that I can have a UI in the other portion. Does anyone have any suggestions? I'll post what I have right now

	public void render(float delta) {
		// Clear the screen.
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		player.update(this);
		
		camera.update();
		renderer.setView(camera);
		renderer.render();
		
		//Draw to screen
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
			batch.draw(player.playerSprite,player.playerPosition.x,player.playerPosition.y);
		batch.end();
	}

and for the player movement class:

	public void update(GameScreen gs) {
		if (moveDirection != null) {
			switch (moveDirection) {
			case LEFT:
				gs.player.playerPosition.x -= gs.player.tileSize;
				gs.camera.translate(-gs.player.tileSize,0);
				break;
			case RIGHT:
				gs.player.playerPosition.x += gs.player.tileSize;
				gs.camera.translate(gs.player.tileSize,0);
				break;
			case UP:
				gs.player.playerPosition.y += gs.player.tileSize;
				gs.camera.translate(0,gs.player.tileSize);
				break;
			case DOWN:
				gs.player.playerPosition.y -= gs.player.tileSize;
				gs.camera.translate(0,-gs.player.tileSize);
				break;
			}
			Gdx.app.log("x",Integer.toString((int)gs.player.playerPosition.x/32));
			Gdx.app.log("y",Integer.toString((int)gs.player.playerPosition.y/32));
		moveDirection = null;
		}
	}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!