Java Libgdx翻译

Java Libgdx翻译,java,libgdx,Java,Libgdx,在我写的游戏中,我需要一个巨大的世界。所以我把它分成12x12块(1个单元)的块 要渲染块,我需要使用局部坐标:对于每个块,我需要0;0将位于块的左下角 在绿色世界合作体系中,1;1是1块,与chunk相同,只是原点在变化 为此,我在SpriteBatch中使用了一个转换矩阵,它有chunk_x*chunk size和chunk_y*chunk size的转换。但是对于块y=-1,它的行为很奇怪 以下是世界渲染代码: public void render() { batch.setPr

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

要渲染块,我需要使用局部坐标:对于每个块,我需要0;0将位于块的左下角

在绿色世界合作体系中,1;1是1块,与chunk相同,只是原点在变化

为此,我在SpriteBatch中使用了一个转换矩阵,它有chunk_x*chunk size和chunk_y*chunk size的转换。但是对于块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();
}
我用另一种方法更新相机。以下是区块渲染代码:

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()方法的更多详细信息,我的实体类扩展了ComponentProvider,它有一个映射Component>,我在其中添加了一个PositionComponent,它存储2个浮点x和y。所以我没有发布代码,因为它是有效的


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

所以经过一些实验后,我得出了这样的结论:否定翻译没有按照我想要的那样工作,而且块移动了太多(只有在spritebatch、ShaperEnder正常工作的情况下)。因此,我编写了错位代码,并使用此代码(在World.render()中)工作:

public void render(){
Matrix4 Matrix4=新Matrix4();
for(块: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);
渲染器。setTransformMatrix(matrix4);
渲染(批处理,渲染器);
}
}

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

所以经过一些实验后,我得出了这样的结论:否定翻译并没有像我所希望的那样起作用,而且块被移动了太多(只有在spritebatch和ShaperEnder正常工作的情况下)。因此,我编写了错位代码,并使用此代码(在World.render()中)工作:

public void render(){
Matrix4 Matrix4=新Matrix4();
for(块: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);
渲染器。setTransformMatrix(matrix4);
渲染(批处理,渲染器);
}
}

这是一个临时解决方案,但它可以工作。

在哪里调用
batch.begin()
batch.end()
?我真的不明白是什么导致了你的问题。这可能是您如何设置EntityBlock数组的问题,您没有显示。顺便问一下,白色网格只是用来调试的吗?因为它会破坏你的批处理。是的,sry我没有添加一些代码,网格是为了调试,我会在以后更新。我会简化代码,只需对块(0,0),(-1,0),(0,-1),(-1,-1)执行四个手动步骤,就可以完成你认为现在正在做的事情,然后看看问题是否仍然存在。我们也看不到get(PositionComponent.class).getX()在做什么,这似乎是一个整体。所以我按照你说的做了一个小映射,它仍然存在(我更新了帖子)。对于get(PositionComponent.class),它只存储实体的位置,若你们愿意的话,我可以做一个entity.getX()或类似的操作,但问题并不是这样来的。我不知道为什么我会这样做,你在哪里调用
batch.begin()
batch.end()
?我真的不明白是什么导致了你的问题。这可能是您如何设置EntityBlock数组的问题,您没有显示。顺便问一下,白色网格只是用来调试的吗?因为它会破坏你的批处理。是的,sry我没有添加一些代码,网格是为了调试,我会在以后更新。我会简化代码,只需对块(0,0),(-1,0),(0,-1),(-1,-1)执行四个手动步骤,就可以完成你认为现在正在做的事情,然后看看问题是否仍然存在。我们也看不到get(PositionComponent.class).getX()在做什么,这似乎是一个整体。所以我按照你说的做了一个小映射,它仍然存在(我更新了帖子)。对于get(PositionComponent.class),它只存储实体的位置,若你们愿意的话,我可以做一个entity.getX()或类似的操作,但问题并不是这样来的。我不知道为什么我会这样做