Hello,
I have recently made a wall creating system that works pretty fine but generating the corners between two walls are so much pain…
I have figured out a solution for that which is clearly not “prefect” for my case
As you can see below i run up into these kind of problems:
Here is the preview of the code where cornering comes up:
local function InstantiatePart()
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = true
local m = Instance.new("BlockMesh")
m.Name = "BlockMesh"
m.Parent = part
return part
end
function GenerateWall(props)
local part = InstantiatePart()
part.Name = "Wall"
part.CFrame = props.CFrame
part.Size = props.Size
part.Color = props.Color
return part
end
function Make2Sided(part)
local cf = part.CFrame
local size = part.Size
local m =Instance.new("Model")
m.Name = "WallSides"
m.Parent = part.Parent
local side1 = part:clone()
side1.Size = side1.Size - Vector3.new(size.X/2,0,0)
side1.CFrame = cf * CFrame.new(side1.Size.X/2,0,0)
side1.Parent = m
side1.Name = "InsideWall"
--side1.BrickColor = BrickColor.Red()
local side2 = part:clone()
side2.Size = side2.Size - Vector3.new(size.X/2,0,0)
side2.CFrame = cf * CFrame.new(-side2.Size.X/2,0,0)
side2.Parent = m
side2.Name = "OutsideWall"
--side2.BrickColor = BrickColor.Green()
for _, corners in pairs(part:GetChildren()) do
if corners.Name:sub(1,6) == "Corner" then
corners.Parent = side2
end
end
part:Destroy()
return m
end
--CONNECTING WALLS
function getWallPoints(wall)
local fp, bp
fp = wall.Position + wall.CFrame.lookVector * wall.Size.Z/2
bp = wall.Position - wall.CFrame.lookVector * wall.Size.Z/2
return fp, bp
end
function EdgesNearby(OEdge, FEdge)
local r = false
local mag = (OEdge - FEdge).magnitude
if mag <= 2 then
r = true
end
return r
end
function ConnectPoints(wallCopy, pA, pB)
local length = (pA - pB).magnitude
local p = wallCopy:clone()
p.Parent = wallCopy
p.Name = "Corner"
p.Size = Vector3.new(p.Size.X*2, p.Size.Y, p.Size.X*2)
p.CFrame = CFrame.new(pA, pB + Vector3.new(0.00001,0,0)) --* CFrame.new(-(p.Size.X*2)/4 + p.Size.X/2,0,0)
return p
end
function FindEdgeConnections(wall, walls)
local frontPoint, backPoint = getWallPoints(wall)
--loop through other walls
for _, other in pairs(walls:GetChildren()) do
if other ~= wall then --skip self
if other:IsA("Model") then
--Decide wether the corner be inside the wall or outside
--Inside or outside
other = other.OutsideWall
end
local connectionNeeded = false
local connectPointA, connectPointB
local nam = "Back"
local otherFP, otherBP = getWallPoints(other)
if EdgesNearby(frontPoint, otherFP) then
connectionNeeded = true
connectPointA, connectPointB = frontPoint, otherFP
nam = "Front"
elseif EdgesNearby(frontPoint, otherBP) then
connectionNeeded = true
connectPointA, connectPointB = frontPoint, otherBP
nam = "Front"
elseif EdgesNearby(backPoint, otherFP) then
connectionNeeded = true
connectPointA, connectPointB = backPoint, otherFP
elseif EdgesNearby(backPoint, otherBP) then
connectionNeeded = true
connectPointA, connectPointB = backPoint, otherBP
end
if connectionNeeded then
local wallThickness = wall.Size.X
local wallHeight = wall.Size.Y
connectPointA = connectPointA
connectPointB = connectPointB
if other:FindFirstChild("Corner_"..nam) == nil then
local corner = ConnectPoints(other, connectPointA, connectPointB)
corner.Name = "Corner_"..nam
end
end
end
end
end
remote.OnServerEvent:Connect(function(player, mode, ...)
local args = {...}
if mode == "GenerateWall" then
local wProperties = args[1]
if wProperties.Size.Z > 1 then
local wall = GenerateWall(wProperties)
local house = workspace:FindFirstChild(player.Name.."'s House")
local WallsFolder = house.Structure.Walls
wall.Parent = WallsFolder
local EdgeConnections = FindEdgeConnections(wall, WallsFolder)
Make2Sided(wall)
end
end
end)
If you do not understand the code and need some explanation please let me know
Basically
• Generate wall at given position and size
• Detect if it needs any cornering and do it
• Make the wall 2 sided (outside and inside parts)