Posts: 22
Threads: 7
Joined: Jul 2020
07-13-2020, 11:35 PM
(This post was last modified: 07-13-2020, 11:44 PM by jamesunity.)
Hi, I would like to place stations along a track and then if a train is scheduled to stop at the station next, find the nearest control point to that station, and then eventually set connections properly to make sure the train will go to the right path that this specific cp is on. I imagine this last bit will get more complicated if there are several connections before this control point as it'd need to calculate a full route not just switch a single path A or B.
So is the easiest way to do this to make a new control point when a station is placed so a station will always be sat on its own control point which could be stored as a value the station itself, or is that not possible?
If it's not possible can I get the nearest control point based on the x,y,z of the station gameobject (which is just an object in the world its not related to the spine or anything)
And then how can I talk to the connections to make sure they are set in a way that will make the train choose the right path at a junction? Even at a basic level where there is only 2 paths A and B and it needs to choose a station on path A or B how would I set the connection to switch (or not) depending what it needs to do?
Sorry if that's sort of 2 questions in one! Any help is appreciated!
Now that I think about it maybe I don't need the nearest CP at all maybe I should just set the station to the spline it's on when placed? So if it's collider (trigger) is touching spline B it's set to that, is that possible even though the spline doesn't have colliders on it?
But then i would still need to know how to make sure the train follows the right spline to the spline that station is set to when it comes to a connection somehow...
Posts: 22
Threads: 7
Joined: Jul 2020
OK I have pretty much got there I think, I'll put the code below roughly in case it helps anyone in the future. I have given the stations a Spline ref so they "know" what spline they are on, and then when the train hits a trigger child of the connection point it checks if its current spline = the spline of the station for its next stop, then checks if the controlpoint[0] on the connection = the station spline and if not switches it. It all seems to work, but eventually I will need this to work out possible routes when there is more than 1 connection between the station and the controller, so if anyone can help with the logic behind that it'd be great!
I added this to JunctionSwitch.cs (really messy code that could be improved a lot I know!)
Code:
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag=="Train")
{
//if trains next station spline != trains current spline
if(collision.gameObject.transform.parent.GetComponentInParent<TrainRouting>().scheduledStops[0].GetComponent<StationManager>().Spline != collision.gameObject.GetComponent<SplineController>().Spline)
{
// check if connection controlpoint[0] is the right spline for the station spline and switch if not
if (GetComponentInParent<CurvySplineSegment>().Connection.ControlPointsList[0] != collision.gameObject.transform.parent.GetComponentInParent<TrainRouting>().scheduledStops[0].GetComponent<StationManager>().Spline)
{
GetComponentInParent<MDJunctionControl>().UseJunction = !GetComponentInParent<MDJunctionControl>().UseJunction;
}
}
}
}
Posts: 2,110
Threads: 92
Joined: Jun 2017
Hi,
I see your problem as being two separate problems:
1- find the way between points A and B
2- make the train follow that way
Point 2 is related to Curvy. Using a mechanism similar to the Junction mechanism in the example scene 12 should do the trick.
Point 1 is a wider problem, not specific to Curvy. It's basically path finding. Since you are planning to have multiple rail roads connected, I think you better try to find an already existing path finding solution that would work good, instead of trying to invent your own path finding algorithm. I have no real experience with the path finding problematics, but I did some projects using that when student. A* algorithm was a good algorithm at that time (is there anything better since?). It's a simple algorithm, and I am sure you can easily find implementations for it using C#
I would advise you to read this: https://en.wikipedia.org/wiki/Pathfinding
Please consider leaving a
review for Curvy, this helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Posts: 22
Threads: 7
Joined: Jul 2020
(07-14-2020, 01:18 PM)_Aka_ Wrote: Hi,
I see your problem as being two separate problems:
1- find the way between points A and B
2- make the train follow that way
Point 2 is related to Curvy. Using a mechanism similar to the Junction mechanism in the example scene 12 should do the trick.
Point 1 is a wider problem, not specific to Curvy. It's basically path finding. Since you are planning to have multiple rail roads connected, I think you better try to find an already existing path finding solution that would work good, instead of trying to invent your own path finding algorithm. I have no real experience with the path finding problematics, but I did some projects using that when student. A* algorithm was a good algorithm at that time (is there anything better since?). It's a simple algorithm, and I am sure you can easily find implementations for it using C#
I would advise you to read this: https://en.wikipedia.org/wiki/Pathfinding
Hi I agree I shouldn't re-invent the wheel, it just feels like this level of AI/pathfinding should be SO simple, it's literally just got to choose path A or B and is constrained to simply moving or stopping on splines... no obstacle courses to try and work its way around or anything tricky!
I did start to look at A* as like you say I think it's considered the best, so I'll take another look, it just felt like it might be overkill for what is essentially very very limited movement and calculations literally restricted to tracks. Thanks as always for your input!
Posts: 2,110
Threads: 92
Joined: Jun 2017
I first understood that you will have a web of roads, but now I realize you are going to have a "tree" of roads (only 2 roads connections and no looping). True, there should probably be a simpler solution for this simpler case, but I don't see it right now. Probably involving a manual or automatic process to mark all the connections leading to a station with some marker (a MetaData would do the trick) to allow the train to decide where to go once reaching a connection
Please consider leaving a
review for Curvy, this helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Posts: 22
Threads: 7
Joined: Jul 2020
(07-14-2020, 08:22 PM)_Aka_ Wrote: I first understood that you will have a web of roads, but now I realize you are going to have a "tree" of roads (only 2 roads connections and no looping). True, there should probably be a simpler solution for this simpler case, but I don't see it right now. Probably involving a manual or automatic process to mark all the connections leading to a station with some marker (a MetaData would do the trick) to allow the train to decide where to go once reaching a connection
Hi, yes I like that idea, I shall look into MetaData some more! Many thanks
Posts: 2,110
Threads: 92
Joined: Jun 2017
you are welcome
Please consider leaving a
review for Curvy, this helps immensely. Thank you.
Available for freelance work—feel free to reach out.