I am trying to create a voxel terrain planet generated using perlin noise.
I have gotten to the point where my terrain generates in a flat rectangular shape. Now I am trying to map that terrain around a spherical shape. Here is what I have so far:
local angle = 0
local layer = 0
local diameter = mapSize
repeat
repeat
local position = CFrame.Angles(math.rad(angle), math.rad(-layer), math.rad(0)) * CFrame.new(0, 0, math.abs(diameter / 2))
local height, material = terrainModule.GetTerrain(position.X, position.Z)
workspace.Terrain:FillBlock(position + Vector3.new(0, (-diameter / 2), 0), Vector3.new(30, 30, 10), material)
angle = angle + 1
until angle == 360
angle = 0
layer = layer + 1
wait()
until layer == 270
The script above creates a perfect sphere. If I change the position used in the FillBlock()
function to blockPosition
:
local blockPosition = CFrame.new(position.X, position.Y + height, position.Z)
workspace.Terrain:FillBlock(blockPosition + Vector3.new(0, (-diameter / 2), 0), Vector3.new(30, 30, 10), material)
The script, of course, only applies the height straight up in the Y direction. So, I need to somehow apply this height value at an angle around the sphere, which would require more than simply adding to position
's Y value. How can I do this?
This is the result of this script:
Here is the same terrain seed except flat to help visualize:
I haven’t taken trigonometry or calculus yet, so the math is unknown to me. If you could explain your answer to me a little more abstract that would be great. Thanks!