LibGDX Mouse Position While Zoomed in

Hey guys,

I’m having a problem with some bit of code that is supposed to return mouse position relative to the main Camera. This code works except when the camera is zooming in (i believe that is when the zoom value is less than 1). Here is the code:


        mx = ((MouseInput.GetX() - camera.viewportWidth/2) + camera.position.x);
	my = ((camera.viewportHeight / 2 + MouseInput.GetY()) + camera.position.y);

PS: This code is only used when selecting game items that are relative to the camera. For Gui I use different Mouse Coordiantes

never mind I fixed it.
for those who want to know how:


	mx = ((MouseInput.GetX()*camera.zoom - camera.viewportWidth*camera.zoom/2) + camera.position.x);
	my = ((camera.viewportHeight*camera.zoom / 2 + MouseInput.GetY()*camera.zoom) + camera.position.y);

I have not try it, but I think that camera.unproject() should work as well.

You should unproject IMO, multiplying by the zoom might cause inaccuracy, since it will round to the nearest pixel. Not much but yeah, unproject avoids this.

Even worse: If you don’t use .unproject() and you add rotation of the viewport some day (for some visual shake effect, for example) the simple Multiplication wouldn’t work anymore. :wink:

Never even considered that, the rotation of the matrix would completely fuck up.

Also, thats precisely what camera.unproject() is meant for so why make your life difficult?