Replaced blacklisted 'hexagon' tag with 'hexagonal-grid', fixed small typos
Rodia
• 313
• 2
• 6
• 17

# What is the easiest way to move towards and along the edge on a hex grid?

I have a hexagonal grid with variable radius. My entity can move exacly 2exactly two hexes per turn.

How should I calculate next step towards border and afterwards travelling clockwise around map?

Preferred movement at the boundary

Character should stay at the border while moving counter-clockwise every turn.

Example movement at the middle

Character should move 1 or 2 hexes towards edge every turn.

I can't figure out simple math to do this.

Edit:

This is how I did movement at the boundary:

  var x = pos.x;
var y = pos.y;
var z = -pos.x-pos.y;
var dx =  0;
var dy =  0;

if      (x == -radius && z >   1) {          dy =  2; }
else if (x == -radius && z ==  1) { dx =  1; dy =  1; }
else if (y ==  radius && x <  -1) { dx =  2           }
else if (y ==  radius && x == -1) { dx =  2; dy = -1; }
else if (z == -radius && y >   1) { dx =  2; dy = -2; }
else if (z == -radius && y ==  1) { dx =  1; dy = -2; }
else if (x ==  radius && z <  -1) {          dy = -2; }
else if (x ==  radius && z == -1) { dx = -1; dy = -1; }
else if (y == -radius && x >   1) { dx = -2;          }
else if (y == -radius && x ==  1) { dx = -2; dy =  1; }
else if (z ==  radius && y <  -1) { dx = -2; dy =  2; }
else if (z ==  radius && y == -1) { dx = -1; dy =  2; }


Any ideas how should I clean this up?

Shortest path to boundary is propablyprobably easiest to calculate from x, y and z.

added 1056 characters in body
warbaque
• 153
• 1
• 4

I have a hexagonal grid with variable radius. My entity can move exacly 2 hexes per turn.

How should I calculate next step towards border and afterwards travelling clockwise around map?

Preferred movement at the boundary

Character should stay at the border while moving counter-clockwise every turn.

Example movement at the middle

Character should move 1 or 2 hexes towards edge every turn.

I can't figure out simple math to do this.

Edit:

This is how I did movement at the boundary

  var x = pos.x;
var y = pos.y;
var z = -pos.x-pos.y;
var dx =  0;
var dy =  0;

if      (x == -radius && z >   1) {          dy =  2; }
else if (x == -radius && z ==  1) { dx =  1; dy =  1; }
else if (y ==  radius && x <  -1) { dx =  2           }
else if (y ==  radius && x == -1) { dx =  2; dy = -1; }
else if (z == -radius && y >   1) { dx =  2; dy = -2; }
else if (z == -radius && y ==  1) { dx =  1; dy = -2; }
else if (x ==  radius && z <  -1) {          dy = -2; }
else if (x ==  radius && z == -1) { dx = -1; dy = -1; }
else if (y == -radius && x >   1) { dx = -2;          }
else if (y == -radius && x ==  1) { dx = -2; dy =  1; }
else if (z ==  radius && y <  -1) { dx = -2; dy =  2; }
else if (z ==  radius && y == -1) { dx = -1; dy =  2; }


Any ideas how should I clean this up?

Shortest path to boundary is propably easiest to calculate from x, y and z.

warbaque
• 153
• 1
• 4

# What is easiest way to move towards and along the edge on a hex grid?

I have a hexagonal grid with variable radius. My entity can move exacly 2 hexes per turn.

How should I calculate next step towards border and afterwards travelling clockwise around map?

Preferred movement at the boundary

Character should stay at the border while moving counter-clockwise every turn.

Example movement at the middle

Character should move 1 or 2 hexes towards edge every turn.

I can't figure out simple math to do this.