libGDX viewport mouse problem

Started by
0 comments, last by Example_user 7 years, 3 months ago

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

Advertisement

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();

This topic is closed to new replies.

Advertisement