Jump to content
  • Advertisement
Sign in to follow this  

libGDX viewport mouse problem

This topic is 714 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

Hi guys,

I'm making a retro game in libGDX, and I've encountered a problem. I'm using FitViewport to add letterboxing and make the game look nice on all resolutions. Anyways, it's a shooter game and I need to draw an "aim" cursor on mouse position.

Now, when there is no letterboxing (i.e. the aspect ratio is the same) it's all good, but otherwise, the cursor will not follow the mouse exactly and there will be an increasing gap (look at the image).

I'm not sure if this is a bug or something.. Also, I should note that I use OrthographicCamera and not FBOs. Relevant code:

camera = new OrthographicCamera();
viewport = new FitViewport(WIDTH, HEIGHT, camera);
camera.position.set(camera.viewportWidth / 2,camera.viewportHeight / 2, 0);

Mouse handling code:

public class MMouse {
	private OrthographicCamera camera;

	private Vector3 pos;

	public MMouse(OrthographicCamera camera) {
		this.camera = camera;
		pos = new Vector3();

	public void update() {
		final int screenX = Gdx.input.getX();
		final int screenY = Gdx.input.getY();

		pos.set(screenX, screenY, 0);


	public float getX() {
		return pos.x;

	public float getY() {
		return pos.y;

I've tried using event-driven input too, nothing changed.


PS: Sorry for bad photo, I had some problems taking a screenshot..


Have a nice day,


Share this post

Link to post
Share on other sites

You should update the camera on each render call:

      Gdx.gl.glClearColor(1, 0, 0, 1);

// draw here

Share this post

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

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!