Physics Based Train System

Wanted to do this for a while–finally did it. This is a (basic, WIP) train that runs completely off physics using the same systems as real world trains. Rather than having invisible parts beside and under the track to keep the train on the track, I used unions with PreciseConvex collisions placed directly on the track. Each wheel has a motorized hinge to a central axle. Two axles are welded together to form a single truck. Eventually I plan to make engines and cars, but this was just a tech demo. A script with a ‘Speed’ number value inside of it controls the speed of each truck. If an internal variable in the script is set to false, the truck will not be motorized and act as a car on its own. Thus far I’ve been able to string 3 trucks together; one motorized and two unpowered. Max speed without derailment so far has been ~45 studs per second with gravity increased to 500 to artificially simulate the increased weight. Capable of climbing up to 10 degree inclines. I’m excited about this because it opens up the possibility of an accurate rail simulation with physics-based derailments and car interactions, rather than the train just being bonded to the track by other parts!

28 Likes

Wow. I’ve been wanting to do something similar for a while now, but I haven’t had the time. I really like doing physics-based things, as I feel like what I call “Scripted Physics” (Using scripts, Body to cause objects to move) feels unnatural and doesn’t provide the authenticity that using actual Roblox physics provides.

2 Likes

Would love too see how you make the engine function, amazing work all around

1 Like
local active = true
local parts = script.Parent:GetDescendants()
local leftdrivers = {}
local rightdrivers = {}
for i = 1,#parts do
if parts[i].Name == "LeftDriver" then table.insert(leftdrivers,1,parts[i]) end
if parts[i].Name == "RightDriver" then table.insert(rightdrivers,1,parts[i]) end
end
if not active then
for i = 1,#rightdrivers do
rightdrivers[i].ActuatorType = "None"
end
for i = 1,#leftdrivers do
leftdrivers[i].ActuatorType = "None"
end
script:Remove() return
end
function UpdateSpeed()
for i = 1,#rightdrivers do
rightdrivers[i].AngularVelocity = -script.Speed.Value
end
for i = 1,#leftdrivers do
leftdrivers[i].AngularVelocity = script.Speed.Value
end
end script.Speed.Changed:Connect(UpdateSpeed)
wait(1)
script.Speed.Value = 0
while active do wait(1) print(script.Parent.Axle.Velocity.Magnitude) end
1 Like

I like your creation but your train become slow when it ascend.

1 Like

That behavior is intentional. If additional power is not applied when a train is driving up a hill in real life, it will decrease in speed. I chose not to apply additional power in the video, so it’s normal for it to slow down.

2 Likes

Great!
I tried to do the same with Body Velocity, but it was too strong to derail on the curve and it didn’t work.
We will refer to your method!

1 Like

Three words – Train Crash Simulator

1 Like

It looks really nice man! It’s nice and smooth and when it’s done it’ll be amazing! Will definitely use/play this if/when it comes out.

1 Like

Do you have Discord?
I’d love to get in touch with you.

1 Like

how i can make it? i want make it.

1 Like

I’ve managed to make something similar with a full train including a loco and passenger cars, motorising the hingeconstraints on the front bogie of the loco. However, I had to set all of the parts in my train to be massless in order for this to work. If I wouldn’t have done that, the hinges on the wheels would rotate around, making the train shake and then tip over on perfectly straight track. I’ll try the increased gravity trick, but wouldn’t this affect other objects in the game too…?

I’ve tried using BodyVelocity. It only works on the axis you set the speeds on. If you try to go round a curve with BodyVelocity set to go straight ahead, the train will be very resistant to rounding the curve and it will derail.