0
@Override
public void create()
{

    batch = new SpriteBatch();

    shape = new ShapeRenderer();
    velocity = new Vector2(100, 0);
    position = new Rectangle(0, 5, 100, 100);

    font = new BitmapFont();
    font.setColor(Color.BLACK);
    font.getData().scale(3f);

    camera = new OrthographicCamera();
    confCamera();

}

@Override
public void render()
{        

       if(Gdx.input.isTouched())
        position.x = Gdx.input.getX() - position.width/2;
        position.y = (Gdx.input.getY()  - position.height/2);

    shape.begin(ShapeRenderer.ShapeType.Filled);

    shape.setColor(Color.BLACK);
    shape.rect(position.x, position.y, position.width, position.height);

    shape.end();
}

It's a simple code, but I'm not undestanding Y axis, my shape moves like a mirror. If I touch on top, my shape goes to bottom. If I touch on bottom, my shape goes to top. How to fix it?

1 Answers1

0

LibGDX (by default) for rendering uses coordinate system where 0 is at bottom of the screen and the more you go up Y coordinate grows.

Also, when you read input coordinates (touches, moves...) you get screen coordinates, but when you render your graphics you are using "world" coordinates. They are in 2 different coordinate system so to convert from screen to world you have to use camera.unproject() call. Should be like:

Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);

and then use touchPos.x and touchPos.y.

The similar question is asked here so you can find more answers there:

Using unProject correctly in Java Libgdx

MilanG
  • 6,994
  • 2
  • 35
  • 64