I want to make a part move like a swirl, so I tried doing it with Bézier curves, but I think I will need more than 1 control point of a Bézier curve. How can I do that?
Swirl:
Example with 2 control points:
P0 (Anchor Point)
P1 (Control Point)
P2 (Control Point)
P3 (Anchor Point)
--[[ SERVICES ]]
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
--[[ VARIABLES ]]
local Step = script:FindFirstChild("Step")
local Part = script.Parent:FindFirstChild("Part")
local P0 = script.Parent:FindFirstChild("P0") -- Anchor Point
local P1 = script.Parent:FindFirstChild("P1") -- Control Point
local P2 = script.Parent:FindFirstChild("P2") -- Anchor Point
--[[ FUNCTIONS ]]
local function LinearInterpolation(a, b, step)
return a * (1 - step) + b * step
end
local function DoQuadBezier(p0, p1, p2, step)
local L1 = LinearInterpolation(p0, p1, step)
local L2 = LinearInterpolation(p1, p2, step)
local Result = LinearInterpolation(L1, L2, step)
return Result
end
--[[ SOURCE ]]
wait(2)
local StepTween = TweenService:Create(Step, TweenInfo.new(4), {Value = 1})
StepTween:Play()
Step.Changed:Connect(function()
local TweenAlphaValue = TweenService:GetValue(Step.Value, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)
local NewPosition = DoQuadBezier(P0.Position, P1.Position, P2.Position, TweenAlphaValue)
Part.Position = NewPosition
end)
Yes , but not with the function you do right now. The function you have is for quadratic bezier instead you could make a function which takes N amount of control points.
This is what I used for my train system :
local function Lerp(a,b,c)
if typeof(a) ~= 'CFrame' then
return a + (b-a) * c
else
return a:Lerp(b,c)
end
end
local function Bezier(t,tbl)
local lastLerped = tbl
repeat
local tempTbl = {}
for i, v in ipairs(lastLerped) do
if (#lastLerped-1) >= i then
tempTbl[i] = Lerp(v, lastLerped[i+1], t)
end
end
lastLerped = tempTbl
until #lastLerped == 1
return lastLerped[1]
end
Where t is the time and tbl is an array containing the starting point, control points and ending point
Example : {start,n1,n2,n3…,end}
Your function worked for me! But can you explain to me what you do in that function for my future knowledge? I tried to understand it myself, but I don’t know some things…
local v1, v2, v3, v4 = start, n1, n2, endpoint;
local v5 = v1:lerp(v2);
local v6 = v2:lerp(v3);
local v7 = v3:lerp(v4);
local v8 = v5:lerp(v6);
local v9 = v6:lerp(v7);
local result = v8:lerp(v9);
This is how it works basically the function just makes it dynamic and easy to use.