I've been having this problem for a long time and I just can't seem to find the exact problem. I have a game where the map is 480x1600 and my camera has a view of 480x800. I have a button that when pressed, allows the user to place a platform on the map, and since the map is too big to fit on the screen, I made it so the user can move the camera up and down the map by dragging.
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector3 pos = new Vector3(screenX, screenY, 0);
cam.unproject(pos);
cam.position.y = pos.y;
return true;
}
Method that controls when the user tries to put a platform down
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 pos = new Vector3(screenX, screenY, 0);
cam.unproject(pos);
Gdx.app.log(Game.LOG, "X Coordinate: " + pos.x + " Y Coordinate: " + pos.y);
if (GameScreen.createPlatform == true) {
world.setPlatform(new Vector2(pos.x - 0.6f, pos.y - 0.1f), 1);
GameScreen.createPlatform = false;
return true;
}
...
}
Where I render the platform sprites
public void render(float delta) {
...
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
for (Sprite platformSprite: world.getPlatformSprites()) {
platformSprite.draw(spriteBatch);
}
spriteBatch.end();
...
}
The game works fine when I don't move the camera. But when I do, the platforms aren't placed where I click, they're always either higher or lower where I actually clicked. Also, the platforms are always placed somewhere where the camera was originally looking before anything has been moved.
I think it's doing this is because for some reason, the coordinates never actually change. So wherever I am on the map, the top of the screen is always 120 and the bottom is always 90. This also causes objects on the map to have different coordinates if I move the camera.
This problem has me completely lost and I would appreciate any help.