# What are some methods to represent train tracks?

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!

• It would help if you described what your problems are that you encounter. Commented Jul 15, 2010 at 19:51
• I would love to see a demo of your game. Commented Sep 7, 2011 at 14:54

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).

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

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.

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.

• I tried something similar to a grid-based approach. I basically created track pieces so that you could click them together in a way to give a little more freedom than simple grids. Very similar concept, I think. It worked ok, but drawing the tracks by selecting the appropriate piece each time kind of sucked. Commented Jul 15, 2010 at 20:20

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.

Maybe this will be useful:
Steering Behaviors For Autonomous Characters: http://www.red3d.com/cwr/steer/

• aren't trains captive to their tracks?
– Will
Commented Sep 7, 2011 at 14:12
• @Will: there's a subsection (which I haven't read) about path following red3d.com/cwr/steer/PathFollow.html
– o0'.
Commented Sep 7, 2011 at 14:27
• yes but a train on a track can only change velocity. Its path is dictated by the track?
– Will
Commented Sep 7, 2011 at 14:51