Roller-coaster Jitter problem

I am having trouble with my roller coaster physics.

When I run the coaster the faster the rollercoaster gets the more the rollercoaster starts to jitter.

I can seem to find a way to fix it.

local Track = workspace["Compiled New Roller Coaster"]
local SpeedData = {}
local DistanceData = {}
local PrevNode = nil

local Speed = 2
local Gravity = 1
local Friction = 1.5
local MinSpeed = 2
local MaxSpeed = 1000
local CurrentNode = 0
local Train = workspace.Trains.Train1

for i,v in Track:GetChildren() do
	local P = Track:FindFirstChild(i-1)
	if PrevNode == nil then
		PrevNode = P
	else
		local Distance = PrevNode.Position - P.Position
		DSpeed = (Distance.Y*Gravity)-Friction/5
		if P.Color == Color3.new(0,0,0) then
			DSpeed -= 2
		end
		if P.Color == Color3.fromRGB(0, 255, 0) then
			DSpeed += 2.5 -(Distance.Y*Gravity/1.2) 
		end
		Distance = Distance.Magnitude
		table.insert(SpeedData,DSpeed)
		table.insert(DistanceData,Distance)
		PrevNode = P
	end

end
print(SpeedData)



Train:PivotTo(Track:FindFirstChild(CurrentNode).CFrame)
local Goal = 0
local StartPOS = Train.PrimaryPart.CFrame
game:GetService("RunService").RenderStepped:Connect(function(dt)
	local Node = Track:FindFirstChild(CurrentNode)
	if CurrentNode+1 ~= #Track:GetChildren() then	
		if (Node.Position - Train.PrimaryPart.Position).Magnitude < 0.01 then
			CurrentNode += 1
			Speed = math.clamp((Speed + SpeedData[CurrentNode]),MinSpeed,MaxSpeed)
			StartPOS = Train.PrimaryPart.CFrame
			Goal = 0
		end
	else
		CurrentNode = 0
		Goal = 0
		Node = Track:FindFirstChild(CurrentNode)
	end
	Goal += Speed/100
	Train:PivotTo(StartPOS:Lerp(Node.CFrame,math.clamp(Goal,0,1)))
end)

The Problem:
https://i.gyazo.com/91228b0762112520b01fc3b9e12246d8.mp4

Hey, could anyone assist me with this problem?

You’re not taking into account deltatime in your speed variables. Lets say your speed is 50 studs per second and we are at node A and want to go towards node b. The new position towards the next node would be A + (A - B) * 50 * dt. note that A and B are vectors, the positions of the nodes.

1 Like