libgdx coordinate system differences between rendering and touch input

前端 未结 4 1777
我在风中等你
我在风中等你 2021-02-08 01:50

I have a screen (BaseScreen implements the Screen interface) that renders a PNG image. On click of the screen, it moves the character to the position touched (for testing purpo

4条回答
  •  忘掉有多难
    2021-02-08 02:48

    The link below discusses this problem.

    Projects the given coords in world space to screen coordinates.

    You need to use the method project(Vector3 worldCoords) in class com.badlogic.gdx.graphics.Camera.

    private Camera camera;
    ............
    
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    

    Create an instance of the vector and initialize it with the coordinates of the input event handler.

        Vector3 worldCoors = new Vector3(screenX, screenY, 0);
    

    Projects the worldCoors given in world space to screen coordinates.

        camera.project(worldCoors);
    

    Use projected coordinates.

        world.hitPoint((int) worldCoors.x, (int) worldCoors.y);
    
        OnTouch();
    
        return true;
    }
    

提交回复
热议问题