How to add points to simple spline

Im using simple spline and in the game im making there are constantly more points being made. I need to add these to the spline but im not sure how. Using the Simple Spline Module

EDIT: this is what i have so far.

You can see what the issue is, with it going too fast

Code:

task.wait(5)

local SimpleSpline = require(workspace.SimpleSpline)
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Player = Players:GetPlayers()[1]

print("Player found: "..Player.Name)

local Points = {Vector3.new(0, 1.5, 0), Vector3.new(10, 1.5, 0)}
local Spline = SimpleSpline.new(Points, 0.5, 0)

Spline:SetOptions({
	Uniform = true,
	Offset = CFrame.new(),
	Loop = false,
	Reverse = true
})

local Speed = 20


local Points = {CFrame.new(0, 1.5, 0)}

RunService.Heartbeat:Connect(function(DeltaTime)
	table.insert(Points, 1, Points[1] + Vector3.new(Speed * DeltaTime, 0, 0))
	Spline:UpdatePoints(Points)
	Spline:ClearVisualize()
	Spline:Visualize()
end)

task.wait(5)

for Count, Object in pairs(workspace.Parts:GetChildren()) do
	Spline:FollowPath(Object, Speed)
end
1 Like

If FollowPath uses Lerp internally, the part will always catch up with the points because it is increasing the “percent” distance along the line. You need to instead use a function that places the part a certain arclength along the entire spline from the starting point.

how would i make said function?

Make a list of points, calculate the distance between each point and store it in another table. To find a point at distance D along the line, go through the distance list. If D is more than the length of that segment, subtract that length from D and go to the next item. If it is not more, divide it by thats segment length and use it as the factor to :Lerp()

that seems correct, but how would I accomplish such a thing. D is x, or an unknown number if I’m not mistaken, and how should I make a table with the distances in them:

Points table

{
Point1
Point2
Point3
}

However should i make the distance between points table like this:

{
Point1 - Point2
Point2 - Point3
}

EDIT: ive made this now

local function UpdateCarriges()
	for Count, Object in pairs(workspace.Parts:GetChildren()) do
		
	end
end

RunService.Heartbeat:Connect(function(DeltaTime)
	table.insert(Points, 1, Points[1]:ToWorldSpace(CFrame.new(Speed * DeltaTime, 0, 0)))
	if Points[2] then
		table.insert(DistanceBetweenPoints, 1, (Points[1].Position - Points[2].Position).Magnitude)
	end
	
	UpdateCarriges()
end)

However, how would i find this part you said:

‘To find a point at distance D along the line, go through the distance list. If D is more than the length of that segment, subtract that length from D and go to the next item.’

I made this example of what I mean which you can run in a client script:

--Add new point to end of spline, if no spline passed, make a new spline
local function AddPointToSpline(point : Vector3, existingSpline)
	if existingSpline == nil then
		existingSpline = {}
		
		existingSpline.Points = {}
		existingSpline.Distances = {}
		
		table.insert(existingSpline.Points, point)
		
		return existingSpline
	end
	
	table.insert(existingSpline.Points, point)
	
	local lastPoint = existingSpline.Points[#existingSpline.Points - 1]
	table.insert(existingSpline.Distances, (point - lastPoint).Magnitude)
	
	return existingSpline
end

--Get point on spline which is `distance` units from the start point
local function DistanceAlongSpline(spline, distance : number)
	for i, length in pairs(spline.Distances) do
		if (i + 1) >= #spline.Points then
			warn("Distance goes off the end of the spline!")

			return spline.Points[#spline.Points]
		end
		
		if distance > length then
			distance = distance - length
		else
			local a = distance / length
			local start = spline.Points[i]
			local finish = spline.Points[i + 1]
			
			return start:Lerp(finish, a)
		end
	end
end

local spline 

for i = 1, 300 do
	local point = Vector3.new(math.cos(i / 30), 0, math.sin(i / 30)) * (8 + 2 * math.cos(i / 20)) + Vector3.yAxis * (i / 20 + 5)
	
	if spline == nil then
		spline = AddPointToSpline(point)
	else
		AddPointToSpline(point, spline)
	end
end

local p = Instance.new("Part")
p.Size = Vector3.one * 0.25
p.Anchored = true
p.Color = Color3.new(0, 1, 0)
p.Parent = workspace

--Track
for i, point in spline.Points do
	local p = p:Clone()
	p.Transparency = 0.8
	p.Color = Color3.new(1, 1, 1)
	p.Parent = workspace
	p.Position = point
end


local distance = 0
local speed = 5

local cars = {}

for i = 1, 5 do
	cars[i] = {}
	
	cars[i].Part = p:Clone()
	cars[i].Part.Parent = workspace
	cars[i].Part.Color = Color3.fromHSV(math.random(), 1, 1)
	cars[i].Lag = i - 1
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
	distance = distance + dt * speed
	
	for i = 1, #cars do
		cars[i].Part.Position = DistanceAlongSpline(spline, distance - cars[i].Lag)
	end
end)

I will try this later, when I’m back at my pc. Probably in 5 - 6 hours.

that works, but only if the points are made all in one go. As i said at the start:

‘in the game im making there are constantly more points being made’

EDIT:

I made it so new parts are made, and it works, but how would i rotate the cars - FIXED.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.