【问题标题】:Moving orthographic camera with mouse drag使用鼠标拖动移动正交相机
【发布时间】:2017-05-27 00:52:17
【问题描述】:

我试图让鼠标单击 + 拖动导致正交相机相对于初始单击移动。

下面的内容几乎是我想要的。

但是,您可以看出,当我点击圣马特奥大桥以南的蓝色区域时,相机会跳转到一个奇怪的位置,然后才能让相机在整个拖动过程中平稳移动。

当前对相机的所有操作都是通过具有 InputProcessor 接口的类完成的。

处理相机拖动的方法有#touchDown和#touchDragged。 #touchDragged 考虑了由#scrolled 操作的地图的缩放。

/**
 * Sets values at which mouse initially clicked to be used for moving the camera about
 *
 * @param screenX - The x coordinate, origin is in the upper left corner
 * @param screenY - The y coordinate, origin is in the upper left corner
 * @param pointer
 * @param button
 * @return
 */
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (button != 0) return false;
    pressedX = screenX;
    pressedY = screenY;
    return false;
}

/**
 * In the event of a drag, the camera position is altered to correspond to a drag relative to the initial click
 *
 * @param screenX - The x coordinate, origin is in the upper left corner
 * @param screenY - The y coordinate, origin is in the upper left corner
 * @param pointer
 * @return
 */
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    final float zoomMultiplier = camera.zoom / 20f;
    camera.position.set(map.getWidth() - screenX * zoomMultiplier, screenY * zoomMultiplier, 0);
    return false;
}

/**
 * Zooms into or out of the map based on which direction the user scrolls
 * @param amount - The intensity of the scroll
 * @return
 */
@Override
public boolean scrolled(int amount) {
    final float zoomAmount = amount * 1.15f;
    camera.zoom += zoomAmount;
    return false;
}

缩放似乎不会影响拖动动作的正确性,因为它已被考虑在内。但是,初始点击似乎没有按预期工作,因为它的初始跳转并跳入了一个看似随机的区域。

这可能是什么原因造成的?

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    你试过GestureDetector.GestureListener.pan吗?

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        camera.translate(deltaX, -deltaY, 0);
        camera.update();
        return false;
    }
    

    您仍然需要使用缩放级别,但如果它是 1:1,这应该与拖动本身一样拖动确切的量。但是,直到真正触发pan 需要一点时间/移动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多