Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to simulate positioning with procedural generation?

Asked by
VicyX 27
4 years ago

I have a procedural generation code that works perfectly. The only problem is that it only works in generating once in one spot. I would like to make it load like how Minecraft loads chunks(Dont worry im not copying the game just tryna get some experience) where the chunks change there "terrain" based on their position. What im trying to say is im not sure what part of the code to change to simulate the chunk being in a different position to where itll be different. Basically trying to break up a map into 16x16 chunks so that i dont have to render the map all at once(since its super slow to do that). The blocks are 4x4x4 cubes. Here is the module script:

local GenTerrain = {}

function GenTerrain.Gen(Freq,Power)
    local Seed = math.random(1, 10e6)
    local MapLimit = 1000
    local Size = 16
    local RenderDistance = 3

    local function RoundY(YVal)
        local NewVal = math.floor(YVal)
        local Diff = NewVal % 4
        return NewVal - Diff
    end
    for T = 1,RenderDistance do
        for T2 = 1,RenderDistance do
            wait()
            for X = 1,Size do
                for Z = 1,Size do
                    local Y1 = math.noise(
                        (X*Freq)/MapLimit,
                        (Z*Freq)/MapLimit,
                        Seed
                    )
                    local Y2 = math.noise(
                        (X*Freq*.125)/MapLimit,
                        (Z*Freq*.125)/MapLimit,
                        Seed
                    )
                    local Y3 = math.noise(
                        (X*Freq*4)/MapLimit,
                        (Z*Freq*4)/MapLimit,
                        Seed
                    )

                    local HeightVal = (Y1*Y2*Power*Power)+Y3

                    local Block = Instance.new("Part")
                    Block.Size = Vector3.new(4,4,4)
                    Block.Parent = workspace.Blocks
                    Block.BrickColor = BrickColor.new("Earth green")
                    Block.Anchored = true
                    Block.CFrame = CFrame.new(X*4+((T-1)*16*4),RoundY(HeightVal),Z*4+((T2-1)*16*4))

                    local NumTimes = (Block.Position.Y + 10) / 4
                    for i = 1,NumTimes do
                        local Block2 = Instance.new("Part")
                        Block2.Parent = workspace.UnderGroundBlocks
                        Block2.BrickColor = BrickColor.new("Dirt brown")
                        Block2.Size = Vector3.new(4,4,4)
                        Block2.CFrame = CFrame.new(Block.Position.X,Block.Position.Y - i*4,Block.Position.Z)
                        Block2.Anchored = true
                    end
                end
            end
        end
    end
end

return GenTerrain

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Your fundamental problem is that your X and Z loop variables are local coordinates within the chunk, and you're feeding these directly into math.noise without taking into account the chunk location in world space. So every chunk is exactly the same. Your arguments to math.noise need to vary with chunk location, which in your existing code would mean using the block offsets Size*T and Size*T2.

For example, try replacing lines 15-33 in your code example with this snippet to get and idea of what I mean:

    for T = 1,RenderDistance do
        for T2 = 1,RenderDistance do
            wait()
            for X = 1,Size do
                local CX = X + Size * T
                for Z = 1,Size do
                    local CZ = Z + Size * T2
                    local Y1 = math.noise(
                        (CX*Freq)/MapLimit,
                        (CZ*Freq)/MapLimit,
                        Seed
                    )
                    local Y2 = math.noise(
                        (CX*Freq*.125)/MapLimit,
                        (CZ*Freq*.125)/MapLimit,
                        Seed
                    )
                    local Y3 = math.noise(
                        (CX*Freq*4)/MapLimit,
                        (CZ*Freq*4)/MapLimit,
                        Seed
                    )
0
Oh my that actually answered my question perfectly and it works! Thank you so much! VicyX 27 — 4y
Ad

Answer this question