i'm using a FitViewport to render my "game world". Viewport virtual dimensions are fixed 400x240. I use viewport.unproject method to scale input touches from screen coordinates to world coordinates, and the values are good.

The problem begins when i use the opposite (viewport.project method). I have to use it for drawing a sprite that is scaled for screen dimensions but i have X and Y positions in "game" coordinates.

Vector2 screenCoords = new Vector2();
screenCoords = (viewport.project(new Vector2(gameX, gameY)));

When i use viewport.project i only have the result i expect if the screen aspect ratio matches my game aspect ratio (so the viewport fits the whole screen).

I noticed that if screen height is higher (so my viewport is fitted horizontally and black bars appear below and above it) the X coordinate is fine, but Y coordinate varies slightly depending if the "game" Y coordinate is near upper or lower side.

Based on my knowledge and on javadoc comments i've red, the method must automatically recognize if the viewport is not full-screen or if it is not, and add an offset based on how tall the black bars are.

Am i doing something wrong???


Use camera projection instead of the viewport projection. And use the cameras viewport instead of your virtual viewport. The stuff on your screen is rendered using the cameras projection and the cameras viewport, so if you want world coordinates to end up in the same place, then this should solve your problem:

cam.project(new Vector2(gameX, gameY), 0, 0, cam.viewportWidth, cam.viewportHeight);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.