I'm a little bit confused. I'm using libgdx for the first time and i have some problems with the coordinate systems. When i'm creating a texture and want to set the position, I do:
texture = new Texture("myGraphic.png", 0, 0);
and my picture will be positioned at the left bottom corner.
But when I try to get the touch position with:
if(Gdx.input.isTouched())
{
Vector3 tmp = new Vector3(Gdx.input.getX(),Gdx.input.getY(),0);
System.out.println("Coord:" + " + " + tmp.x + " + " + tmp.y);
}
I recognized that (0,0) is in the left top corner. So I tried camera.unproject(tmp) before my output, but then i will get only values between -1 and 1. How do i get the same coordinate system for all elements?
In Libgdx coordinate system for touch is y-down
but for Screen or Image it's y-up
.
Take a look of LibGDX Coordinate systems
If can use camera, set y-axis pointing up by camera.setToOrtho(false);
and get world point by camera.unproject(vector3);
method.
public class TouchSystem extends ApplicationAdapter {
SpriteBatch batch;
Texture texture;
Vector3 vector3;
OrthographicCamera camera;
@Override
public void create() {
batch=new SpriteBatch();
texture=new Texture("badlogic.jpg");
vector3=new Vector3();
camera=new OrthographicCamera();
camera.setToOrtho(false); // this will set screen resolution as viewport
}
@Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture,0,0);
batch.end();
if(Gdx.input.justTouched()) {
vector3.set(Gdx.input.getX(),Gdx.input.getY(),0);
camera.unproject(vector3);
System.out.println("Coord:" + " + " + vector3.x + " + " + vector3.y);
}
}
@Override
public void dispose() {
texture.dispose();
batch.dispose();
}
}