【问题标题】:How to correct aspect ratio of tiled map rendering in libgdx?如何纠正libgdx中平铺地图渲染的纵横比?
【发布时间】:2013-06-11 08:59:17
【问题描述】:

首先,对情况进行一些说明。

初始化平铺地图代码:

map = new TmxMapLoader().load("maps/map.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);

渲染代码:

mapRenderer.setView(cam);
mapRenderer.render();

问题。我有方形地图和非方形屏幕。地图填满整个屏幕。由于该地图缩放不正确(根据屏幕的 X 和 Y 缩放比例,32x32 单元格转换为 32x40 单元格)。如何在不填满整个屏幕的情况下渲染地图?

【问题讨论】:

  • 我们需要查看您的相机设置。我的猜测是您的相机视口定义不正确。
  • 我的相机是:cam = new OrthographicCamera(MyWorld.CAMERA_WIDTH, MyWorld.CAMERA_HEIGHT);
  • MyWorld.CAMERA_WIDTHMyWorld.CAMERA_HEIGHT的值是?
  • 正如官方网站所说,MyWorld.CAMERA_WIDTH、MyWorld.CAMERA_HEIGHT 解析与屏幕尺寸相关。在我的情况下 CAMERA_WIDTH = CAMERA_HEIGHT = 10。10 是平铺地图行中的单元格计数

标签: android libgdx tiled


【解决方案1】:

根据问题上的 cmets...

您在非方形屏幕上使用方形相机并忽略纵横比。要考虑纵横比,请执行以下操作:

float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();

MyWorld.CAMERA_HEIGHT = 10;  // Fill the height of the screen
MyWorld.CAMERA_WIDTH  = 10 * (w / h); // Account for the aspect ratio

camera = new OrthographicCamera(MyWorld.CAMERA_WIDTH, MyWorld.CAMERA_HEIGHT);

【讨论】:

  • 非常感谢。但是舞台呢?我可以使用“new MyWorld(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),...)”吗?或者我应该初始化方形阶段? MyWorld 扩展舞台
  • 如果您愿意,您的舞台可以使用实际的屏幕尺寸。您可能也不想要方形舞台。
猜你喜欢
  • 2020-08-24
  • 2014-10-23
  • 2016-06-04
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多