You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Libgdx相机跟随玩家时瓦片地图异常移动问题求助

问题:相机跟随玩家时玩家看似静止,瓦片地图同步移动

我编写了用于渲染TiledMap、控制玩家Texture移动并实现相机跟随的代码。但移动玩家触发相机移动时,瓦片地图会同步移动,导致玩家看起来完全没有移动。注释掉相机位置设置代码后,玩家可以正常在瓦片地图上移动。

以下是完整代码:

package com.worldgen.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.ScreenUtils;
import com.worldgen.map.WorldMap;

public class WorldGenGame extends ApplicationAdapter {
    private static final int MAP_WIDTH = 512;
    private static final int MAP_HEIGHT = 512;
    private static final int TILE_SIZE = 16;
    public final static float VP_WIDTH = 32;
    public final static float VP_HEIGHT = 18;

    private SpriteBatch batch;
    private OrthographicCamera camera;

    WorldMap worldMap;
    private TiledMapRenderer tiledMapRenderer;

    private Texture playerTexture;
    private Rectangle player;
    
    @Override
    public void create () {
        batch = new SpriteBatch();
        playerTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
        player = new Rectangle(0, 0, 1, 1);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, VP_WIDTH, VP_HEIGHT);

        worldMap = new WorldMap(MAP_WIDTH, MAP_HEIGHT, TILE_SIZE, TILE_SIZE);
        tiledMapRenderer = new OrthogonalTiledMapRenderer(worldMap.getMap(), 1f/TILE_SIZE);
    }

    @Override
    public void render () {
        ScreenUtils.clear(0, 0, 0, 1, true);
        // Update
        float deltaTime = Gdx.graphics.getDeltaTime();
        updateCamera(deltaTime);
        updatePlayer(deltaTime);

        //Draw map
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();

        //Draw player
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(playerTexture, player.x, player.y, player.width, player.height);
        batch.end();
    }
    
    @Override
    public void dispose () {
        batch.dispose();
        worldMap.dispose();
        playerTexture.dispose();
    }

    private void updatePlayer(float deltaTime) {
        float movementAmount = 4 * deltaTime;
        if (Gdx.input.isKeyPressed(Input.Keys.A)) {
            player.x -= movementAmount;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.D)) {
            player.x += movementAmount;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.W)) {
            player.y += movementAmount;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.S)) {
            player.y -= movementAmount;
        }
    }

    private void updateCamera(float deltaTime) {
        camera.position.set(player.getX(), player.getY(), 0);
        camera.update();
    }
}

注释相机位置设置后的代码片段:

private void updateCamera(float deltaTime) {
//      camera.position.set(player.getX(), player.getY(), 0);
        camera.update();
    }

问题原因及解决方法

问题出在更新顺序颠倒:你先调用了updateCamera,再调用updatePlayer。这会导致相机先锁定玩家的旧位置,之后玩家才移动到新位置,渲染时相机视角停留在玩家移动前的位置,视觉上就表现为地图跟着反向移动,玩家看似静止。

修正方法是调换两个方法的调用顺序,先更新玩家位置,再让相机跟随新的玩家位置:

@Override
public void render () {
    ScreenUtils.clear(0, 0, 0, 1, true);
    // Update
    float deltaTime = Gdx.graphics.getDeltaTime();
    updatePlayer(deltaTime); // 先更新玩家位置
    updateCamera(deltaTime); // 再让相机跟随新位置

    //Draw map
    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();

    //Draw player
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(playerTexture, player.x, player.y, player.width, player.height);
    batch.end();
}

另外,为了让玩家始终保持在屏幕中心(更自然的跟随效果),可以把相机位置设置为玩家中心坐标:

private void updateCamera(float deltaTime) {
    camera.position.set(player.getX() + player.width/2, player.getY() + player.height/2, 0);
    camera.update();
}

内容的提问来源于stack exchange,提问作者Sagnik Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:45:35