Bhristt's Bezier Curve Module (Tween support)

Make sure you’re using Bezier:CalculatePositionRelativeToLength(). This function takes in a percentage of the length of the Bezier curve and returns the position at the given percentage.

Since the object is moving at a constant speed, you can track how much distance the object has covered and use this to get the percentage of the length of the curve.

distance = speed * time
dx = speed*dt

By adding intervals of distance covered every time step, we can accurately keep track of distance covered in real time.

local speed = 10
local position = 0
local bezier = Bezier.new() -- your bezier curve

-- make sure your bezier curve is completely updated and ready to use
-- it should not be updated any further after this point, otherwise
-- the length of the curve should be updated accordingly

-- we are using bezierLength to refer to the length of our curve
local bezierLength = bezier.Length

local lastTimeStamp = os.clock()

while task.wait() do

    local currentTimeStamp = os.clock()
    local dt = currentTimeStamp - lastTimeStamp

    position += speed*dt

    if (position > length) then
        break
    end
    
    -- this gives the current position assuming constant speed
    local currentPosition = bezier:CalculatePositionRelativeToLength(position/length)

    lastTimeStamp = currentTimeStamp
end
1 Like