Need help understanding Bezier Curves

that’s the new point. add them to another curve and you’re done.

what do you mean another curve?

add it to a list, put a part there… or whatever you want to do with them. it’s part of the weighted bezier curve.

Alright I finished everything but im having some issues with my sorter that finds the nearest point. Am I doing everything correct? (v is the current point that is being fixed and this is not the full script btw)

Script:

for i,x in pairs(points) do
				print(x)
				if x.distance < target then -- make sure distance is less then target
					table.insert(newlist, (x.distance - v.distance)) --if so then insert into a temporary table
					end
			end
			table.sort(newlist) -- sort the table from least to greatest
			local lowest = newlist[1] -- get the least value, we now have the least distance but thats not what we need, we need the actual point
			local nearestpoint
			for i,x in pairs(points) do
				if (x.distance - v.distance) == lowest then --use lowest to get the actual point not just the distance inbetween
					nearestpoint = x -- save nearest point
				end
			end

sorry, still not sure what v is?

the way I would do it (without binary search) is get two variables, one for p0 and another for p1, and keep going through the distances until you find one with a distance that’s > target. that one is p1, and the one before it is p0.

after you lerp then add that to the newList. I’m guessing newList is supposed to be the curve?

not sure what the table.sort is for either :slightly_smiling_face:

I made some edits, hopefully that will make more sense

I think I understand what you’re trying to do. can you tell me how you’re getting v? are you trying to go through it point by point?

v is just the original loop for the points im doing

heres the full script

function travelCurve(p0, p1, p2, p3, cart)
	
	local points = {}
	points[#points] = {
		["point"] = Vector3.new(),
		["distance"] = 0,
		["percent"] = 0
	}
	local Bezierpoints = {}
	for t = 0, 1, 0.01 do 
		local v3 = cubic(p0.Position, p1.Position, p2.Position, p3.Position, t / 100)
		Bezierpoints[t] = v3
	end

	for t = 0, 1, 0.01 do
		local point = Bezierpoints[t]
		local previous = points[#points]

		local distance = previous.distance + (point - previous.point).Magnitude

		points[#points] = {
			["point"] = point,
			["distance"] = distance,
			["percent"] = 0
		}
	end

	local total = points[#points].distance
	
	for t = 0, 1, 0.01 do
		local new = nil
		for i,v in pairs(points) do
			if v.point == Bezierpoints[t] then
				v.percent = total * t
			end
			local target = (total / #points) * i
			local newlist = {}
			for i,x in pairs(points) do
				print(x)
				if x.distance < target then
					table.insert(newlist, (x.distance - v.distance))
					end
			end
			table.sort(newlist)
			local lowest = newlist[1]
			local nearestpoint
			for i,x in pairs(points) do
				print(x, lowest)
				if (x.distance - v.distance) == lowest then
					nearestpoint = x
				end
			end
			 new = lerp2(nearestpoint.point, v.point, (target - nearestpoint.distance) / (v.distance - nearestpoint.distance))
		end
		cart.Position = new
	end
end

damn it, this might be my fault, I think it’s the way I worded it.

by “closest point” I don’t mean the point that is closest to the i’th point. forget about the original curve for a second, just think in distances.

you need the closest distance that is <= target. noticed how I put the distances and vectors in the same little tables. each distance has a corresponding point, and that’s the one you’re looking for.

in your second loop don’t iterate the points, iterate by t like you did in the first one, use it to find the target, then get the matching point using that.

I did iterate with t in the 2nd who though

ok… might need to start from the top. first off, in the second loop that I wrote, you need points[#points+1] = {…}. forgot the +1. otherwise it’s overwriting the same thing every time.

second, you can scratch the inner for loop. target is already total * t. use that.

But isnt that why we have the starter one?

I think im just gonna start from fresh, after the

yeah, that’s to fall back on when using previous. by the end it should be n+1 points long. to that end, you can start t at 0.01 instead of 0 or it will get added twice. right now it just ends with one point added.

that’s fair. by the way, I could send you the whole code right now if it’s getting frustrating. just wanted to help you understand it :slight_smile:

Send me the code and ill ask about any questions i have

Im just bad with tables and dictionaries and stuff of that sort so

sure, sure. I’m afraid I can’t send it now as it’s late, will get it back to you tomorrow morning. keep trying if you can.