So I’m working on making an utility module that allows the camera to be moved in an arc instead of linearly, using a goal velocity. So I need to estimate the length of the arc in order to obtain the goal velocity. Anyway, basically this makes me want to have a function that returns a point along a quadratic bezier, and this function should take either a Vector3 or a CFrame as I would assume linearly interpolating a Vector3 would be less expensive than lerping a CFrame. The problem is that I don’t want to have two identical functions as the member Lerp exists in both CFrames and Vector3’s. So my function expects either a Vector3 or a CFrame for each argument. The problem arises from when I attempt to call p0:Lerp(p1, t) and p1:Lerp(p2, t), it says I’m attempting to call a non-function, which I am not doing. Is there any way to omit this warning?
I’ve added a couple comments in the code below to show why I need to expect both CFrames and Vector3’s
-- going to be calling this function when actually interpolating the camera, using CFrames to take into account where the camera should face:
function module:QuadraticBezier(p0: CFrame | Vector3, p1: CFrame | Vector3, p2: CFrame | Vector3, time: number)
local l1 = p0:Lerp(p1, time)
local l2 = p1:Lerp(p2, time)
return l1:Lerp(l2, time)
end
-- to get the goal velocity we need to estimate the length of the quadratic bezier which we can do by breaking it into segments and just adding the magnitude between the start and end of each segment:
function module:EstimateQuadraticBezierLength(p0: CFrame, p1: CFrame, p2: CFrame, amountOfSegments: number?)
local amountOfSegments: number = amountOfSegments or 10
local segments: {Vector3} = {}
for i = 1, amountOfSegments do
table.insert(segments, module:QuadraticBezier(p0.Position, p1.Position, p2.Position, i / amountOfSegments)) -- however, I also want to call it here, using Vector3's as it's less expensive and it would be nonsensical as I'd just be using the positional components of the CFrame anyway.
end
local length = 0
for i = 1, amountOfSegments do
local this, next = segments[i], segments[i + 1]
if not next then
break
end
length += (this - next).Magnitude
end
return length
end
return module
Here’s the error/warning: