- Home /
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
Answer by hexagonius · Aug 18, 2017 at 08:52 AM
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..
Thanks for your feedback.
Actually, pathfinding is not really the issue I admit. I should of been more clear.
$$anonymous$$ain point was on the different implementation solution. I actually now think splines would be the best solution. Even if this was not an original goal, allowing free-form tracks would of course be much cooler than pre-built small track pieces. I am trying to implement them currently, early test show that I should have listen more in math class when I was young... But it is clearly doable. You cant indeed find the 100% exact length of a spline, but you actually can get pretty close by cheating a bit. I don't need the 0.0001% precision in any case
For the pathfinding, not sure what I'll use exactly, but it wont have to be very complicated. I think a grid in this case would actually be slower than using nodes. Since tracks can only change direction at intersections, there wont be a lot of possible routes for each path requested. So I'll just need to store the junctions locations, the distance between each of them and the directions they can take (if not both) from one junction to the next. Then use any pathfinding algorithm that will allow me to find the shortest path
Thanks again!
isn't navmesh pathfinding in unity implement a* algorithm?
Yes, but it's area based which is easy too much for his purposes.
Your answer
Follow this Question
Related Questions
check if an object is reachable 2 Answers
Is it possible to translate a NavMesh at runtime? 2 Answers
Why isn't a Ai Waypoint Spawning??? 0 Answers
walking through the countryside ? 1 Answer
Navmesh with destructible obstacles 1 Answer