Help creating artificial terrain

Hello fellow developer!

I’'ve written a script for a project i’ve been working on. The goal is to acheive artificial randomly generated terrain each time you enter the game.
So far, i’ve made good progress with a system that creates nodes that then get connected.


(Sorry for the lag, studio issue)

I have yet to do the hard part. I need to connect these nodes using triangles. I’ve attempting doing a similar system to my connections, but that failed. I cant seem to find any devforum articles that are similar to mine.
Heres a copy of the code:

nodezero = workspace.Nodes.Node0
local Nodenum = 0
lastnode = nodezero

while Nodenum < 677 do -- Creates and positions nodes
	Nodenum = Nodenum + 1
	
	newnode = nodezero:Clone()
	newnode.Parent = workspace.Nodes
	newnode.Name =  "Node" .. tostring(Nodenum)
	
	x = lastnode.Position.X + 20
	y = math.random(0, script:GetAttribute("Intensity"))
	z = lastnode.Position.Z
	
		if math.floor(Nodenum / 26) == Nodenum / 26 then
		
		x = -244
		z = lastnode.Position.Z + 20
		
	end
	
	newnode.Position = Vector3.new(x,y,z)
	
	lastnode = newnode
	
end

Nodenum = 0

while Nodenum < 676 do -- places and positions X connections
	local a = workspace.Nodes['Node' .. tostring(Nodenum)].Position
	local b = workspace.Nodes['Node' .. tostring(Nodenum + 1)].Position

	local distance = (a-b).magnitude
	local midPosition = (a+b)/2
	local UP = Vector3.new(0,1,0)

	local thickness = 1
	if distance < 150 then
		
		local part = Instance.new("Part")
	part.Anchored = true
	part.Color = Color3.new(0, 1, 0)

	part.Size = Vector3.new(thickness, thickness, distance)
	part.CFrame = CFrame.lookAt(midPosition, b, UP)

	part.Parent = workspace
	end
	
	
	Nodenum = Nodenum + 1
	
end
Nodenum = 0
while Nodenum < 650 do -- places and positions Z connections.
	local a = workspace.Nodes['Node' .. tostring(Nodenum)].Position
	local b = workspace.Nodes['Node' .. tostring(Nodenum + 26)].Position

	local distance = (a-b).magnitude
	local midPosition = (a+b)/2
	local UP = Vector3.new(0,1,0)

	local thickness = 1
	if distance < 150 then

		local part = Instance.new("Part")
		part.Anchored = true
		part.Color = Color3.new(0, 1, 0)

		part.Size = Vector3.new(thickness, thickness, distance)
		part.CFrame = CFrame.lookAt(midPosition, b, UP)

		part.Parent = workspace
	end


	Nodenum = Nodenum + 1

end

I hope this problem can be sorted out and im willing to provide more information in the future.

3 Likes

I used this module to make triangles from 3 points. Since yours is a grid, you will create two triangles for each square.

image

As you iterate through the grid, you will first find the points to create the triangle. For the image above, it should be something like this:

--pseudocode
for x, z in grid do

    --skip if the points are on the top-right edge since any triangles formed
    --from these points will be out of bounds
    if IsEdge(x, z) then continue end 

    --first triangle
    local t1_1 = (x, z) --current point
    local t1_2 = (x+1, z) --point to the right
    local t1_3 = (z+1, x) --point above

    --second triangle
    local t1_1 = (x+1, z+1) --point to the top right
    local t1_2 = (x+1, z) --point to the right
    local t1_3 = (z+1, x) --point above

    local triangle1 = TriangleFromPoints(t1_1, t1_2, t1_3)
    local triangle2 = TriangleFromPoints(t2_1, t2_2, t2_3)
end

And this is what it should look like (an example from my own project)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.