Help with bezier curves

I’ve got multiple questions about bezier curves
and yes i did look at the bezier curve developer article and some other resources
but they didn’t answer all my questions about them and then some of them i had a hard time understanding

questions that i would like to answer are

  1. how would i be able to combine multiple bezier curves and straight lines to create a single path

image
image

  1. how would i be able to make an object move at a constant speed in a bezier curve/path

Cubic-Bezier-Curve-Speed-Uniform

  1. lastly how would i get the the percentage of a object traveled on a bezier curve/path over the total distance of the curve, like current position to final destination (and yes i would like this to correlate with the first question, and getting its distance traveled over the total distance of the entire path/multiple bezier curve segments connected distance travled/total distance of path)

yeah this image is very simplified but i do want to make something in relation to this with the only exception with a bezier path

if there’s anything you don’t understand about this topic or the questions that im asking, please ask me and i’ll try to clear things up

2 Likes

To answer 2 and 3

for i = 1, 5000 do
    local percent = i/5000
    local position = cubicBezier(percent, p0, p1, p2, p3)
    part.Position = position

    -- The part will travel at a constant speed through the cubic bezier. And the percent is the percent you've traveled through.

Hopefully I understood you correctly

you sure that’ll be constant speed, because on my bezier curve all the points are spread out
differently, the points are closer together on the curve and farther near the both start and end points

Yeah no, the for I would be no good with it, you actually use 2 lerps for the Bezier curve.

It’d be something like this.

local function Curve(t,P0,P1,P2)
            local A = P0:Lerp(P1,t)
            local B = P1:Lerp(P2,t)
            return A:Lerp(B,t)
        end

so how would i use that for my bezier curve, and does that answer at least one of my questions?

I mean the most that I can help you is by showing how I used it for myself.

local function Curve(t,P0,P1,P2)
            local A = P0:Lerp(P1,t)
            local B = P1:Lerp(P2,t)
            return A:Lerp(B,t)
        end
        local Magnitude = (hrp.Position - hrp.Position + Vector3.new(0,15,8.5)).Magnitude
        local MagTable = {Magnitude,-Magnitude}
        local Part0 = hrp.Position + Vector3.new(0,.5,1)
        local Part1 = (hrp.CFrame * CFrame.new(MagTable[math.random(1,2)]/2,Magnitude/5,-Magnitude/5)).Position
        local RandomY = Random.new():NextNumber(.15,1)
        local RandomZ = Random.new():NextNumber(7.25,8.25)
        local Part2 = hrp.CFrame * CFrame.new(0,RandomY,-RandomZ).Position

        task.defer(function()
            task.wait(.35)
            for i = 0,1,0.045 do
                local Bezier = Curve(i,Part0,Part1,Part2)
                DebrisBall.Position = Bezier
                task.wait()
                task.spawn(function()
                    local Trans = TS:Create(DebrisBall, Info, {Transparency = 1})
                    Trans:Play()
                end)
            end
        end)
1 Like

Can’t you just travel through the bezier curve like this then?

-- Pregenerate arrayOfPoints
local arrayOfPoints = {}
local startPoint = arrayOfPoints[1]

local lastPoint = startPoint

for index, point in ipairs(arrayOfPoints) do
    local distance = (point.Position - lastPoint.Position).Magnitude + 0.01
    local speedToTravelThroughPoints = 10

    part.Position = arrayOfPoints[index]

    task.wait(distance/speed)
end

what’s the code for the cubic bezier function, because i can’t use the function

I’ve edited my above reply to be a bit more clear

First you create an array of all the positions on your bezier curve

then you can run the above code

Which might look like this:

for i = 1, 5000 do
    local percent = i/5000
    local position = cubicBezier(percent, p0, p1, p2, p3)
    
    arrayOfPositions[i] = position

it took me a while to get the code to work
im not sure if this is how is supposed to look like since i had to modify the code a little bit

local ser = game:GetService("ServerStorage")
local rep = game:GetService("ReplicatedStorage")

local Points = workspace.Points
local path = workspace.Path

local base = ser.Part

local Start = Points.Start
local Finish = Points.Finish
local Control = Points.Control

local samples = 1000
local speed = 500

local part = workspace.part

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

function quadraticBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)
	return quad
end

local arrayOfPoints = {}
local startPoint = arrayOfPoints[1]

local lastPoint = Finish.Position

for i = 0, 1, 1/samples do
	local percent = math.floor(i * 100)
	
	local position = quadraticBezier(i, Start.Position, Control.Position, Finish.Position)
	arrayOfPoints[percent] = position
end

for index, point in ipairs(arrayOfPoints) do
	local distance = (point - lastPoint).Magnitude + 0.01
	part.Position = point
	print(point)
	task.wait(distance/speed)
end

1 Like

i tested it out, and its not much different from mine

I mean, whatever works the best ig. I don’t know much about bezier curves.

me neither, but im trying to find how make bezier curves for a game im making

  1. because this path has curves and a straight line (connect multiple curves and paths)
  2. and i’ll make enemies walk on this path (at a set speed)
  3. lastly i’ll need to know how far an enemy has gone on the path to know which one is the furthest/closest from the end (percentage of current position to end position)

If you’re trying to make a Tower Defence game I’m not sure making the zombies move with a bezier curve is the best way to go about it

If you want to continue and try this you may want to check out this and try and convert it to lua

I would personally check out something like this though

Or this

well i kinda want the enemies move on the path in a smooth way, i don’t wanna place too many waypoints just to make the movement curved, id rather use a bezier curve to achieve this effect
and i wanna make it smooth like Tower Defense Simulator

1 Like

All these problems are solved by my PolyBezier module, check it out here:

1 Like