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
how would i be able to combine multiple bezier curves and straight lines to create a single path
how would i be able to make an object move at a constant speed in a bezier curve/path
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
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.
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
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)
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
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
because this path has curves and a straight line (connect multiple curves and paths)
and i’ll make enemies walk on this path (at a set speed)
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)
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