1

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.

Bakterhaz
  • 25
  • 4
  • Move up until you're at the height of red, move along until you're at the depth of red, move along the other way until you're at the width of red. Count how far you moved, minus how many times you changed direction to adjust for counting the same cube twice. Then realise you can do RedX-GreenX to get the distance in each direction instead of walking it. This is the Manhattan Distance. – TessellatingHeckler Aug 11 '23 at 13:44

1 Answers1

2

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|

Ewan Brown
  • 640
  • 3
  • 12