Hey hey devForum,
currently i am trying to make models for an attraction. i decided to do this with bezier curves so i can create the model with ease. and it somewhat works only its not smooth.
ref image:
my goal is to fill in those gaps to line up the parts.
but how would i aproch this?
here is the code:
local p1 = script.Parent.p1
local p2 = script.Parent.p2
local m1 = script.Parent.m1
local parts = {}
local one = Vector3.new(1, 1, 1)
local cooldownTime = 0
local last = cooldownTime
function quadBezier(t, p0, p1, p2)
return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end
function smoothOut(targetPart, after)
local rad = 0
local lookVector
if targetPart.Shape == Enum.PartType.Cylinder then
rad = 90
--lookVector = targetPart.CFrame.RightVector * -targetPart.Size.Z
end
targetPart.CFrame = CFrame.new(targetPart.Position, after) * CFrame.Angles(0, math.rad(rad), 0)
end
function makecurve()
if (tick() - last > cooldownTime) == false then
return
end
last = tick()
for _,v in ipairs(parts) do
if v:IsA("BasePart") then
v:Destroy()
end
end
parts = {}
local resolution = (p1.Position - m1.Position).Magnitude + (m1.Position - p2.Position).Magnitude
for t = 1, resolution/1.25 do
local t = t / (resolution/1.25)
local part = p1:Clone()
part.Anchored = true
table.insert(parts, part)
part.Position = quadBezier(t, p1.Position, m1.Position, p2.Position)
part.Name = "Part"
part.Parent = workspace
end
local lastPart = nil
for i,v in ipairs(parts) do
local part0
local part1
if parts[i-1] then
part0 = parts[i-1]
else
part0 = p1
end
if parts[i+1] then
part1 = parts[i+1]
else
part1 = p2
end
local newSize = (part0.Position - part1.Position).Magnitude/2
local size = v.Size
local x = size.X
local y = size.Y
local z = size.Z
if v.Shape == Enum.PartType.Cylinder then
v.Size = Vector3.new(newSize, y, z)
else
v.Size = Vector3.new(x, y, newSize)
end
if parts[i+1] then
smoothOut(v, parts[i+1].Position)
else
smoothOut(v, p2.Position)
end
end
end
p1.Changed:Connect(makecurve)
m1.Changed:Connect(makecurve)
p2.Changed:Connect(makecurve)
makecurve()
and a download of the test place:
Bezier Curves.rbxl (33.8 KB)