Tweeted twitter.com/StackGameDev/status/998193190271406081
Replaced blacklisted 'hexagon' tag with 'hexagonal-grid', fixed small typos
Source Link

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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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.

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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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.

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 exactly 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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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 probably easiest to calculate from x, y and z.

added 1056 characters in body
Source Link
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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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.

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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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

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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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.

Source Link
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.

Preferred movement at the boundary

Example movement at the middle

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

enter image description here

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