fenomas / noa

Experimental voxel game engine.
MIT License
616 stars 91 forks source link

Strange terrain generation #3

Closed andrewtheone closed 8 years ago

andrewtheone commented 8 years ago

Hey, merry christmas to you!

I'm trying to create a new world generation algorithm, I'd like to have a 200*200 flat space, the rest of the world should be "air" (hence: players could fall from the edges) My generation script is:

for(var i = 0; i < data.shape[0]; i++) {
        for(var j = 0; j < data.shape[1]; j++) {
            for(var k = 0; k < data.shape[2]; k++) {

                if((Math.abs(x) > 100) || (Math.abs(z) > 100)) {
                    data.set(i,j,k, 0);
                } else {

                    if((j >= -10) && (j < 1)) {
                        data.set(i,j,k, grassID);
                    } else {
                        data.set(i,j,k, 0);
                    }
                }

            }
        }
    }

but I get strange effects, any idea?

fenomas commented 8 years ago

In that function, x,y,z is the world-space location of each chunk and i,j,k is the local position inside the chunk. So to set each block based on its global position, you want to look at i+x, j+y, k+z.

Try something like this:

    function decideBlock(x,y,z) {
        // here xyz are world positions
        if ((Math.abs(x) > 100) || (Math.abs(z) > 100)) return 0
        if ((y >= -10) && (y < 1)) return grassID
        return 0
    }

    for(var i = 0; i < data.shape[0]; i++) {
        for(var j = 0; j < data.shape[1]; j++) {
            for(var k = 0; k < data.shape[2]; k++) {
                var block = decideBlock(x+i, y+j, z+k) // not (x,y,z) or (i,j,k) !
                data.set(i,j,k, block)
            }
        }
    }