Hello devs!
I made a simple in use bezier curves module without any restrictions in amount of points.
Here is it: BezierCurve - Roblox
So its very eazy to use in 2d and 3d.
Here example of using it in 3d: 3d_2PointsBezier.rbxl (30.4 КБ)
For now it has 2 functions:
Calculate(Start position of your curve, End position of your curve, “Step”-it means how much line you need to have(as you have more lines you have smoother bezier but your pc need to calculate more(50 lines is good amount)), Poses-its the points of bezier.)
basicly calculate all curve,
CalculatePointStart position of your curve, End position of your curve, Poses-its the points of bezier, Time(value from 0-1 basicly its follows your curve))
basicly its just calculate point in your curve by time value
As you can see in simulation this thing calculates smooth bezier by this small code:
local s = workspace.START
local e = workspace.END
local p1 = workspace.POINT1
local p2 = workspace.POINT2
local bc = require(script.BezierCurve)
game:GetService("RunService").RenderStepped:Connect(function()
workspace.curveParts:ClearAllChildren()
local lp = nil
workspace.point.Position = bc:CalculatePoint(s.Position,e.Position,{p1.Position,p2.Position},0.25)
for ind,pos in pairs(bc:Calculate(s.Position,e.Position,20,{p1.Position,p2.Position})) do
local cp = Instance.new("Part")
cp.Parent = workspace.curveParts
cp.Anchored = true
cp.CanCollide = false
if lp then
cp.Size = Vector3.new(0.2,0.2,(lp-pos).Magnitude)
cp.CFrame = CFrame.new((lp+pos)/2,pos)
else
cp.Size = Vector3.new(0.2,0.2,(s.Position-pos).Magnitude)
cp.CFrame = CFrame.new((s.Position+pos)/2,pos)
end
lp = pos
end
end)
Also i have a 2d bezier thing: Bézier curves showcase - Roblox
Thanks for reading, also sorry for my english its quite bad i think
I am not sure if you have included this, but let me give you an idea for an extra feature you could add. Directions. You return a Position (the interpolation between the 2 points) but you can also return a direction using the first derivative of the Bezier formula (Derivatives of a Bézier Curve) so that if someone wants to animate an arrow for example, they can set its CFrame orientation.
I think i need to make so you can get the position by the only time value so you can add like 0.02 to time and then get the vector so developer can get vector between points how he want to
I know but i working on cool feature for this module which i dont found anywhere and n this tutorial too.
Its make bezier curve builds not jst regards some points but it will go trough them!
One quick note of feedback, I would always reccomend using RunService.Heartbeat over RunService.RenderStepped unless it’s necessary for said function to run before physics calculations, like character movement or camera modules.
RenderStepped is a unique event. All RenderStepped events must be completed in order for the engine to continue. This is a big reason why functions tied to it should never yield, and why only necessary functions run on it.
Heartbeat works almost identical to this, but it runs in parallel and all hartbeat functions don’t need to end in order for the next frame to render. The dev hub would give you this same exact warning, as it can really lag out a player if misused.