Get cursor position in LIBGDX

前端 未结 2 935
说谎
说谎 2021-02-05 09:50

How to get cursor position in libgdx and apply it to sprite? Like this? \"enter

I want my

2条回答
  •  孤城傲影
    2021-02-05 10:09

    I usually track my mouse into world coordinates like this:

        // variables
        private final Vector2 mouseInWorld2D = new Vector2();
        private final Vector3 mouseInWorld3D = new Vector3();
        private final OrthographicCamera cam; 
        .
        .
        .
        //in render() code
        mouseInWorld3D.x = Gdx.input.getX();
        mouseInWorld3D.y = Gdx.input.getY();
        mouseInWorld3D.z = 0;
        cam.unproject(mouseInWorld3D);
        mouseInWorld2D.x = mouseInWorld3D.x;
        mouseInWorld2D.y = mouseInWorld3D.y;
    

    Then, I can use mouseInWorld2D to access mouse coordinates (relative to the scene/world) in my game. A sprite can track that coordinate very easy and compute its angle and direction.

提交回复
热议问题