I’m generating branch segments along a Bezier curve. On simple bends it works fine, but when the curve bends in both X and Y (Example: (2, 2, 0)), the segments overlap instead of meeting flush:
if LastSegment and GlobalPreviousDirection then
local Angle = math.acos(math.clamp(GlobalPreviousDirection:Dot(Direction), -1, 1))
local Miter = (Width/2) * math.tan(Angle/2)
LastSegment.Size += Vector3.new(0, 0, Miter)
LastSegment.CFrame *= CFrame.new(0, 0, -Miter/2)
Segment.Size = Vector3.new(Width, Width, Length + Miter)
Segment.CFrame = CFrame.new((Pos + NextPos)/2, NextPos) * CFrame.new(0,0, Miter/2)
else
Segment.Size = Vector3.new(Width, Width, Length)
Segment.CFrame = CFrame.new((Pos + NextPos)/2, NextPos)
end
I think I need to rotate each segment toward the angle bisector instead of just stretching them
What’s the best way to fix this so the corners line up cleanly?
Full code if anyone comes across, I’ve been trying for 14h now
function BranchGenerator:GenerateBranch(SegmentTemplate: BasePart, Points: {Point}): Model
local Model = Instance.new("Model")
local RNG = Random.new()
if #Points < 2 then
warn("Not enough points")
return Model
end
local LastSegment: BasePart?, PreviousDirection: Vector3? = nil
for PointIndex = 1, #Points - 1 do
local CurrentPoint = Points[PointIndex]
local NextPoint = Points[PointIndex + 1]
local A = ApplyRandomOffset(CurrentPoint, RNG)
local B = ApplyRandomOffset(NextPoint, RNG)
local Forward = (B - A).Unit
local Middle = (A + B) / 2
local Control = Middle
local Look = CFrame.lookAt(Middle, Middle + Forward)
local Right = Look.RightVector
local Up = Look.UpVector
local BezierOffset = CurrentPoint.Settings.BezierOffset
if BezierOffset then
local LocalOffset = Vector3.new(
RNG:NextNumber(BezierOffset.Min.X, BezierOffset.Max.X),
RNG:NextNumber(BezierOffset.Min.Y, BezierOffset.Max.Y),
RNG:NextNumber(BezierOffset.Min.Z, BezierOffset.Max.Z)
)
Control += Right * LocalOffset.X + Up * LocalOffset.Y + Forward * LocalOffset.Z
end
if CurrentPoint.Settings.BezierNoise then
local Noise = CurrentPoint.Settings.BezierNoise
Control += Vector3.new(
RNG:NextNumber(-Noise, Noise),
RNG:NextNumber(-Noise, Noise),
RNG:NextNumber(-Noise, Noise)
)
end
local StepsPerSegment = RNG:NextInteger(CurrentPoint.AmountOfSegments.Min, CurrentPoint.AmountOfSegments.Max)
for Step = 0, StepsPerSegment - 1 do
local t1 = Step / StepsPerSegment
local t2 = (Step+1) / StepsPerSegment
local Pos = QuadraticBezier(A, Control, B, t1)
local NextPos = QuadraticBezier(A, Control, B, t2)
local W1 = CurrentPoint.Settings.WidthRatio or 1
local W2 = NextPoint.Settings.WidthRatio or 1
local Width = LerpNumber(W1, W2, t1)
local Direction = (NextPos - Pos).Unit
local Length = (NextPos - Pos).Magnitude
local Mid = (Pos + NextPos)/2
local Segment = SegmentTemplate:Clone()
Segment.Size = Vector3.new(Width, Width, Length)
Segment.CFrame = CFrame.lookAt(Mid, NextPos)
if LastSegment and PreviousDirection then
local Angle = math.acos(math.clamp(PreviousDirection:Dot(Direction), -1, 1))
local Miter = (Width/2) * math.tan(Angle/2)
local MiterDirection = Vector3.zAxis * Miter
LastSegment.Size += MiterDirection
LastSegment.CFrame *= CFrame.new(-MiterDirection/2)
Segment.Size += MiterDirection
Segment.CFrame *= CFrame.new(MiterDirection/2)
end
Segment.Anchored = true
Segment.Parent = Model
LastSegment, PreviousDirection = Segment, Direction
end
end
return Model
end
Will have a think about this but just to note, it will be impossible to make rectangular shapes flush when the angle of rotation does not line up with one of the edges (as in the first image, regardless of how the objects are moved it will never line up perfectly).
Anyway, that being said I’ll have a look, if I come up with something I’ll make another post.
By rotating, it seemed possible, couldn’t find how tho.