Terrain generation, what and how?
Posted on March 27, 2017 by RubenKan
What is terrain generation and how does it work?
The key to terrain generation is math.noise
. The most important thing to note from the start, math.noise
is not random. (From now on i'll reference math.noise
as just "noise".)
What is noise?
The function uses a Perlin noise algorithm to assign fixed values to coordinates. (Same input(s), same output.)
How do we use it?
The noise function takes 3 arguments, but only one is required.
math.noise (x [, y [, z]])
is how roblox describes it on their wiki, wich is what confuses most people. The correct way to get noise working for a terrain generator is y = math.noise(x,z,seed)
.
How can we use it to make terrain?
It's simple.
- Set a seed (For this example, 42, as it is the answer to life, the universe and everything.)
- Create a for loop for the X coordinate.
- Create a for loop for the Z coordinate.
- Create parts
- Call
local Y = math.noise(X,Z,42)
- Set the part position to X,Y,Z.
And thats it!
Now hold up a seccond, this doesn't look smooth at all, thats just a flat base! - That is correct.
The reason why it looks like a flat base, is because all of the values are integers. If all values are integers, math noise will return 1. If we devide the X and Z coordinates by 20, you'll notice it starts to get nice and smooth like your roblox smooth terrain looks like, but the parts are still very close to eachother, we can make the terrain have a bit more depth by multiplying the Y value, lets say by 5. (local Y = math.noise(X/20,Z/20,42) * 5
)
Into the scripts
How would a script look like to run this? result
local Seed = 42 -- The answer to life, the universe and everything. for X = 0,50 do -- X Loop for Z = 0,50 do -- Z Loop -- Make the part local p = Instance.new("Part",workspace) p.Anchored = true p.Size = Vector3.new(1,1,1) --Just for style p.TopSurface = "Smooth" p.Material = Enum.Material.Grass p.BrickColor = BrickColor.Green() --The noise local Y = math.noise(X/20,Z/20,Seed) * 5 --Set the position p.CFrame = CFrame.new(X,Y,Z) end --Wait a bit so your toaster doesn't freeze. wait() end --Tfw you write all of this and it works in one try.
Well, that's about all there is to it. Experiment around with it and comment what you've made with this.
Commentary
Leave a Comment