Road Building Applet

31 October 2004, updated in 2009 and 2011

I’ve made a series of experiments in how to represent roads. None of these experiments got very far but I’m writing down my thoughts in case someone else is writing a game that has roads, and also because I’d like to, one of these days.

Many tile-based games make you click on each tile to draw a road. I think drag & drop could be used to build roads. See this post about Locomotion [1] for my motivation. I love playing games like Transport Tycoon, Simutrans, and SimCity. My first experiment [2004] lets you draw spline roads between edges of the grid, instead of drawing in the tiles:

This browser does not have Java support enabled, or the author of this page messed something up, or Java applets in general aren’t supported in your browser. You might try a different browser, or ask amitp@cs.stanford.edu to rewrite this applet in html5.

Since it’s unlikely the applet will run anymore, either download world.jar and roads-applet.html and try to run them in appletviewer, or see the World.java source code. Here’s a screenshot:

Screenshot of road editing applet

I also have a newer page [2] that runs in html5 but doesn’t have the same features as the applet.

What did I learn? Grid edges work reasonably well for roads with more complex shapes. I later wrote an article [3] about grid tiles, edges, and corners.

My later experiments [2009-2013] are for non-grid games.

  • [2013] Notes on Curved Paths [4]: Bezier curves, circular arcs, piecewise linear, piecewise circular, and biarcs. Includes some interactive demos that run in the browser.
  • [2009] Road intersections with multiple lanes [5]: drag the colored boxes around to see how a road intersection might change. Still needs work:
    • left-turn lanes, right-turn lanes
    • size of intersection should change depending on number of lanes
    • roads could add/remove lanes near the intersection
    Flash demo would show up here if you have flash enabled, but since you probably don’t, here’s a screenshot: Screenshot of road intersection applet
  • [2009] Curved roads with multiple lanes [6]: drag the circles around to change parameters. I never finished cleaning this demo up so it looks rather messy. (Polishing this demo is on my list [7].)
    • On the left you can play with roads that use bezier curves, including one segment that adds a lane gradually. If you want the road segments to line up you need to change the bezier control point on one or the other segment.
    • In the middle is an experiment to figure out whether a bezier curve (blue) can approximate a circular arc (orange) — it’s close but not the same. You can change the bezier curve parameters to try to make it match up. I put this in because some graphics libraries have bezier curves but not circular arcs.
    • On the right you can play with two straight road sections connected by a circular arc. Note that unlike bezier curves, there’s no trickiness in making the segments line up. The drag handle on the road lets controls where the straight segments would meet up if there was no curved segment. The slider on the right controls how much that corner gets rounded off.
    • On the bottom you can move the knobs to change the number of lanes on the roads.
    Flash demo would show up here if you have flash enabled, but since you probably don’t, here’s a screenshot: Screenshot of road curvature applet

What did I learn? Curved roads should not use Bezier curves but instead circular arcs (both on grids and non-grids). Beziers aren’t great for making offset curves (needed when you have multiple lanes) and don’t match how roads are actually built. Bezier curves are also annoying to deal with when you want to have vehicles move along them at constant velocity (see this [8] for a clever but complicated solution). Instead, roads in real life are built with circular curves, which have simpler math, support offset curves, and support vehicles moving at constant velocity.

For grid roads, I am reasonably happy with dragging from edge to edge. However for non-grid roads I didn’t come up with a control scheme I’m happy with. That’ll be a future experiment.

The source code for the grid edge Java applet is here. I’m not much of a Java programmer, so be prepared to laugh or cry at my coding style. The source code for the non-grid Flash applets are on github [9].

I’m not doing anything with roads at the moment but every once in a while I look into it, because I’d like to eventually make a game that has roads or railroads. These experiments have taught me something about road/railroad drawing.

Footnote: getting an applet to work across browsers is a big pain. I used the code from here [10] but I don’t know how many systems it works on. Flash is a bit easier to make work. In 2004 I used Java; in 2008 I switched to Flash; in 2012 I switched to Javascript/HTML5 for demos. In 2024 I added the Ruffle [11] Flash emulator to run some of my old Flash demos.

More reading:

Email me redblobgames@gmail.com, or comment here:

Links

  1. [1]: https://simblob.blogspot.com/2004/10/god-games-locomotion.html
  2. [2]: https://www.redblobgames.com/articles/curved-paths/grids.html
  3. [3]: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
  4. [4]: https://www.redblobgames.com/articles/curved-paths/
  5. [5]: http://www-cs-students.stanford.edu/~amitp/_test/demo_intersection.swf
  6. [6]: http://www-cs-students.stanford.edu/~amitp/_test/demo_roads.swf
  7. [7]: https://trello.com/board/writing-for/4f1dbfdc0fc2508c1b238d7d
  8. [8]: http://community.spinxengine.com/content/19-constant-speed-cubic-spline-interpolation.html
  9. [9]: https://github.com/amitp/containerport
  10. [10]: https://web.archive.org/web/20070210105626/https://ww2.cs.fsu.edu/~steele/XHTML/appletObject.html
  11. [11]: https://en.wikipedia.org/wiki/Ruffle_(software)
  12. [12]: https://www.redblobgames.com/articles/curved-paths/
  13. [13]: https://citiesxl.fandom.com/wiki/Realistic_Highway_Mod
  14. [14]: https://www.youtube.com/watch?v=8YlR2nSB-ZU
  15. [15]: https://aeplay.org/citybound-devblog/the-road-to-alpha-week-66-more-on-planning-mode
  16. [16]: https://web.archive.org/web/20120714092247/http://www.learn-cocos2d.com/2012/07/starting-point-train-game-freeform-tracks/
  17. [17]: https://gamedev.stackexchange.com/questions/504/what-are-some-methods-to-represent-train-tracks
  18. [18]: https://en.wikipedia.org/wiki/Geometric_design_of_roads
  19. [19]: https://en.wikipedia.org/wiki/Minimum_railway_curve_radius
  20. [20]: https://en.wikipedia.org/wiki/Cornu_spiral
View the discussion thread.

AltStyle によって変換されたページ (->オリジナル) /