Sign in to follow this  
MatejaS

libGDX viewport mouse problem

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);
viewport.apply();
		
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);

		camera.unproject(pos);
	}

	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,

MatejaS

Share this post


Link to post
Share on other sites

You should update the camera on each render call:

      camera.update();
      Gdx.gl.glClearColor(1, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      batch.setProjectionMatrix(camera.combined);
      batch.begin();
// draw here
      batch.end();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this