【问题标题】:Libgdx translation weirdLibgdx 翻译很奇怪
【发布时间】:2015-09-14 20:43:03
【问题描述】:

在我正在编写的游戏中,我需要一个巨大的世界。所以我把它分成了 12x12 块(1 个单元)。

要渲染一个块,我想使用局部坐标:对于每个块,我希望 0;0 位于块的左下角。

在绿色世界坐标系中,1;1 是 1 个块,块也一样,只是原点在变化。

为此,我在 SpriteBatch 中使用了一个转换矩阵,该矩阵转换为 chunk_x * 块大小和 chunk_y * 块大小。但是对于块 y=-1,它的行为很奇怪。

这是世界渲染代码:

public void render() {
    batch.setProjectionMatrix(camera.combined);
    renderer.setProjectionMatrix(camera.combined);
    Matrix4 matrix4 = new Matrix4();
    for(Chunk chunk : Collections.unmodifiableCollection(loadedChunks.values())) {
        matrix4.setToTranslation(chunk.getX() * Chunk.SIZE, chunk.getY() * Chunk.SIZE, 0);
        batch.setTransformMatrix(matrix4);
        renderer.setTransformMatrix(matrix4);
        chunk.render(batch, renderer);
    }
}

我以单独的方法更新相机。这是块渲染代码:

public void render(SpriteBatch batch, ShapeRenderer renderer) {
    batch.begin();
    for(EntityBlock block : blockLayer0) {
        block.render(batch);
    }
    for(EntityBlock block : blockLayer1) {
        block.render(batch);
    }
    batch.end();
    renderer.begin(ShapeRenderer.ShapeType.Line);
        renderer.setColor(Color.WHITE);
        renderer.rect(0, 0, SIZE /* SIZE is constants and = 12 */, SIZE);
    renderer.end();
}

我的实体块中的渲染方法:

 @Override
public void render(SpriteBatch batch) {
    batch.draw(texture, get(PositionComponent.class).getX(), get(PositionComponent.class).getY(), 1f, 1f);
}

这是从 (-1;0) 到 (0;1) 的 4 个块的测试图,但我得到了相同的结果:

为了提供有关实体的 getComponent() 方法的更多详细信息,我的实体类扩展了具有映射 Component> 的 ComponentProvider,我向其添加了一个存储 2 个浮点 x 和 y 的 PositionComponent。所以我没有发布代码,因为它可以工作

PS:黑色部分的右边是x = 0。

【问题讨论】:

  • 你在哪里打电话给batch.begin()batch.end()?我真的不明白是什么导致了你的问题。这可能与您设置 EntityBlock 数组的方式有关,而您没有显示。顺便说一句,白色网格只是为了调试吗?因为它破坏了你的批处理。
  • 是的,对不起,我错过了添加一些代码,网格用于调试,我稍后会更新
  • 我会简化代码,只需要四个手动步骤即可完成您认为现在正在做的事情,块 (0,0),(-1,0),(0,-1), (-1,-1) 并查看问题是否仍然存在。我们也看不到 get(PositionComponent.class).getX() 在做什么,这似乎是不可或缺的。
  • 所以我按照你所说的做了一张小地图,它仍然存在(我更新了帖子)。对于 get(PositionComponent.class) 它只存储实体的位置,如果您愿意,我可以执行 entity.getX() 或类似操作,但问题不会通过这种方式出现。我不知道为什么我的负面翻译会这样

标签: java libgdx


【解决方案1】:

所以经过一些实验后,我得出了这样的结论:否定翻译没有按我的意愿工作,并且块的移动量太大(只有 spritebatch、shaperenderer 正常工作)。所以我写了错位,并且使用这段代码它正在工作(在 World.render() 中):

public void render() {
    Matrix4 matrix4 = new Matrix4();
    for(Chunk chunk : loadedChunks) {
        matrix4.setToTranslation((chunk.getX() * Chunk.SIZE) < 0 ? chunk.getX() * Chunk.SIZE + Chunk.SIZE - 1 : chunk.getX() * Chunk.SIZE, chunk.getY() * Chunk.SIZE, 0);
        batch.setTransformMatrix(matrix4);
        matrix4.setToTranslation(chunk.getX() * Chunk.SIZE, chunk.getY() * Chunk.SIZE, 0);
        renderer.setTransformMatrix(matrix4);
        chunk.render(batch, renderer);
    }
}

这是一个临时解决方案,但它有效。

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 2015-05-14
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多