Any ideas on reducing lag in terrain generation?

Posted by l5p4ngl312 on Game Development See other posts from Game Development or by l5p4ngl312
Published on 2011-12-03T07:05:00Z Indexed on 2012/04/02 5:43 UTC
Read the original article Hit count: 269

Ok so here's the deal. I've written an isometric engine that generates terrain based on camera values using 2D perlin noise. I planned on doing 3D but first I need to work out the lag issues I'm having. I will try to explain how I am doing this so that maybe someone can spot where I am going wrong. I know it should not be this laggy.

There is the abstract class Block which right now just contains render(). BlockGrass, etc. extend this class and each has code in the render function to create a textured quad at the given position.

Then there is the class Chunk which has the function Generate() and setBlocksInArea(). Generate uses 2D perlin noise to make a height map and stores the heights in a 2D array. It stores the positions of each block it generates in blockarray[x][y][z]. The chunks are 8x8x128.

In the main game class there is a 3D array called blocksInArea. The blocks in this array are what gets rendered. When a chunk generates, it adds its blocks to this array at the correct index. It is like this so chunks can be saved to the hard drive (even though they aren't yet) but there can still be optimization with the rendering that you wouldn't have if you rendered each chunk separately.

Here's where the laggy part comes in:

When the camera moves to a new chunk, a row of chunks generates on the end of the axis that the camera moved on. But it still has to move the other chunks up/down in the blocksInArea (render) array. It does this by calculating the new position in the array and doing the Chunk.setBlocksInArea():

for(int x = 0; x < 8; x++){
     for(int y = 0; y < 8; y++){
         nx = x+(coordX - camCoordX)*8
         ny = y+(coordY - camCoordY)*8
         for(int z = 0; z < height[x][y]; z++){
             blockarray[x][y][z] = Game.blocksInArea[nx][ny][z];
          }
       }
}

My reasoning was that this would be much faster than doing the perlin noise all over again, but there are still little spikes of lag when you move in between chunks.

Edit: Would it be possible to create a 3 dimensional array list so that shifting of chunks within the array would not be neccessary?

© Game Development or respective owner

Related posts about java

Related posts about terrain-rendering