Paths turning back or intersecting

I’m trying to make a random path generation system however the paths just keep on turning back and intersecting with each other. This isn’t something like a pathfinding system.

This is how it looks like:
Capture

This is how it should look like:
Capture

This is my code:

local origin = Vector3.new(10, 0, 10)
local mainBranchCount = 1 --math.random(8, 12)
local branches = {}
local possibleMultipliers = {-1, 1}

print(mainBranchCount)
for count = 1, mainBranchCount do
	local multiplier = math.random(-1, 1)
	if multiplier == 0 then
		multiplier = multiplier - 1
	end
	local seperation = (180 / mainBranchCount * count - math.random(10, 20)) * multiplier
	local branchLength = math.random(2, 4)
	local branchPosition = CFrame.new(origin):ToWorldSpace(CFrame.Angles(0, count * seperation, 0)):ToWorldSpace(CFrame.new(branchLength / 2, 0, 0))
	local branchPart = Instance.new("Part", workspace)
	branchPart.Material = "SmoothPlastic"
	branchPart.Color = Color3.new(0, 0, 0)
	branchPart.Anchored = true
	branchPart.Material = "Neon"
	branchPart.Transparency = 0.75
	branchPart.Size = Vector3.new(branchLength, 0.1, 0.1)
	branchPart.CFrame = branchPosition
	table.insert(branches, branchPart)
end

print(branches)

for _, branch in pairs(branches) do
	print("run1")
	local points = {}
	local usedUpLength = 0
	local branchLength = branch.Size.X
	local lastPoint = CFrame.new(origin, branch.Position) --CFrame.new(branch.Position):ToWorldSpace(CFrame.new(0, 0, branch.Size.X / 2 * -1))
	local newBranches = 1
	local currentDirection = 4 --90
	local branchLastPoint = branch.CFrame:ToWorldSpace(CFrame.new(branch.Size.X / 2, 0, 0))
	print("run2")
	while true do
		print("run3")
		--local direction = math.random(1, 30)
		--local directionMultiplier = possibleMultipliers[math.random(1, 2)]
		--currentDirection = currentDirection + direction * directionMultiplier
		--direction = direction * directionMultiplier
		local direction = math.random(1, 10)
		--while true do
			--if currentDirection > 130 then
			--	local subtract = math.random(1, 30)
			--	currentDirection = currentDirection - subtract
			--	direction = direction - subtract
			--elseif currentDirection < 50 then
			--	local add = math.random(1, 30)
			--	direction = direction + add
			--	currentDirection = currentDirection + add
			--elseif currentDirection == currentDirection then
			--	print(direction, currentDirection)
			--	break
			--end
			--wait()
		--end
		if currentDirection < 0 then
			currentDirection = 0 + direction
		elseif currentDirection > 0 then
			currentDirection = 0 - direction
			direction = direction * -1
		end
		print("run4")
		local length = math.random(1, 5) / 10
		length = math.clamp(length, 0, branchLength - usedUpLength)
		local branchPart = Instance.new("Part", workspace)
		branchPart.Material = "SmoothPlastic"
		branchPart.Color = Color3.new(0, 0, 0)
		branchPart.Anchored = true
		branchPart.Material = "Neon"
		branchPart.Transparency = 0
		branchPart.Size = Vector3.new(length, 0.05, 0.05)
		branchPart.Name = "branch"..tostring(newBranches)
		branchPart.CFrame = lastPoint:ToWorldSpace(CFrame.Angles(0, direction, 0)):ToWorldSpace(CFrame.new(length / 2, 0, 0)) --lastPoint:ToWorldSpace(CFrame.Angles(0, direction, 0)):ToWorldSpace(CFrame.new(length / 2, 0, 0))
		local part1 = Instance.new("Part", workspace)
		part1.CFrame = branchPart.CFrame
		part1.Color = Color3.new(0, 1, 0)
		part1.Material = "Neon"
		part1.Anchored = true
		part1.Size = Vector3.new(0.01, 0.1, 0.01)
		part1.Name = "branchy"..tostring(newBranches).."a"
		lastPoint = branchPart.CFrame:ToWorldSpace(CFrame.new(branchPart.Size.X / 2, 0, 0))
		local part2 = Instance.new("Part", workspace)
		part2.CFrame = lastPoint
		part2.Color = Color3.new(1, 0, 0)
		part2.Material = "Neon"
		part2.Anchored = true
		part2.Size = Vector3.new(0.01, 0.1, 0.01)
		part2.Name = "branchy"..tostring(newBranches).."b"
		local part3 = Instance.new("Part", workspace)
		part3.CFrame = branchLastPoint
		part3.Color = Color3.new(0, 0, 1)
		part3.Material = "Neon"
		part3.Anchored = true
		part3.Size = Vector3.new(0.01, 0.1, 0.01)
		part3.Name = "branchy"..tostring(newBranches).."b"
		newBranches = newBranches + 1
		local relativeTo = branchLastPoint:ToObjectSpace(lastPoint)
		print(relativeTo)
		if relativeTo.Position.X >= 0 then
			break
		end
		print("run5")
		wait()
	end
end
1 Like

do you keep track of an endpoint to move towards?
do you check that a new path part will intersect with the path before it’s finalized?

i try to track the current rotation to make sure it doesnt go back and intersect with another part. this means that there is no need for intersection detection. if it always goes forward it wotn intersect. i just do it wrong. havent been able to figure out why for 3 days

would probably know if you read the script

Anybody found the solution? ( pretty sure replies put the thread at the top of the posts list )