# LibGDX Drawing sprites when moving orthographic camera

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.

• You need to reverse the transform on your mouse click from screen coordinates to world coordinates. You can do this by applying the inverse of the world-to-screen transformation matrix which was used for the camera. – Shotgun Ninja Aug 14 '13 at 17:33
• @ShotgunNinja Sorry, I'm not sure if I know exactly what you mean. Are you saying that I need to reverse cam.unproject(pos)? – Jonathan Aug 14 '13 at 19:02
• gamedev.stackexchange.com/a/27793/20399 – wes Aug 14 '13 at 20:18
• @wes Sorry, I still don't think I'm getting it. Am I supposed to do cam.project(pos) somewhere in touchDown()? If I am, then the platforms still aren't being placed in the right spot. I just don't see why I would need to go from world to screen coordinates anywhere in my program – Jonathan Aug 16 '13 at 3:25

cam.unproject(touchPos) will give you the coordinates as they relate to your screen and cam.project(touchPos) will give you the coordinates as they relate to the game world.

gamedev.stackexchange.com/a/27793/20399

so in here:

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;
}

...
}


make it like this:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 pos = new Vector3(screenX, screenY, 0);
cam.project(pos); //changed
Gdx.app.log(Game.LOG, "world X Coordinate: " + pos.x + " world 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;
}

...
}

• I tried that and the platforms aren't in the screen anymore. When I tried putting it down in the middle of the screen the coordinates come out to be "Pos.x: 6120 and Pos.y: 6373". Are they supposed to be that high? – Jonathan Aug 19 '13 at 17:41
• – wes Aug 19 '13 at 18:48