[LibGDX] Mouse coordinates all funky.

Alright, so whenever I call Gdx.input.getMouseX(); and test it out, I get insanely high numbers even though my screen width is 480. Now, the beginning (left) is still 0, but the end (right of screen) goes almost to 30,000. I’m not using Cameras or anything for my game so could that be the problem?

I cut off unimportant parts of this code.

public class Player {
	private int x = 480 / 2, y = 100, reviveTime = 0;
	private Rectangle rect;
	private Texture playerTexture;
	private SpriteBatch batch;
	public boolean timedOut = false;

	public Player() {
		rect = new Rectangle(x, y, 32, 32);
		Texture.setEnforcePotImages(false);
		playerTexture = new Texture(Gdx.files.internal("data/Graphics/Orb.png"));
		batch = new SpriteBatch();
		

	}

	public void render() {
			
		batch.begin();
		batch.draw(playerTexture, x, y);
		batch.end();

	}

	public void update() {
		x = Gdx.input.getX(0)-75 /2;
		
		rect.setPosition(x, y);

		
		if (x < 0) {
			x = 0;
		}
		;
		if (x > 480) {
			x = 480;
		}
		;
		
			if(timedOut == true){
				revive();
			}
	}

I don’t think the problem is in this code, so would you care to share some more? Are you changing the player position somewhere else? A problem may be that you are setting the rectangle’s position before the if statements check if x <0 or x > 480.

public void update() {
      x = Gdx.input.getX(0)-75 /2;
      
      rect.setPosition(x, y); //<-------------- try putting this under the if statements

      
      if (x < 0) {
         x = 0;
      }
      ;
      if (x > 480) {
         x = 480;
      }
      ; 
      
         if(timedOut == true){
            revive();
         }
   }

Changing the position of that piece of code does nothing, and I do not modify the location of the player anywhere else. I would give you the rest of the code, but they’re just getter methods. Thank you for trying to help though! ??? ??? ???

I suggest you to use a camera and do this:

if (Gdx.input.justTouched())
{
	vector3 newPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

	camera.unproject(newPos);
	
	System.out.println(newPos.x); //New X
	System.out.println(newPos.y); //New Y
}

You can override InputAdapter>MouseEvent with the above code.

Alright, so i’ve added the unproject code you guys told me to in my update loop and nothing. What I have found out though is that nothing is being printed to the console which means that Gdx.input.justTouched() is never getting called. Whats going on???

if(Gdx.input.justTouched()){
		   newPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
		   camera.unproject(newPos);
		   System.out.println(newPos.x); //New X
		   System.out.println(newPos.y); //New Y
		}

justTouched() is a method called when you touch the screen (Android) or Mouse Click Event (Desktop), I don’t know if you tried “click”, but if you want a constant test:


Vector3 newPos = new Vector3();

update()
{
	newPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	camera.unproject(newPos);

	System.out.println("Normal getX(): " + Gdx.input.getX() + " and Unproject X: " + newPos.x); // Print: Normal getX(): "SomeNumber" and Unproject X: "SomeNumber"
	System.out.println("Normal getY(): " + Gdx.input.getY() + " and Unproject Y: " + newPos.y); // Print: Normal getY(): "SomeNumber" and Unproject Y: "SomeNumber"
}

Thanks mate! It done did worked! ;D