Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
3  
It would help if you described what your problems are that you encounter. –  Rachel Blum Jul 15 '10 at 19:51
1  
I would love to see a demo of your game. –  ashes999 Sep 7 '11 at 14:54
add comment

7 Answers

up vote 14 down vote accepted

I'm taking a wild guess at your train following spline issue here - one of the common problems with this is that maintaining constant speed along a spline is a bit tricky. Basically, if F(t) is your spline function over time, distance travelled and time elapsed are not linearly correlated.

Thankfully, there's a good article on how to solve that: http://community.spinxengine.com/content/19-constant-speed-cubic-spline-interpolation.html.

Depending on how detailed your sim is, you'll face other issues, like the fact that a wagon really connects with the rail on two points, too much curvature makes wagons intersect, etc...

share|improve this answer
    
You nailed it! That was the most annoying problem I had. –  hokiecsgrad Jul 15 '10 at 20:15
add comment

Using proper hermite splines can lead to tight corners and look rather un-train-track-like:

using hermite splines

I've had much more pleasing results from treating the path as straight lines joining circles at the joints:

enter image description here

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
I've written a longer article about using circular arcs to represent roads and train tracks. –  amitp Jul 26 '13 at 1:41
add comment

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.

share|improve this answer
1  
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. –  hokiecsgrad Jul 15 '10 at 20:20
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
aren't trains captive to their tracks? –  Will Sep 7 '11 at 14:12
    
@Will: there's a subsection (which I haven't read) about path following red3d.com/cwr/steer/PathFollow.html –  Lohoris Sep 7 '11 at 14:27
    
yes but a train on a track can only change velocity. Its path is dictated by the track? –  Will Sep 7 '11 at 14:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.