【问题标题】:Having problems with a 2d terrain generator in JavaJava 中的二维地形生成器有问题
【发布时间】:2013-10-01 03:49:07
【问题描述】:

由于某种原因,这些块一直被渲染在相同的位置。有人可以帮帮我吗?

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();
        }
}

【问题讨论】:

    标签: java terrain procedural-generation


    【解决方案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);
    

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

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

    这样:positions[x][y][0] 是 x 的值,positions[x][y][1] 是 y 的值......

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多