# How To: Calculate Line Of Best Travel For Grid From Line

I’m working on a game where units move on a game board (like a chess board) and I’m having trouble figuring out which blocks to travel on to maintain straight lines of travel on the board.

Meaning some sort of algorithm that can turn a straight line, vector to vector, to multiple vectors based on the direction it has to travel to get to the selected square.

I could work something up that just determined at what point that a unit needs to turn - this would most likely make them follow the blue path.

What I’m looking for is some sort of formula that could map those vectors along the green line.

Ya’ll could have just been like, look up Dijkstra’s algorithm…

It actually turned out to be quite simple.

``````public class PathingHandler
{
public static int ParseVectorDistance(Vector2 delta, Vector2 gamma)
{
float xDistance = Mathf.Abs(delta.x - gamma.x);
float yDistance = Mathf.Abs(delta.y - gamma.y);

int i = (int)(xDistance + yDistance);

return i;
}

public static GameBoardComponent DetermineNextNode
(GameBoardComponent current, Vector2 destination)
{
int currentLowestDistance = Int16.MaxValue;
int currentLowestHeuristic = Int16.MaxValue;

GameBoardComponent closestComponent = null;
Vector2 origin = current.Vector;

GameBoardComponent[] components = current.GetNeighbors();

for (int i = 0; i < components.Length; i++)
{
if (!components*.IsPassable)*
``````

break;

int h = ParseVectorDistance(components*.Vector, destination);*
int g = ParseVectorDistance(components*.Vector, origin);*
int d = h + g;

if (d < currentLowestDistance)
{
closestComponent = components*;*
currentLowestDistance = d;
currentLowestHeuristic = h;
}

else if (d == currentLowestDistance)
{
if (h < currentLowestHeuristic)
{
closestComponent = components*;*
currentLowestDistance = d;
currentLowestHeuristic = h;
}

else if (h == currentLowestHeuristic)
{
if (Utility.RandomBoolean())
{
closestComponent = components*;*
currentLowestDistance = d;
currentLowestHeuristic = h;
}
}
}
}

if (closestComponent != null) return closestComponent;
else
return null;
}
}
[94827-path.png|94827]

You need to split the main “Red” vector’s components into different vectors.
Assuming you board is on the XZ plane at a “Y” height of 0f, you’ll need to do something like this to get the vectors you need:
For the “Blue” vectors you’ll need to split Red into two new Vector3:

``````  Vector3 blueX;
Vector3 blueZ;

void Split(Vector3 red)
{
blueX = new Vector3(red.x, 0f, 0f);
blueZ = new Vector3(0f, 0f, red.z);
}
``````

For the “Green” vectors you’ll need to get which one is the largest component, in order to split that component into 2, you’ll end up using 2 times the same Vector3. So in order to get to the goal, you’ll have to walk along green1 → green2 → green1 again:

``````Vector3 green1;
Vector3 green2;

void SplitThreeWay(Vector3 red)
{
if (red.x > red.z)
{
green1 = new Vector3(red.x / 2f, 0f, 0f);
green2 = new Vector3(0f, 0f, red.z);
}
else
{
green1 = new Vector3(0f, 0f, red.z / 2f);
green2 = new Vector3(red.x, 0f, 0f);
}
``````