在Java中遇到2d地形生成器的问题

时间:2013-10-01 03:49:07

标签: java terrain procedural-generation

由于某种原因,块保持在同一位置呈现。有人能帮助我吗?

Block[][] chunk = new Block[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS];
    float[][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS];
    float frequency = 1.0f / (float) chunk.length; 

    for (int x = 0; x < chunk.length - 1; x++) 
    { 
        for (int y = 0; y < chunk[x].length - 1; y++) 
        { 
            positions[x][y] = SimplexNoise.Generate((float) x * frequency, (float) y * frequency);
            g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);
        } 
    } 

    for (int x = 0; x < Chunk.CHUNK_WIDTH_BLOCKS; x++)
    {
        for (int y = 0; y < Chunk.CHUNK_HEIGHT_BLOCKS; y++)
        {
            if (positions[x][y] < 0f)
                chunk[x][y] = new Block();
            if (positions[x][y] >= -0f)
                chunk[x][y] = new Block();
        }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在多个问题。例如:

for (int x = 0; x < chunk.length - 1; x++) 

应该是:

for (int x = 0; x < chunk.length; x++)

另外,请考虑以下事项:

g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);

这不会使用“position [x] [y]”中的所有值....我认为你想要的是数组为3d ...例如:

float[][][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS][2];

那样:posistions [x] [y] [0]是x的值,而位置[x] [y] [1]是y的值......

g.drawRect(positions[x][y][0], positions[x][y][1], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);

我不确定我是否完全理解您的代码,但似乎确实存在问题。