# Help with T-Junctions!

## I’m Building a Dungeon Generator - Help with T-Junctions!

I’m creating a cool algorithm that builds dungeons! I’ve got the basic layout and paths figured out using pathfinding algorithms like A*. Now I’m stuck on a tricky part: hallways with two paths.

Imagine a hallway that connects two different corridors in the dungeon. These corridors could be coming from any direction (north, south, east, west). My problem is figuring out which way this hallway should face to smoothly connect both paths and make sense for the player’s exploration.

``````local hallwayType = function(d1:Vector3, d2:Vector3, NodesEnteringCurrent:table)
local LeftVector = Vector3.new(-1,0,0)
local RightVector = Vector3.new(1,0,0)

local BackVector = Vector3.new(0,0,1)
local LookVector = Vector3.new(0,0,-1)

--print(d1, d2)

if #NodesEnteringCurrent > 1 then
if(d2 == nil and (d1 == LookVector or d1 == BackVector)) or (d1 == d2) then
if (NodesEnteringCurrent[1] == LookVector and NodesEnteringCurrent[2] == BackVector) or (NodesEnteringCurrent[1] == BackVector and NodesEnteringCurrent[2] == LookVector) then
return "Front"
end

if (NodesEnteringCurrent[1] == RightVector and NodesEnteringCurrent[2] == LeftVector) or (NodesEnteringCurrent[1] == LeftVector and NodesEnteringCurrent[2] == RightVector) then
return "Front"
end
end

return "Junction"
else
if(d2 == nil and (d1 == LookVector or d1 == BackVector)) or (d1 == d2) then
return "Front"
elseif (d1 == RightVector and d2 == LookVector) or (d1 == LeftVector and d2 == BackVector) or (d1 == LookVector and d2 == LeftVector) or (d1 == BackVector and d2 == RightVector) then
return "Left"
elseif (d1 == LookVector and d2 == RightVector) or (d1 == BackVector and d2 == LeftVector) or (d1 == RightVector and d2 == BackVector) or (d1 == LeftVector and d2 == LookVector) then
return "Right"
end
end

return nil
end

local createHallWays = function(path:table, totalPathNodes:table, nodePlaced:table)
local startNode, EndNode

for i, Node:Node in pairs(path) do
if nodePlaced[Node.Position] or Node.Type ~= "Air" then continue end
nodePlaced[Node.Position] = true
local direction
local d1, d2
local HWType

if not path[i-1] or not path[i+1] then
HWType = "Front"
else
d1 = (path[(i+1)].Position - Node.Position).Unit
d2 = (Node.Position - path[(i-1)].Position).Unit
HWType = hallwayType(d1, d2, totalPathNodes[Node.Position])
end

if not HWType then return false end

local Lookat
local cube

if totalPathNodes[Node.Position] and #totalPathNodes[Node.Position] > 1 and HWType ~= "Front" then
print(totalPathNodes[Node.Position])
if #totalPathNodes[Node.Position] >= 3 then
cube = RepStorage.HallWays.Intersection:Clone()
elseif #totalPathNodes[Node.Position] == 2 then
if path[i+1].Type == "Air" then
--if direction == "Left" then
--	cube = RepStorage.HallWays.LeftJunction:Clone()
--elseif direction == "Right" then
--	cube = RepStorage.HallWays.RightJunction:Clone()
--end
Lookat = path[i-1].Position
else
Lookat = path[i+1].Position
end

cube = RepStorage.HallWays.Junction:Clone()
end
else
if HWType == "Front" then
cube = RepStorage.HallWays.HallWay1:Clone()
elseif HWType == "Left" then
cube = RepStorage.HallWays.LeftHallWay:Clone()
elseif HWType == "Right" then
cube = RepStorage.HallWays.RightHallWay:Clone()
end
Lookat = path[i-1].Position
end

if not cube then print(HWType) warn("No Hallway Found") return end

if not startNode then startNode = cube else EndNode = cube end
--cube.Name = "Hallway "..i
--cube.Size = Vector3.new(12,12,12)
--cube.Position = Node.Position
cube:PivotTo(CFrame.new(Node.Position, Lookat))
--cube.Anchored = true
--cube.Material = Enum.Material.SmoothPlastic
--cube.CanCollide = false
--cube.BrickColor = BrickColor.new("Dark stone grey")
cube.Parent = workspace.Dungeon.HallWays
CreateDoor(startNode)
if EndNode then CreateDoor(EndNode) end
wait()
end

--print(startNode, EndNode)
end
``````
2 Likes

So I ended up solving it, by tracking every direction going in and out from the Node that the Junction will occupy, Since there are only 3 connections on a T junction, I just track the direction from the T-Junction Node to the nodes that will be connecting it.

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