So I’m working on a chunk system and I don’t know how to combine two chunks.
Here’s a picture:
How would I think about doing this?
So I’m working on a chunk system and I don’t know how to combine two chunks.
Here’s a picture:
How would I think about doing this?
I don’t know much about chunk generation but it looks like you generated the same chunk twice, was that intentional?
It was intentionally. I tried generating two random chunks and it didn’t work.
You need to generate the chunks in a manner that would still match up even when generated independently. This means using no information from nearby chunks even if they’re already generated, otherwise the chunk can come out looking different depending on what order they were generated in.
math.noise
is good for this. It will return a number that smoothly matches up with nearby values, with no other information other than the location. Your example already looks like it uses math.noise
, just use it relative to the other chunk’s position instead of the center chunk’s position again.
How would I do that? Would I have to add up both noises?
You have to figure out where the tile is in “world” space. There can be only one tile on {1,1}, only one on {100,75} etc.
When you generate the tile, you take where it would end up in the world, and make its height based on the noise value of that location.
In your example, you have two chunks, let’s say that their chunk grid coordinates are {0,0} and {1,0}
Each chunk has 64 tiles to each dimension. The tiles go from {0,0} to {63,63} within the chunks, the former being top left and latter being bottom right
You loop over each of the tiles. You do something like math.noise(tileX, tileY)
to get the tile’s height.
However, this approach doesn’t work for the {1,0} tile, because it will look identical to the {0,0} tile like this! The exact same calculations are being done for every chunk. This is what you have right now. You were generating the tiles {0,0} to {63,63} multiple times, but putting them where they don’t belong.
So you use the global tile X for math.noise
.
Instead of the top left part of the second chunk having tileX = 0, tileY = 0
, exactly like the first tile,
You will need it to be tileX = 64, tileY
= 0, and the tiles within the chunk to go from {64,0} to {127,63}
Because the top rightmost tile of the first chunk, {63,0}, must be right beside the top leftmost tile of the second chunk, {64,0}!
local chunkW = 32 -- chunk size in tiles
local chunkH = 32
local tileW = 4 -- tile size in studs
local tileH = 4
local seed = math.random(2 ^ 32)
local scale = 1/16 -- to easily adjust how fine the terrain is
local tallness = 10
function getHeight(x, y)
return math.noise(x * scale, y * scale, seed)
-- seed is used as the Z coordinate here. like picking a position from a random faraway land, except this land is underground, so to speak, and can't be found on any other seed
end
function drawChunk(chunkX, chunkY)
-- chunkX, Y are the chunk positions in chunk grid space, not world stud space
-- tileX, Y are tile positions within the chunk, from 0 to chunkW-1
-- globalX, Y are tile positions in the world. there can be a tile for every x,y position and their heights all are similar to nearby tiles' heights, regardless of which chunk they are from
for tileY = 0, chunkH-1 do -- 0 to 31
for tileX = 0, chunkW-1 do
local globalX = (chunkX * chunkW) + tileX -- to the tile's position, add the contribution of the chunk's position, to get the global position
local globalY = (chunkY * chunkH) + tileY
drawTile(globalX, globalY, getHeight(globalX, globalY))
end
end
end
local base = tallness * 2.5
function drawTile(globalX, globalY, height)
height = base + height * tallness -- to stop it from going under 0
local position = Vector3.new(globalX * tileW, height / 2, globalY * tileH)
local size = Vector3.new(tileW, height, tileH)
-- create your part here
end
How would I make two chunks that connect? That code is basically the same thing as my code.
Assuming chunks are 3x3, these are the math.noise values each tile would use on your terrain:
3,1 isn’t adjacent to 1,1 in math.noise and will look disconnected. All your terrain will look like it’s the first chunk repeated over and over.
This is what it should look like:
3,1 is adjacent to 4,1 and looks connected. The terrain will be unique everywhere.
When you set the position of a part with math.noise, take note of what arguments math.noise is getting. Make the name of the part whatever you put into math.noise to see it more clearly. You will find that parts on the edges of the chunks will have very different numbers from the parts on the edges of another chunk. But within a chunk, the numbers will be similar, which is why they are connected smoothly within the chunk, but not between chunks.
Your problem is that you’re using the same coordinates as inputs to math.noise
. You shouldn’t loop back to x=0
when you reach the next chunk, just continue with the next x
value.
That graph makes sense now thanks for the graph. So you have to find adjacent tiles and then draw them out. Before I was getting tiles that were away from each other.