I have a Grid I am generating to perform path finding. When storing all the walkable Nodes I perform a check if the Obstacles
HashSet has an obstacle placed there. I am passing a Node
to the IsCellOccupied(Node node)
function but I think .Contains
is failing to recognise the passed in Node.
Pastebin Grid.cs
The Node class inherits IEquatable
However using this and implemeneting public bool Equals(Node other)
method produces an odd result of the Grid where many empty spots can be found. Only where the red obstacles are placed the area should be empty and not green.
Removing the IEquatable
inheritance and it's method produces the full Grid. However, the whole Grid is now being generated even under the obstacles, where it should be empty
I can't seem to figure out where it's going wrong. I think it has to do with the .Contains
check performed by the IsCellOccupied
function but I am not sure what I am missing.
Node
public class Node : IEquatable<Node>
{
public Vector3 Position { get; set; }
public CellType CellType { get; set; }
public int Cost { get; set; }
public Node parentNode {get; set;}
public Node(Vector3 position, CellType cellType, int cost)
{
Position = position;
CellType = cellType;
Cost = cost;
}
public override int GetHashCode()
{
return Position.GetHashCode();
}
public bool Equals(Node other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return GetHashCode() == other.GetHashCode();
}
public override bool Equals(object obj)
{
if((obj == null) || ! this.GetType().Equals(obj.GetType()))
{
return false;
}
else
{
Node n = (Node)obj;
return Position == n.Position;
}
}
}
IsCellOccupied Function
private bool IsCellOccupied(Node node)
{
if (Obstacles.Contains(node))
return true;
return false;
}
Implementing suggested changes
Suggestion was to set the y as 0. This seems to still place a green node where an obstacle is.
public override int GetHashCode()
{
return new Vector3(Position.x, 0, Position.y).GetHashCode();
}