【问题标题】:Pinch zoom and pan to specific screen coordinate捏缩放并平移到特定的屏幕坐标
【发布时间】:2017-10-10 07:31:18
【问题描述】:

我想在平铺的 15f x 15f 3D 板上围绕特定坐标进行缩放。 IE。我不想放大原点。因此我需要相应地平移棋盘。

我正在使用PerspectiveCamera(近=0.1f,远=100f)。为了与屏幕完美贴合,缩放前相机位于approx.z=13.4

现在(我认为)我想做的是:

每次捏合缩放都会取消投影屏幕坐标(GestureDetector.pinch 方法):

float icx = (initialPointer1.x + initialPointer2.x) / 2;
float icy = (initialPointer1.y + initialPointer2.y) / 2;
pointToMaintain = new Vector3(icx, icy, 0f);
mCamera.unproject(pointToMaintain);

现在对于每个缩放周期(当我相应地调整 mCamera.z 并执行 mCamera.update() 时)我将点投影回屏幕坐标:

Vector3 pointNewPos = new Vector3(pointToMaintain);
mCamera.project(pointNewPos);

然后相应地计算增量和平移:

int dX = (int) (pointNewPos.x - icx);
int dY = (int) (pointNewPos.y - icy);
pan(...); /* I.e. mCamera.translate(...) */

我的问题是 mCamera.z 最初位于 pointToMaintain.z 上方,然后随着用户移动手指而下降:

   cam.z   ptm.z   dX    dY
0  13.40
1  13.32   13.30   12   134
2  13.24   13.30   12  -188
...

(0) 是缩放开始前 mCamera.z 的原始值。 (1) 无效?但是(2)应该没问题。

我的问题是:

(1) 在相机上取消投影屏幕坐标时,如何获得“有效”pointToMaintain。 IE。一个不小于cam.z的点。 (我得到 13.30 点的原因是(我猜)因为 near=0.1f。但如上所示,这会导致(奇怪?)屏幕坐标)。

(2) 将图块板移动到更靠近用户捏拉放大的坐标处,这是一个好的策略吗?

【问题讨论】:

    标签: android libgdx opengl-es-2.0


    【解决方案1】:

    为了保持焦点,我做了以下代码:

    Obs:此代码依赖于重载运算符,您需要通过其方法(addMe、subtract 等)更改向量运算符

    void zoomAt(float changeAmmount, Vector2D focus) {
        float newZoom = thisZoom + changeAmmount;
        offset = focus - ((focus - offset) * newZoom / thisZoom);
        thisZoom = newZoom;
    }
    

    在哪里

    focus = 当前要保持的中心点

    offset = 到 0,0 的距离

    thisZoom = 当前缩放数量; (从 1 开始)

    changeAmmount = 增加或减少缩放的值

    我花了 3 年时间做了 4 次尝试才完成,当你把它画下来时很容易,它只有两个三角形。

    【讨论】:

    • thisZoom 最初不是 0 对吧?您将其设置为 camera.z 值?
    • thisZoom 从 1 开始
    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2023-03-27
    • 2012-05-11
    • 1970-01-01
    • 2016-11-11
    • 2016-11-11
    • 2017-03-06
    相关资源
    最近更新 更多