Procedural island generation

@dispeller your game is cool but it’s not really much like what I’m looking for. I’m not asking for an entire script, either. Just some pseudo code.

I want smooth terrain island generation that can spawn little monuments on islands, trees, rocks, overhangs, hills, holes like in the example in the OP, etc

@Hexcede I’ve already tried to Google but so far can’t find much that will help me. How would I even generate a basic island shape?

For object spawning RayCasting would most likely do what you want. Roblox’s Random object can also help.

Determining islands’ center points would probably done the same way. Then you can determine a basic shape for the island using math.noise (probably using some kind of threshold), give it some heights again with math.noise, and give it some rotation.

Then you can use some smoothing to smooth the edges of the terrain and give it some curves (or maybe spikes) on the bottom of the terrain. Again you can smooth this out so it matches up better with the island.

1 Like

I’m still confused about how I can get a circular shape without it being a perfect circle?

Well math.noise gives you smooth values between x, y, and z changes so your island can use a threshold value from math.noise to determine the island shape. That’ll give you a relatively roundish shape.

1 Like

Could you give some pseudo code - I still don’t really understand it.

Basics of using Perlin Noise (2D Based) This thread is perfect for learning about perlin noise. it also contains psuedo code

1 Like

I was just looking at that post, but I still don’t understand this:

I’m still confused about how I should make the island with that and where I get Z from.

The X, Y, and Z of the island position can be your noise inputs according to the docs math.noise returns a value between -0.5 and 0.5. You can have a threshold to determine the shape of your island. For example, if the value is above 0.4, you could generate terrain there.

Most terrain generation uses perlin or simplex noise. These noise generators produce smooth values very much like hills or mountains.

But how do I get a circular shape? Wouldn’t this make a cube? Plus I don’t want perfect circles:

(The hand-made islands I did, not the best)

No it would not give a cube. Try visualizing the values returned by math.noise. make some parts for a 2d range in a map and then set their height to the output of the noise function.

Noise is random, but it is basically smooth randomness.

When generating your heightmap, you can use this function at each coordinate to apply a circular gradient:

function applyGradient(heightMapSize, x, y, z)
    local distance_x = math.abs(x - heightMapSize * 0.5)
    local distance_y = math.abs(z - heightMapSize * 0.5)
    local distance = math.sqrt(distance_x*distance_x + distance_y*distance_y)

    local max_width = heightMapSize * 0.5 - 10.0
    local delta = distance / max_width
    local gradient = delta * delta

    return y * math.max(0.0, 1.0 - gradient)
end
6 Likes

So, math.noise gives one value back, I could use that to make a perfect circle. How could I make it more of an oval?

You can use a threshold. If the value returned is high enough you could generate terrain there. You can adjust the threshold and the higher the number the smaller area will be generated. Too low, terrain will connect. Too high, terrain will be too small.

1 Like

How can I translate this into smooth terrain? I’m trying now but it’s not going well :joy:

The value returned would be used for terrain height. For 2d terrain, like the topbof your island, the z value isn’t usually used.

When I get a chance I’ll show you what my game’s terrain code looks like right now

How would I even generate the cylinder/oval shape and then round it off? I have literally never done this type of thing before sorry :joy:

You would use a threshold value. The noise function when graphed looks like hills. That’s why it’s used in terrain generation. If you cut the top off of a hill it’ll look kind of circular.

Please could you get that code snippet you said about? I’m so confused :joy:

Sorry if I’m being annoying but this is making 0 sense to me

Despite the optimization benefits, I’ve personally always found Roblox terrain textures very unoriginal, so I generally don’t use it.
I might use it for water sometimes but that’s about it.

I guess I should clarify what I meant by cframing.

It’s fairly easy to cframe terrain. Using Terrain:FillBlock(cframe,size,material) and Terrain:FillBall(position,radius,material) you can place chunks of terrain anywhere.
https://developer.roblox.com/en-us/api-reference/function/Terrain/FillBlock
https://developer.roblox.com/en-us/api-reference/function/Terrain/FillBall

If you’re doing a proof of concept, then working out the math would be really cool.

If you’re trying to build a game however, I recommend going the easier rout of taking an island made out of Parts and converting said island into terrain with a function similar to this:

function fill(part,material)
	if part.Shape == 'Ball' then
		Terrain:FillBall(part.Position,part.Size.Y/2,Enum.Material[material])
	else
		Terrain:FillBlock(part.CFrame,part.Size,Enum.Material[material])
	end
end

function convertIslandToTerrain(model)
	for i,v in pairs(model:GetChildren()) do
		if v.Name == 'Grass' then
			Terrain:FillBlock(v.CFrame,v.Size,Enum.Material.Grass)
			v:Destroy()
		elseif v.Name == 'Rock' then
			Terrain:FillBlock(v.CFrame,v.Size,Enum.Material.Rock)
			v:Destroy()
		end
	end
end

From personal experience, unless you’re really into the math itself, the end product will be very similar.

This is not to say that your islands can not be random.
You can still have lots of randomness if you take a bunch of premade islands and mixed them together.

Again, the product will be very similar.
That’s my personal opinion on the matter.
Hope this helps ¯\(ツ)/¯

2 Likes

This will give a very blocky feel because you’re just converting a cube part to terrain. I don’t think it’s going to work for my use case. Plus, I want all the islands to be different.