Active7 years, 8 months ago
$begingroup$Boxes of railroad ties looks impressive, but does little to run a railroad. Establishing the track locations and glueing the ties is the middle step for me in building track. The tutorial below reflects the method I've developed over the years. It is not necessarily the best way, or even the correct way. But it works for me. Your results may vary. One hot summer afternoon he walked into his train room to see the hot glue had loosened because of the heat - and the splines popped around all the corners! After the two centerline splines dry, the first outside spline is added. Every few inches a spacer is inserted to maintain the distance between the side and center splines.
I looked into this a while back for a small project, but didn't get too far. I'd like to create a small program that lets a user draw a train track and then allows a 'train' to follow said track on a 2D plane. I'd like the track to be pretty free form and result in a nice smooth line. I played around with cubic splines and had decent results, but had a ton of trouble getting the train to follow the spline. I also had a lot of trouble creating multiple tracks and switching a train from one track to another.
Does anyone have advice, articles, or source code that can help me figure something out? Thanks!
hokiecsgradhokiecsgrad1,3443 gold badges15 silver badges14 bronze badges
$endgroup$6 Answers
$begingroup$When drawing tracks or roads, I always want to start with splines. However, the last time I was struggling with splines (as Rachel Blum mentions, smooth movement on a spline is icky both because of parameterization and distances), someone suggested to me that many real roads use circular arcs instead of splines. The constant curvature makes it easy to calculate distances and move at a constant velocity along them. Plus, they look good.
I started playing with roads that alternated between circular arcs and straight lines. It's similar to Will's approach but the circular arcs have variable radius. I was quite happy with this approach but didn't end up doing anything with it. Try this demo (Flash): the controls on the left are for splines and the controls on the right are for a circular arc (use the slider to control the length and the drag handle to control the angle). Make a very tight curve by dragging the end of the road to see how much nicer the circular arc looks.
Alternatively, if you want to play with grids (suggested by Iain), try drawing from edge to edge instead of square to square. Here's a demo (Java applet).
Community♦
amitpamitp5,6071 gold badge19 silver badges34 bronze badges
$endgroup$$begingroup$Using proper hermite splines can lead to tight corners and look rather un-train-track-like:
I've had much more pleasing results from treating the path as straight lines joining circles at the joints:
The trick is computing the inner and outer tangents of adjacent joint circles and working out the winding of each joint so you pass on the right side of it.
And, in track-laying logic, avoid path self-intersections or turn them into junctions :)
Here's a video using this approach: http://www.youtube.com/watch?v=PAVksn4btKU
WillWill5,2603 gold badges27 silver badges55 bronze badges
$endgroup$$begingroup$You could try letting the user author the tracks using Cornu spirals. You'll find next-to-no literature about it online, because splines are the de facto way of representing curves in software. Nonetheless, they are how actual railroad tracks are pathed. Raph Levien has done a lot of work popularizing them.
munificentmunificent11k4 gold badges30 silver badges35 bronze badges
$endgroup$$begingroup$You could try a grid based approach where each square can link to one or more other squares. You could then have some pieces that cover multiple squares, to create some smoother curves. Basically modular, like a train set.
Iain![Godot Railroad Track Spline Godot Railroad Track Spline](http://www.fsmods17.com/wp-content/uploads/2016/11/TRACK-SPLINE-HELPPER-V1.0-FS-17.jpg)
5,1432 gold badges29 silver badges45 bronze badges
$endgroup$$begingroup$I tried various techniques to implement rail tracks, and I found that sections of circles and straight lines were giving the best looking result, and the easiest math to run the train.
Unfortunately, it also made the track laying pretty complicated and not very intuitive, as small movements of the control points will send the track in a completely different direction.
The math for deciding how to lay the track becomes quite tricky, as is adding more control points.
I've got a short demo on http://www.dailymotion.com/video/xgl2kp_openrailz-tech-demo-3_videogames.
small_ducksmall_duck
$endgroup$$begingroup$Maybe this will be useful:
Steering Behaviors For Autonomous Characters:http://www.red3d.com/cwr/steer/
Steering Behaviors For Autonomous Characters:http://www.red3d.com/cwr/steer/
GregoryGregory
$endgroup$