垂直等距平移限制不起作用

时间:2017-11-24 03:21:00

标签: libgdx

我正在用LibGDX构建等距游戏。我试图找出平移限制,因此用户无法平移屏幕上的等距地图。限制也需要依赖于缩放。

我找到了以下用于平移限制的正交自顶向下游戏的代码:

    float pxWidth =((TiledMapTileLayer) tiledMap.getLayers().get("Background")).getWidth() * ((TiledMapTileLayer) tiledMap.getLayers().get("Background")).getTileWidth();
    float pxHeight = ((TiledMapTileLayer) tiledMap.getLayers().get("Background")).getHeight() * ((TiledMapTileLayer) tiledMap.getLayers().get("Background")).getTileHeight();

    float scaledViewportWidthHalfExtent = cam.viewportWidth * cam.zoom * 0.5f;
    float scaledViewportHeightHalfExtent = cam.viewportHeight * cam.zoom * 0.5f;

    float xmax = pxWidth;
    float ymax = pxHeight;

    // Horizontal
    if (cam.position.x < scaledViewportWidthHalfExtent)
        cam.position.x = scaledViewportWidthHalfExtent;
    else if (cam.position.x > xmax - scaledViewportWidthHalfExtent)
        cam.position.x = xmax - scaledViewportWidthHalfExtent;

    // Vertical
    if (cam.position.y < scaledViewportHeightHalfExtent) {
        cam.position.y = scaledViewportHeightHalfExtent;
    }
    else if (cam.position.y > ymax - scaledViewportHeightHalfExtent)
        cam.position.y = ymax - scaledViewportHeightHalfExtent;

    cam.update();

它适用于水平方向,但不适用于垂直方向。垂直方向似乎被半个地图大小偏移。这是一个概述问题的GIF:

enter image description here

如何将正交垂直限制转换为等距垂直限制?

此外,好像相机的y = 0位置位于屏幕中央。

其他可能相关的信息:

  • 平铺为512 x 256像素
  • 使用IsometricTiledMapRenderer
  • 呈现的平铺
  • 与视口设置为屏幕像素一起使用的ExtendViewport
  • 用于平移,缩放等的OrthographicCamera

1 个答案:

答案 0 :(得分:1)

y限制的问题是y = 0是屏幕的中心。这意味着垂直地图边缘不是0ymax,而是-ymax / 2ymax / 2。所以你的最终代码就是:

if (cam.position.y < -ymax + scaledViewportHeightHalfExtent) cam.position.y = -ymax + scaledViewportHeightHalfExtent;
else if (cam.position.y > ymax - scaledViewportHeightHalfExtent) cam.position.y = ymax - scaledViewportHeightHalfExtent;