0

I have started learning android and libgdx now, and trying to figure out how does the screen, and the touch input work in android, now I am testing touch positions by drawing a rectangle upon touch.

The rectangle will be drawing on the touched X, Y.

But when I did it, when I touch on the left, it will draw a rectangle on the right, same if I do the opposite, it will draw on left.

if I touch the center, somewhat it will draw it in the center, but really not same coords as my finger's touch.

What is the accurate way to detect positions?

public class myApp implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;
    private ShapeRenderer shapeRender;
    private Player player;
    private ArrayList<Player> players;

    @Override
    public void create() {      
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera(w, h);
        shapeRender = new ShapeRenderer();

        batch = new SpriteBatch();

        texture = new Texture(Gdx.files.internal("data/libgdx.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

        sprite = new Sprite(region);
        sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
        sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
        sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
        this.players = new ArrayList<Player>();
    }

    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }

    @Override
    public void render() {      
        update(Gdx.graphics.getDeltaTime());
        renderGame();
    }

    private void update(double delta) {
        Input i = Gdx.input;

        if (i.isTouched()) {
            this.players.add(new Player(i.getX(), i.getY()));
        }
    }

    private void renderGame() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        //shapeRender.setProjectionMatrix(camera.combined);
        if (players.size() > 0) {
            shapeRender.begin(ShapeType.Filled);
            shapeRender.setColor(Color.RED);            
            for (Player p : this.players) {
                shapeRender.rect(p.getX(), p.getY(), 100, 100);
            }
            shapeRender.end();  
        }

    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}
Artemkller545
  • 929
  • 3
  • 17
  • 52

1 Answers1

0

this is because the libgdx (well, opengl native) coordinates and the touch input Y coordinates are not measured from the same corner. as you've noticed, the touch coordinate is relative to the top left while the libgdx coordinate is relative to the bottom left.

You can invert this manually by subtracting from the height, or you can "fix" the camera (probably a better idea).

see: https://stackoverflow.com/a/7751183/763530 (an answer from badlogic himself.)

Community
  • 1
  • 1
nebulae
  • 2,627
  • 1
  • 16
  • 16