LibGdx TouchInput gives strange numbers for positi

2019-07-11 21:13发布

have a small problem. Cannot understand how to solve it. I am implementing user touch input in my LibGdx game. My screenSize is next: width = 408 and height = 272; When I am trying to print values I get from method touchDown, I get absolutely weird numbers for positions on the screen. They can be even bigger than 1000... How is it even possible if my screenSize is 408x272?

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println(screenX + "     " + screenY);    //For some reasons this gives completely absurd numbers for the positions on the screen.
return true;
}

It will be very appreciated if you could help me. I think I miss something easy and straightforward... Thank you.

2条回答
一夜七次
2楼-- · 2019-07-11 21:39

The only thing I can think of is that you screen size is not what you think it is - are you possibly mixing that up with having defined your Camera to view a 408x272 region? Is the size of the window on the screen actually 408x272 pixels?

If this is the issue, you need to do - assuming you are using an OrthographicCamera:

Vector3 v3 = new Vector3(screenX, screenY, 0);
cam.unproject(v3);

This will set the v3.x and v3.y to what you are looking for.

If this is not the issue, I'd try to make sense of how the strange positions are related to the expected ones - does it behave like the screen was larger? Is it flipped? If you manage to observe that, it might be easier to figure out.

查看更多
狗以群分
3楼-- · 2019-07-11 21:44

A lot of people will use Vector3 for storing touch points and will use Gdx.input to get X and Y coordinates. Checkout Superjumper for more details, specifically the updateRunning function.

查看更多
登录 后发表回答