LibGDX projecting coordinates into world space

I know this is quite the newbie question, but I’m having trouble setting up mouse input in my game. The game world can scroll, and I’m trying to get the position of the mouse when it clicks. My game is a tiled game, so I take every mouse click and divide it by the tile size to try to get world coordinates. However, when I get the coordinates, they are crazy. The y is opposite of what I’m clicking (click at 0, 1 and I get 0, -1). I realize this is probably because my camera is set up with the origin to the bottom left, but when I try subtracting the window height, the y coordinate is still crazy. Its generally the same for the x coordinate, although I can’t really tell if its doing the same as y because the x seems so sporadic… I thought about it and I decided that since my world can scroll, maybe that was what was messing it up. So I used unproject, but that did nothing. Here’s my code:

if (Gdx.input.isTouched()) {
			Vector3 touchPos = new Vector3();
			touchPos.set(Gdx.input.getX() / 16, Gdx.input.getY() / 16, 0);
			cam.getCamera().unproject(touchPos);
							
			if (touchPos.x >= 0 && touchPos.y >= 0 && touchPos.x <= tileWidth && touchPos.y <= tileHeight) {
				tiles[(int) touchPos.x][(int) touchPos.y] = Tile.base;
			}
		}

And my camera code:

package com.nishu.balancedEnergy.utilities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;

public class Camera {
	
	private OrthographicCamera camera;
	
	private float x, y;
	private float moveSpeed = 1f;
	private float zoom = 0.5f;
	
	public Camera(){
		camera = new OrthographicCamera();
		camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.zoom = zoom;
	}
	
	public void update(){
		camera.update();
		if(zoom <= 0){
			zoom = 0.1f;
		}
		if(zoom > 1){
			zoom = 1;
		}
		camera.zoom = zoom;
	}
	
	public void move(){
		if(Gdx.input.isKeyPressed(Keys.W)){
			y += moveSpeed;
		}
		if(Gdx.input.isKeyPressed(Keys.S)){
			y -= moveSpeed;
		}
		if(Gdx.input.isKeyPressed(Keys.A)){
			x -= moveSpeed;
		}
		if(Gdx.input.isKeyPressed(Keys.D)){
			x += moveSpeed;
		}
		if(Gdx.input.isKeyPressed(Keys.NUM_1)){
			zoom += 0.0625f;
		}
		if(Gdx.input.isKeyPressed(Keys.NUM_2)){
			zoom -= 0.0625f;
		}
		translate(x, y);
		x = 0; 
		y = 0;
	}
	
	public void translate(float x, float y){
		camera.translate(x, y);
	}

	public OrthographicCamera getCamera() {
		return camera;
	}

	public void setCamera(OrthographicCamera camera) {
		this.camera = camera;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

}

So what am I doing wrong here?

I had issues with unprojecting mouse coordinates a while back as well, which I documented here.
I still haven’t found a good solution though, so I’m afraid my ‘fix’ is all I can help you with.
Note however that I am using a PerspectiveCamera and an isometric (ish) view of the tiles.

Well, thanks for the help but I don’t know if any of that really applies to me :confused:

I’m not sure what perspective you’re talking about, top-down tile based, or isometric tile based?

If it is top-down perspective, you can just subtract your maps render offset and divide by your tile width\height.

If it’s isometric, the best methods are to (aside from those provided by LibGdx as I am unfamiliar with the library myself)

(1) reverse the projection matrix (limited to tile-based picking),

(2) (inefficient in Java2D since you have to cache a processed version of that image just to render it into the colour map) use a colour map, or

(3) (not efficiently doable in Java2D) use a depth buffer of some sort and pack the depth in to the pixel data (possible, but slow because you’ll be using software shaders in Java2D.)

If your using JWJGL/OpenGL (3) is your best bet and it is by far the easiest of the three to pull off (assuming the library doesn’t provide it’s own way of doing that)

That said, I know a way of doing per-pixel picking in Java2D as well using bit sets etc, I haven’t tested the idea out but I’m pretty sure it’d perform decently enough. It is a bit of an odd work around though, if anyone is interested.

Shouldn’t you divide by 16 after the unproject? I mean the grid is in the world coordinates, not in screen coordinates, right? Also, you can indeed adjust for the negative values by subtracting the mouse y from the height:

		if (Gdx.input.isTouched()) {
			Vector3 touchPos = new Vector3();
			touchPos.set(Gdx.input.getX() , Gdx.graphics.getHeight() - Gdx.input.getY(), 0);
			cam.getCamera().unproject(touchPos);
			touchPos.scl(1f/16f);
			
			if (touchPos.x >= 0 && touchPos.y >= 0 && touchPos.x <= tileWidth && touchPos.y <= tileHeight) {
				tiles[(int) touchPos.x][(int) touchPos.y] = Tile.base;
			}
		}

Does this help?

Sorry I’m so late responding, I still am having the issue after trying to fix it for weeks.

I forgot to mention that I zoom my camera in and out, and at zoom level 1(no zoom pretty much), it works fine, as it should. My issue is that it doesn’t work after I zoom in at all. I’m using an orthological camera.

Your response is no response. You have received several responses and suggestions, maybe try starting there?

Sorry, I did. None of them worked, scaling the vector probably came the closest to helping, but I still can only get true screen to world coordinates when the camera isn’t zoomed at all.