Search code examples
unreal-engine5unreal-blueprint

UE5 Given a 3x3x3 array of cubes, how to determine number of cubes needed to get from point A to point B


enter image description here

Given the above array of cubes. Green is the starting position, red being the end position. How can I determine the number of cubes I would have to travel from start to end.

This is done in UnrealEngine, each individual cube is a blueprint actor in the scene.

I can determine distance by casting a ray from start point to end point, but this would give me a direct distance between the two points. I'd like to find the number of cubes i'd have to travel through to get to the end.


Solution

  • Given a 3x3x3 and 2 positions, the # of cubes you'd have to travel would be a pretty straightforward thing to calculate, and more of a math thing than a Unreal Engine problem.

    If I can assume you can't travel diagonally:

    This is Taxicab/Manhattan distance, just in 3D.

    Assuming that all cubes are traversable, you just need to sum the differences in all 3 dimensions between the 2 positions:

    Travel Distance = |x2 - x1| + |y2 - y1| + |z2 - z1|