Skip to main content
updated project URL as codeplex is going offline soon, changed bold mark down to code markdown for in text code
Source Link
Pikalek
  • 12.5k
  • 5
  • 45
  • 52

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so in the HexGridUtilitiesForGames project (in an opensource project underunder the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

/// <summary>Z component of the 'Vector'- or Cross-Product of two IntVector2Ds</summary>
/// <returns>A pseudo-scalar (it reverses sign on exchange of its arguments).</returns>
public static int operator ^ (IntVector2D v1, IntVector2D v2) {
  return v1.X*v2.Y - v1.Y*v2.X;
}

The ^^ operator above on the IntVector2DIntVector2D objects is overloaded to perform a 2D cross-product (as shown), which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

/// <summary>Z component of the 'Vector'- or Cross-Product of two IntVector2Ds</summary>
/// <returns>A pseudo-scalar (it reverses sign on exchange of its arguments).</returns>
public static int operator ^ (IntVector2D v1, IntVector2D v2) {
  return v1.X*v2.Y - v1.Y*v2.X;
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product (as shown), which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so in the HexGridUtilitiesForGames project (under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

/// <summary>Z component of the 'Vector'- or Cross-Product of two IntVector2Ds</summary>
/// <returns>A pseudo-scalar (it reverses sign on exchange of its arguments).</returns>
public static int operator ^ (IntVector2D v1, IntVector2D v2) {
  return v1.X*v2.Y - v1.Y*v2.X;
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product (as shown), which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing paths which are more visually direct to be favored.

added 315 characters in body
Source Link

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
} 

/// <summary>Z component of the 'Vector'- or Cross-Product of two IntVector2Ds</summary>
/// <returns>A pseudo-scalar (it reverses sign on exchange of its arguments).</returns>
public static int operator ^ (IntVector2D v1, IntVector2D v2) {
  return v1.X*v2.Y - v1.Y*v2.X;
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product (as shown), which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing more paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product, which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing more paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
} 

/// <summary>Z component of the 'Vector'- or Cross-Product of two IntVector2Ds</summary>
/// <returns>A pseudo-scalar (it reverses sign on exchange of its arguments).</returns>
public static int operator ^ (IntVector2D v1, IntVector2D v2) {
  return v1.X*v2.Y - v1.Y*v2.X;
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product (as shown), which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing paths which are more visually direct to be favored.

added 6 characters in body
Source Link

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

The ^ operator above on the IntVector2DIntVector2D objects is overloaded to perform a 2D cross-product, which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing more paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product, which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing more paths which are more visually direct to be favored.

I have implemented A* pathfinding on a hexagonal grid in C# that nicely smooths paths like so (in an opensource project under the MIT License):

enter image description here

Key portions of the code are reproduced here.

void ExpandNeighbour(IDirectedPath path, NeighbourHex neighbour) {
  if ( ! OpenSet.Contains(neighbour.Hex.Coords)) {
    var cost = StepCost(neighbour.Hex, neighbour.HexsideExit);
    if (cost > 0) {
      var newPath = path.AddStep(neighbour, cost);
      var key     = Estimate(Heuristic, VectorGoal, Source.Coords, 
                             neighbour.Hex.Coords, newPath.TotalCost);

      TraceFindPathEnqueue(neighbour.Hex.Coords, key>>16, (int)(key & 0xFFFFu));

      Queue.Enqueue(key, newPath);
    }
  }
}

static int Estimate(Func<int,int> heuristic, IntVector2D vectorGoal, HexCoords start, 
        HexCoords hex, int totalCost) {
  var estimate   = heuristic(start.Range(hex)) + totalCost;
  var preference = Preference(vectorGoal, start.Canon - hex.Canon);
  return (estimate << 16) + preference;
}

static int Preference(IntVector2D vectorGoal, IntVector2D vectorHex) {
  return (0xFFFF & Math.Abs(vectorGoal ^ vectorHex ));
}

The ^ operator above on the IntVector2D objects is overloaded to perform a 2D cross-product, which measures the orthogonality of the two vector factors. The A* heuristic is shifted 16 bits right and the absolute value of the cross product is added to the heuristic value, causing more paths which are more visually direct to be favored.

Source Link
Loading