سؤال

For some reason the blocks keep being rendered in the same position. Can anybody help me?

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();
        }
}
هل كانت مفيدة؟

المحلول

You have multiple problems with your code. For instance:

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

That should be:

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

Also, consider the following:

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

That will not use all values in "positions[x][y]".... I think that what you will want is for the array to be 3d... for instance:

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

That way: posistions[x][y][0] is the value for x, and positions[x][y][1] is the value for y....

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

I am not sure I understand your code exactly, but it does seem to have issues.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top