Train Track pathfinding - Navmesh? Waypoints? Spline?

Hi all,

I’m here to ask you what solution you think would be best for train tracks & pathfinding & train movements on the tracks.

What I’m trying to do:

  • Allow user to place their own tracks. No need to have free-form tracks, could be built with a series of pre-built tracks pieces (curves, straight, stations,…)
  • Separate tracks in ‘blocks’ using signals
  • Have trains pick the best path using pathfinding
  • Of course, move the trains when needed along those path/trains in a relatively realistic manner
  • Trains should be able to have constant/smooth speed/acceleration and follow the tracks/curves smoothly
  • End game should allow tens (hundreds?) of trains moving constantly. So the solution should be light enough to run on non-NASA computers… Target is PC, not phone nore tablets

The different solutions I have thought of:

  • Physics. Seems like to worse idea. Tracks & train wheels should have colliders. Seems like the worse even if tweeks could maybe make this possible. Would be the worse for CPU even if working
  • Splines. Seems too complicated for my application, I do not need free form train track laying
  • Waypoints. Seems like the easiest solutions, each track piece could have pre-placed waypoints, all separated by the same distance. Already did early testing, seems ok for now. Exactly the same spacing between all waypoints allows to have smooth movement and constant speed. Waypoints at intersections could be use for a ‘simple’ path finding algorithm
  • NavMesh. This one has been proposed to me by a friend, telling me that in recent version of Unity, one could bake them at runtime. If working, might be the lightest solution. I have doubts about width issues on the tracks. Also have doubts on how to later use signals on my tracks to separate tracks in ‘blocks’

Main dilemma I have for now is weather to continue on a waypoint system, or to switch to NavMeshes & bake them each time a new track has been added/removed (this would happen frequently)

Any thoughts? Thanks for your time

  • Physics: very bad idea, indeed
  • Splines: Have no idea of their length, so no predictable speeds along paths
  • Waypoints: seems tedious to place all those manually
  • NavMesh: Used for area navigation, unsuitable for straight, predictable tracks.

As I see it, since you’re not going for freeform tracks, you must have some sort of grid, any kind of predefined pattern to place the tracks on. If that’s so, you know all the distances you need. What I would do is imlpementing an A* algorithm to sort out which the best path is and does so very fast. You could create a large array of gridInfoPieces. Each piece knows which direction it is connected, if it’s passable, if it’s on a slope, whatever you need. With every track piece added or removed, you change that array structure. A* should be able to run through that grid and get you the paths you need.
Smooth movement along curves would just be calculating radian measure, sin, cos, you name it. The grid and tracks and trains moving might be very visible on screen, but that distracts from the underlying logic they simply are. Data consisting of distance, path, connectivity etc…