How to get the next and last nodes

Could you provide your script, or at least the part of it that generates the tracks?

Issue is they are not in Order in the explorer.

There is none yet. :confused:
I was using touching parts and it didn’t work.
I’ve been at this problem for months.

Does their name have to be something specific? If not, you can set it’s name to it’s position in the order as it’s generating.

The name doesn’t matter as long as the nodes have their values and are in order.

The nodes are already there.
It’s just adding in the NextNode and LastNode values.
The node is a part.

Are the nodes being generated with a loop? Or prebuilt in the game

Prebuilt. As the picture shows.
I’ve just got to add the NextNode and LastNode object values.

I’m sorry, but I’m on my phone and can’t quite see the picture clearly. Are all of the node parts physically touching the ones next to them? If so, we can make something work with BasePart:GetTouchingParts()

They are.
But the issue is I’ve found that very unreliable in testing.
Let me attach some pictures. And I’ll make them bigger.


image

In what way had you used BasePart:GetTouchingParts() that was unreliable, and how was it unreliable?

Sometimes, it would only get that it was a touching part 50% of the time.

Because the GetTouchingParts() method is not reliable, I’m afraid I do not currently have a solution to your problem. However, if I think of a solution I’ll be sure to post it.

Thank you for your help so far!

1 Like

The only way i can think of right now that you really can get the next nodes whilst being as accurate as possible and while only knowing the end node, start node and the nodes in between( without order) is to compare distances. In theory, we can start at the start node put it in a “closed” list( so essentially we cant go to it again), find the nearest node to it then go to that one and then put that one in the closed list and so on until we reach the end node. So for example:

The way i am describing it makes it sound a lot like A* ( A path finding algorithm), lol

Pseudo Code (theoretical)

local Closedlist = {} -- The list that holds parts that we cannot find/index
local OpenList = Nodes:GetChildren() -- The list that holds parts that we can find/index

local EndNode = --- end node/ last node

 for i,Node in pairs(OpenList ) do

local NearestNode = FindNearestNodeinOpenList() --- you can implement a function to find the nearest part that is in the open list from our current    

if Node == EndNode  and #OpenList < 2 then -- this is checking wether we are at the end of the track
    print("The End!!") 
   return
end


if NearestNode  then
    table.insert( Closedlist, Node) -- insert the node into the list that hold parts that we cannot find 
    table.remove(OpenList, i) -- removing it from the list that holds parts that we can find
 end

end

The only limitation to this i can think of right now( there are probably more) is if the nodes are perpendicular or side by side which could result in the array “Skipping” nodes ( My best guess to prevent this, is to add in some extra checks pertaining to positioning)

Side Note: if you did do something (the method i mentioned) like this looking into heaps might be a good decision

Something that i would recommend looking into are Bezier curves, it would be better to create a train track using these( or use it with it) , then we could easily find the nodes in between and i am guessing you are asking this question so you can make a train to follow the nodes, so traversing nodes as well.

Just some thoughts, Have a Great Day or Night!

1 Like

Thanks!
I’m just looking for some more idea’s, but I will certainly take yours into consideration.

@BanTech Just wondering, how do you generate your nodes?

Generating them is quite similar to what you’ve done, but they are similar width to the sleeper, and thin in the trackside direction to produce gaps between them so a raycast from one to find the next is quite reliable. Putting them inside a folder or model allows whitelisting to avoid picking up anything else from the ray.

I’d like to change it at some point to use non-physical nodes but it’s tricky to find the next/previous without rays. The nodes at junctions contain a junction ID and a direction (Straight or Diverging for a simple junction). I organise these into tables in a script, and when raycasting for the next node, if it finds a junction node it then searches using the correct direction table as the whitelist.

I helped GCR do a slightly more elegant solution of just moving the train by its lookVector and then align it to whatever track is below it. Not sure how they handle junctions though.

4 Likes

Yea, I’ve already got a node-script there, so I’d rather not use lookVector.
I’ve never done some raycasting before, as I am more experienced w/ web development and other things so can you give me an example of what to look into?
Thanks for your amazingly detailed answer,
Sasial.

1 Like

I can try to search some YouTube tutorials if that would help.
I’ve found some videos that might help. https://www.youtube.com/watch?v=40FuZUA5Y-o
https://www.youtube.com/watch?v=flRS157lJGc I didn’t watch them, but I’ll take a look, because I am really interested as well.

EDIT: Also found another interested topic on DevForum related to Raycasting. I don't understand raycasting - #10 by x86_architecture

2 Likes