The code is still far from optimal but the track network traversal and linear distance computations are working correctly. I haven’t tested a train with cars facing in both forward and reverse directions but the code is all written to handle it. It’s a lot of code for something that looks so simple.
|Launch in external player
The car with the red arrow is the locomotive and the solid green dot is the position that is maintained for the entire train. All other positions are generated from that for each frame. That keeps the linear distance between cars and between the wheels of the cars exact and unchanging, even on curves. If the train wheel locations were simply moved along the track at fixed distances, curves would cause cars to shrink.
There is missing code for handling cars ahead of the train but only because I wanted to get the important parts working. Doing each direction in a loop instead of the one direction without a loop will be simple.
Next: Creating routes for trains instead of letting the train pick the route randomly.