Bezier Curve gets slower at the end

Hi! I’m scripting a Bezier Curve but for some it gets slower at the end. Here is a video about the behaviour:

https://streamable.com/gtlda1

Even after adjusting the curve-part it still is slower at the end.

https://streamable.com/uav8ze

Here is the script:

local function lerp(beginning, goal, alpha)
	return (1 - alpha) * beginning + alpha * goal
end

local function quadratic(beginning, curvePoint, goal, alpha)
	local Lerp1 = lerp(beginning, curvePoint, alpha)
	local Lerp2 = lerp(curvePoint, goal, alpha)
	return lerp(Lerp1, Lerp2, alpha)
end

for i = 0, 1, 0.01 do
	wait()
	workspace.beginning.Position = quadratic(workspace.beginning.Position, workspace.curve.Position, workspace.goal.Position, i)
end

So the problem is that the majority of alpha (i) is between the 2 final parts, so I tried to base the wait time on it, but that didn’t turn out that well too.

local function lerp(beginning, goal, alpha)
	return (1 - alpha) * beginning + alpha * goal
end

local function quadratic(beginning, curvePoint, goal, alpha)
	local Lerp1 = lerp(beginning, curvePoint, alpha)
	local Lerp2 = lerp(curvePoint, goal, alpha)
	return lerp(Lerp1, Lerp2, alpha)
end

local lastPoint = nil

for i = 0, 1, 0.01 do
	local l = quadratic(workspace.beginning.Position, workspace.curve.Position, workspace.goal.Position, i)
	if lastPoint then
		wait((lastPoint - l).magnitude/20)
	else
		wait()
	end
	workspace.beginning.Position = l
	lastPoint = l
end

https://streamable.com/y02uri
Thank you!

Btw: Sorry for the discord notifications, I forgot to turn them off, lol.

1 Like
local function Lerp(a, b, c)
    return a + (b - a) * c
end
local function CalculateQuadBezier(Iteration, StartPosition, MiddlePosition, EndPosition)
    local FirstLerp = Lerp(StartPosition, MiddlePosition, Iteration)
    local SecondLerp = Lerp(MiddlePosition, EndPosition, Iteration)

    local FinalLerp = Lerp(FirstLerp, SecondLerp, Iteration)
    return FinalLerp
end
local P0 = workspace.beginning.Position -- start pos here
local P1 = ((P0 + workspace.goal)/2) + Vector3.new(0, 15, 0) --adding a vector for the arc
local P2 = workspace.goal 
for Iteration = 0, 1, .01 do
    workspace.Beginning.Position = CalculateQuadBezier(Iteration, P0, P1, workspace.goal)
    RunService.Heartbeat:Wait()
end
1 Like

You should have explained to him why it happens, it’s due to the arc parametric problem. The solution is catmul splines but that’s a way to solve it too :blush:

1 Like