【发布时间】:2013-08-01 02:15:16
【问题描述】:
我正在尝试将相机设置为聚焦在中心的玩家身上,因为它会越过固定到位的 TiledMap。我正在为相机使用这个渲染代码:
OverworldWorld world;
Player player;
OrthographicCamera cam;
SpriteBatch batch;
TiledMap map;
TileAtlas tileAtlas;
TileMapRenderer tmr;
public Renderer(OverworldWorld world){
this.world = world;
world.setRenderer(this);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
new TiledLoader();
map = TiledLoader.createMap(Gdx.files.internal("maps/island.tmx"));
tileAtlas = new TileAtlas(map, Gdx.files.internal("maps/"));
tmr = new TileMapRenderer(map, tileAtlas, 16, 16);
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false, width, height);
cam.position.set(0, 0, 0);
cam.update();
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.projection);
sr = new ShapeRenderer();
}
public void render(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
player = world.getPlayer();
cam.position.set(player.getPosition().x, player.getPosition().y, 0);
cam.update();
tmr.getProjectionMatrix().set(cam.combined);
Vector3 tmp = new Vector3();
tmp.set(0, 0, 0);
cam.unproject(tmp);
tmr.render((int) tmp.x, (int) tmp.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
结果是玩家被锁定在地图的左下角,移动玩家会使相机在地图上移动。这个动作非常基础:
player = world.getPlayer();
switch(keycode){
case Keys.W:
player.getVelocity().y = 1;
player.direction = "up";
break;
并使用此更新玩家位置(这是 0.9.8 版本):
position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * speed));
是什么导致了这个问题,如何解决?
【问题讨论】:
-
不知道为什么你的精灵没有移动,我正在处理它......但是如果你想让相机直接在玩家上方居中,你应该添加一半的宽度和一半的高度到他的位置并将其传递给相机,因为 LIBGDX 通常会从左下角向上渲染一个精灵。
-
玩家开始时以摄像机为中心,但相对于地图始终处于同一位置,当玩家“移动”时,摄像机在地图上移动,使玩家偏离中心。