Curve Function Module

Hey, so I’ve been dabbling with the art of Bezier curves for a while now and one of the biggest problems that I was facing, is to make them linear or uniform speed.
There is numerous articles that shows how and different algorithms however I couldn’t wrap my head around any one of them so today I have made a module that can take any parametric functions with argument t and process it into a table of linear points or calculate it’s length, etc…

But before I launch it to the public and make a video about, I need your feedback about it!

So here is my implementation, I made it myself, so I want to know your thoughts and feedback about it.

The code is well documented!

Code in github (you can also contribute or open an issue no matter how small):
CodesOtakuModules/CurveFunction.lua at main · CodesOtakuYT/CodesOtakuModules (github.com)

3 Likes

Example:
Here is a simple example for the ‘Bake’ and ‘Length’ functions:

local CurveFunction = require(script.CurveFunction)

local TAU = 2*math.pi
local SQRT_2 = math.sqrt(2)

local cos, sin = math.cos, math.sin
local vector2 = Vector2.new
local abs = math.abs

-- Radius of the circle
local R = 1 
-- Polygon sides count
local N = 4

-- Circle's parametric function
local function circle(t)
	local angle = t * TAU
	return R * vector2( cos(angle), sin(angle) )
end

-- Create a table with N+1 points using the parametric function circle
local data = CurveFunction.Bake(circle, N+1)
local length = CurveFunction.Length(data)

-- The perimeter of the polygon with N sides
-- that is inscribed inside the circle with the radius R
print("Perimeter:", length)

-- Formula for square's perimeter inscribed inside a circle with radius r
local function squarePerimeterInCircle(r)
	return 4 * SQRT_2 * r
end

-- There is an error (underestimation) of 9.6812937222523e-08
-- between the real formula and the calculated one in this case
print("Error margin:", squarePerimeterInCircle(R)-length)




-- Function to create a part in the specified position
local function part(pos)
	local p = Instance.new("Part")
	p.Anchored = true
	p.CFrame = CFrame.lookAt(pos, Vector3.zero, Vector3.new(0, 1, 0))
	p.BrickColor = BrickColor.Black()
	p.Size = Vector3.one*0.1
	p.Parent = workspace
end

-- Draw the points generated using parts
for _, point in ipairs(data) do
	part(Vector3.new(point.X, point.Y, 0))
end


image

local N = 10


image

local N = 50


image

local N = 100


image

5 Likes

Woah, I havent seen that smooth circle, probably smoother than bezier curves, nice

1 Like

You can also use bezier curves here, because it’s a parametric function
You can use the BakeLinear function to control the speed of the curve because bezier curves aren’t uniform like circle parametric function.
In fact, I made this module for any parametric function, with bezier curves in mind

1 Like

Hey @IlyasTawawe this is so cool like all your other things you have created on here and on YT. Nice work and keep it up!

1 Like

Thanks man, I highly appreciate your kind words!