gpt4 book ai didi

java - 使用 Libgdx 更新相机位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:33 25 4
gpt4 key购买 nike

我正在尝试让 OrthographicCamera 跟随用户控制的 Sprite 。我根本无法让相机正确更新位置。与其他人所做的相比,我似乎看不出我的代码有什么问题。

我仍在学习,此时我认为问题是由一些我目前还不完全理解的简单问题引起的。

感谢任何帮助,谢谢。

这是我的渲染器:

public class WorldRenderer {

private static final float CAMERA_WIDTH = 10;
private static final float CAMERA_HEIGHT = 7;

private World world;
private OrthographicCamera oCam;
private Hero hero;
ShapeRenderer debugRenderer = new ShapeRenderer();

/** TEXTURES **/
private Texture heroTexture;
private Texture tileTexture;

private SpriteBatch spriteBatch;
private int width, height;
private float ppuX; // Pixels per unit on the X axis
private float ppuY; // Pixels per unit on the Y axis

public void setSize (int w, int h) {
this.width = w;
this.height = h;
ppuX = (float)width / CAMERA_WIDTH;
ppuY = (float)height / CAMERA_HEIGHT;
}

public WorldRenderer(World world, boolean debug) {
hero = world.getHero();
this.world = world;
spriteBatch = new SpriteBatch();
oCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
oCam.update();
loadTextures();
}


private void loadTextures() {
tileTexture = new Texture(Gdx.files.internal("images/tile.png"));
heroTexture = new Texture(Gdx.files.internal("images/hero_01.png"));
}

public void render() {
oCam.update();
spriteBatch.begin();
spriteBatch.disableBlending();
drawTiles();
spriteBatch.enableBlending();
drawHero();
spriteBatch.end();
}

private void drawHero() {
spriteBatch.draw(heroTexture, hero.getPosition().x * ppuX, hero.getPosition().y * ppuY, Hero.SIZE * ppuX, Hero.SIZE * ppuY);
oCam.position.set(hero.getPosition().x, hero.getPosition().y, 0);
}
}

最佳答案

SpriteBatch 管理自己的投影和变换矩阵。因此,您必须设置它的矩阵(如果可能,在调用 begin() 之前)。

除非您需要单独访问矩阵(投影和模型 View ,例如在着色器中),否则将投影矩阵设置为投影模型 View 矩阵就足够了。

无论如何,这应该适用于您的代码:

oCam.update();
spriteBatch.setProjectionMatrix(oCam.combined);

关于java - 使用 Libgdx 更新相机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com