Java 如何在libgdx中将触摸坐标转换为本地对象坐标?

Java 如何在libgdx中将触摸坐标转换为本地对象坐标?,java,android,libgdx,transformation,Java,Android,Libgdx,Transformation,我想将用户触摸时接收到的点坐标转换为本地对象坐标 触地时,我得到屏幕坐标中的(x,y): left-top=(0,0), bottom-right=(SCREEN_WIDTH,SCREEN_HEIGHT). 然后我做camera.unproject(…)并在世界坐标(视口)中获取(x,y),但我不知道如何将获取的点转换为局部对象坐标。我尝试了一些操作(请参见MyRect.unproject(..))中的注释),但都不起作用 public class MyLibgdxGame extends G

我想将用户触摸时接收到的点坐标转换为本地对象坐标

触地时,我得到屏幕坐标中的(x,y):

left-top=(0,0), bottom-right=(SCREEN_WIDTH,SCREEN_HEIGHT).
然后我做
camera.unproject(…)
并在世界坐标(视口)中获取(x,y),但我不知道如何将获取的点转换为局部对象坐标。我尝试了一些操作(请参见
MyRect.unproject(..)
)中的注释),但都不起作用

public class MyLibgdxGame extends Game implements InputProcessor {
    private MyRect obj;
    ...
    public void render() {
        camera.update();

        Gdx.gl.glClearColor(.1f,.1f,.1f,1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        renderer.setProjectionMatrix(camera.combined);

        renderer.begin(ShapeRenderer.ShapeType.Line);
        renderer.setColor(Color.GREEN);
        renderer.line(0,0, 0, 30);
        renderer.setColor(Color.BLUE);
        renderer.line(0,0, 30, 0);
        renderer.end();

        obj.render(renderer);
    }

    public void resize(int width, int height) {
        float units = 100f;
        float aspectRatio = (float) width / (float) height;
        camera = new OrthographicCamera(units * aspectRatio, units);
        camera.update();
    }

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Gdx.app.log("my", String.format("[Touch] x: %d, y: %d", screenX, screenY));
        Vector3 v = new Vector3(screenX, screenY, 0);
        camera.unproject(v);
        Gdx.app.log("my", String.format("[S to VP] x: %.1f, y: %.1f", v.x, v.y));

        // !! convert world coordinate "v" to local object coordinate
        obj.unproject(v);
        Gdx.app.log("my", String.format("[VP to Object] x: %.1f, y: %.1f", v.x, v.y));

        return false;
    }
    ...
}

public class MyRect {

    private Rectangle rect;
    private float angle;
    private Matrix4 transform;

    public MyRect(float x, float y, float w, float h, float angle) {
        this.rect = new Rectangle(x, y, w, h);
        this.angle = angle;

        transform = new Matrix4().translate(x,y,0).rotate(0,0,1,angle);
    }

    public void render(ShapeRenderer renderer) {

        renderer.identity();
        renderer.setTransformMatrix(transform);

        renderer.begin(ShapeRenderer.ShapeType.Rectangle);
        renderer.setColor(Color.RED);
        renderer.rect(0, 0, rect.width, rect.height);
        renderer.end();

        r.begin(ShapeRenderer.ShapeType.Line);
        r.setColor(Color.GREEN);
        r.line(0,0, 0, 30);
        r.setColor(Color.BLUE);
        r.line(0,0, 30, 0);
        r.end();

        renderer.identity();
    }

    public Vector3 unproject(Vector3 v) {
//      return v.mul(transform); // 1
//      return v.prj(transform); // 2
        return v.rotate(0,0,1,angle).sub(new Vector3(rect.x, rect.y, 0)); //3
    }

}

最快的方法是使用
MyRect
扩展
Actor
并使用
Actor.stageToLocalCoordinates()
。如果您不想子类化
Actor
,请获取libgdx源代码并查看上述方法


你完全弄错了

camera.unproject(v);
Gdx.app.log("my", String.format("[S to VP] x: %.1f, y: %.1f", v.x, v.y));

// !! convert world coordinate "v" to local object coordinate
obj.unproject(v);
Gdx.app.log("my", String.format("[VP to Object] x: %.1f, y: %.1f", v.x, v.y));
你说的“局部坐标”是摄像机坐标。当您取消投影触摸时,您将屏幕坐标转换为相机坐标。但你想要的是世界坐标。不需要取消对象的投影,因为这样会得到一个无用的向量。只需将相机坐标转换为世界坐标,然后使用以下坐标:

camera.unproject(v);
Gdx.app.log("my", String.format("[S to VP] x: %.1f, y: %.1f", v.x, v.y));
x = camera.position.x-camera.viewportWidth+v.x;
y = camera.position.y-camera.viewportHeight+v.y;