我正在使用 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();
它适用于水平方向,但不适用于垂直方向。垂直方向似乎偏移了一半 map 大小。这是概述该问题的 gif:
如何将正交垂直限制转换为等距垂直限制?
此外,相机的 y=0 位置似乎位于屏幕中央。
其他可能相关的信息:
- 图 block 尺寸为 512 x 256 像素
- 使用 IsometricTiledMapRenderer 渲染的图 block
- ExtendViewport 与设置为屏幕像素的视口(viewport)一起使用
- OrthographicCamera 用于平移、缩放等。
最佳答案
y 限制的问题在于 y=0 是屏幕的中心。这意味着垂直 map 边缘不是 0
和ymax
,但它们是 -ymax / 2
和ymax / 2
。所以你的最终代码将是这样的:
if (cam.position.y < -ymax + scaledViewportHeightHalfExtent) cam.position.y = -ymax + scaledViewportHeightHalfExtent;
else if (cam.position.y > ymax - scaledViewportHeightHalfExtent) cam.position.y = ymax - scaledViewportHeightHalfExtent;
关于libgdx - 垂直等距平移限制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465881/