Get distance between 2 locations

I’m trying to make a map system for my open world space game, and I want to get the distance from one system to another. This is my test map, which uses parts as the systems you can spawn on, and the beams connecting them are “paths” you can take from one to another. For example, you can’t go from the red part to the pink part in one move, as they don’t have a connector, but you can go from the red to green to pink part.


I’m trying to make it get the distance between the parts with those constraints, and while I’ve gotten something sort of working, it’s not working as expected. For any systems that are 1 beam apart, such as the black to the blue part, it works just fine, however, it fails with a lot of systems that are more than 1 beam apart. I have an rbxl of the test map if that would be helpful. I understand this isn’t the most efficient code, but my main goal right now is just to get a working routing system.
Baseplate.rbxl (21.4 KB)

2 Likes

You could use .Magnitude, an example would be:

local Part1 = game.Workspace.Part1 --An example of it's position would be 0,1,0
local Part2 = game.Workspace.Part2 --It's position would be 0,0,0

local Distance = (Part1.Position - Part2.Position).Magnitude --It would be 1.

Since i know that many users here in the devforum now about Magnitude, i don’t know if this is what you’re asking for :slight_smile:.

The issue with this is that these locations could be moved around the world, and something that is closer could be shown as further because the “physical” distance is not uniform.

Just for clarification, are you trying to count how many ‘beams’ are in between two locations?

Let me understand, so you want to get the distance between those paths? Sorry, i’m a bit confused.

Yes, that is exactly what I would like to do.

maybe use pathfinding for it? you could loop through all the waypoints and get their distance from the last waypoint and add it up to get the total distance? if thats what you mean?

1 Like

I just did a test with that, and due to the way my paths are arranged, there doesn’t seem to be a relationship with how far away a part is in beams and how far away it is when I get all the path waypoints.

This is a classic graph theory problem!

I’d recommend looking at the A* search algorithm to find the shortest path between a given starting point and ending point: A* search algorithm - Wikipedia

(alternatively, you can try Dijkstra’s algorithm for a simpler, but less efficient, solution: Dijkstra's algorithm - Wikipedia)

From there, you can sum up the lengths of the paths you used to get from the starting point to the ending point.

4 Likes