libgdx vector3 转换错误

libdgx vector3 conversion wrong

大家晚上好,

我试图通过学习教程 Here 来熟悉 libdgx 和 android。除了在 Vector3 转换中倾斜时抓取屏幕坐标外,一切似乎都很好。

所以 101 的 x 输入被转换为 -796,968 的 y 输入被转换为 -429(触摸屏幕的左上角,模拟器的结果与我的 phone 的结果相同)。单击右下角时,动画在屏幕中间触发。

这一切看起来都很基本,所以不太确定我的设置不正确,导致转换出现偏差。感谢您的帮助!

相机创建:

camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);

抓取触摸坐标:

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touchCoordinateX = screenX;
    touchCoordinateY = screenY;
    stateTime = 0;
    explosionHappening = true;
    return true;
}

渲染循环:

public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stateTime += Gdx.graphics.getDeltaTime();
    batch.begin();

    if (!explosionAnimation.isAnimationFinished(stateTime) && explosionHappening) {
        Vector3 touchPoint = new Vector3();
        touchPoint.set(touchCoordinateX,touchCoordinateY,0);
        TextureRegion currentFrame = explosionAnimation.getKeyFrame(stateTime, false);  // #16
        camera.unproject(touchPoint);
        batch.draw(currentFrame, touchPoint.x, touchPoint.y);
    }
            //  batch.draw(img, 0, 0);
    batch.end();
    if (explosionAnimation.isAnimationFinished(stateTime)){explosionHappening = false;}
}

我想你忘了为你的 SpriteBatch 设置相机投影矩阵。只需添加

batch.setProjectionMatrix(camera.combined);

之前

batch.begin();