Calculating Bezier Curves

How to calculate a point on a bezier curve

by CodyTheDwagon

Author Avatar

This is a tutorial on generating a Bezier curve!

This script was made to go in the ServerScriptService

##Control Points

First, you will need to create 3 (or more) different Parts/Control Points for the curve to follow. I won't be covering more than 3 in this tutorial though. You can create these parts with a script or have them ready in the workspace.

Start by defining the 3 parts as variables.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

This lets us access these parts, and their properties later in the script.

##Lerping

Next, we'll create a "Lerp" function to help calculate positions for the curve. Lerping is short for Linear Interpolation and can help find points between two values.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)

end

In this function, a is the first value, and b is the second. The last variable t, is a value between 0 and 1 that decides what point between the numbers we are trying to find. t=0.5 would find the point exactly between the two values, t=0.25 would find the point that is one fourth of the way between a and b, etc.

Now, we need to write the code for the Lerp function.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)
	return a+(b-a)*t
end	

Now when we call on the function, it will calculate the point using the three variables we give it. Next lets create the function to calculate the point on the curve.

##Creating the curve

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)
	return a+(b-a)*t
end	

local function Bezier(t)

end

Again, were using the variable of t in this function. Now it's time to use the 3 variables we defined at the start, and the Lerp function.

The image below better shows how the point of the curve is calculated. The starting point is the dot marked 1, the middle point is the dot marked 2, and the end point is the dot marked 3.

image|100x100

A curve with t=0.25

image|100x100

Now we lerp between 1 and 2, then between 2 and 3. This gives us 2 new variables, and we then lerp between those 2 variables to find the point on the curve.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)
	return a+(b-a)*t
end	

local function Bezier(t)
	local MiddlePoint1 = Lerp(startPoint.Position, middlePoint.Position, t)
	local MiddlePoint2 = Lerp(middlePoint.Position, endPoint.Position, t)
end

Now, to find the point on the curve, lerp between those two variables.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)
	return a+(b-a)*t
end	

local function Bezier(t)
	local middleLerp1 = Lerp(startPoint.Position, middlePoint.Position, t)
	local middleLerp2 = Lerp(middlePoint.Position, endPoint.Position, t)

	local CurvePoint = Lerp(middleLerp1, middleLerp2, t)

	return CurvePoint
end

Adding "return CurvePoint" lets the function give a variable when we call on it, so it can be used like this.

local BezierPoint = Bezier(0.5)

##Vizualizing the curve with parts

Finally, let's make a function that creates a bunch of parts at the points of the curve when we run the game.


for i = 1, 10 do

end

This function let's us repeat something 10 times, and changing the 10 to a different number will change how many times it runs. Now lets make it create the parts.

for i = 1, 10 do
	local curvePart = Instance.new("Part", workspace)
	curvePart.Size = Vector3.new(0.25,0.25,0.25)
	curvePart.Anchored = true
end

Currently this only creates the parts, but doesn't position them along the curve. To do this, we need the number for t. To find this, we will use the i variable from the "for i " function. i stands for Index, or the number the function is on when repeating. First to make things a little easier, add a new variable at the start of the script called "Steps" which will decide how many parts are created along the curve.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local Steps = 10

Now, back to the "for i" function, lets change the 10 to Steps, and then position the parts on the curve.

for i = 1, Steps do
	local curvePart = Instance.new("Part", workspace)
	curvePart.Size = Vector3.new(0.25,0.25,0.25)
	curvePart.Anchored = true
	curvePart.Position = Bezier(i/Steps)
end

We divide i/Steps to fit the index between 0 and 1. Now add this to the rest of the script at the bottom, then try running the game! You can also try changing the number of steps or moving the parts around to change the curve.

local startPoint = workspace.StartingPoint
local endPoint = workspace.EndingPoint
local middlePoint = workspace.MiddlePoint

local function Lerp(a, b, t)
    return a+(b-a)*t
end    

local function Bezier(t)
    local middleLerp1 = Lerp(startPoint.Position, middlePoint.Position, t)
    local middleLerp2 = Lerp(middlePoint.Position, endPoint.Position, t)

    local CurvePoint = Lerp(middleLerp1, middleLerp2, t)

    return CurvePoint
end

for i = 1, Steps do
    local curvePart = Instance.new("Part", workspace)
    curvePart.Size = Vector3.new(0.25,0.25,0.25)
    curvePart.Anchored = true
    curvePart.Position = Bezier(i/Steps)
end

In the end, you should have something like this!

image|100x100

I hope this tutorial helps and that I did alright explaining things!

View in-game to comment, award, and more!