Test if a point lies within the blue area

Hi, I’m doing a 2D game.

Is there a quick method to test if a point lies within the blue (possibly reachable after scan) area?
I use a grid graph.

Hello,

Check the question I asked last week.

Julen.

2 Likes

Thank you, julen. But I can’t find a method like ‘PointOnNavmesh’ for a GridGraph.

Check the other two alternatives @aron_granberg gave me.

2
Check if the closest point on the navmesh is close

float distance = Vector3.Distance(somePoint, AstarPath.active.GetNearest(somePoint).position);
if (distance < someThreshold) {
    // Yay, this point is very close to the navmesh
}

3
Check if the closest point on the navmesh is close when seen from above

var nn = NNConstraint.None;
var directionToClosestPoint = AstarPath.active.GetNearest(somePoint).position - somePoint;
directionToClosestPoint.y = 0;
float distance = directionToClosestPoint.magnitude;
if (distance < someThreshold) {
    // Yay, this point is very close to the navmesh when seen from above
}

Also. Depending on what you end up going for, this option may turn out to be useful for you https://arongranberg.com/astar/docs/nnconstraint.html#distanceXZ

EDIT:
I haven’t tested this, but:
bool isIn = AstarPath.active.data.gridGraph.GetNodesInRegion(bound).Count > 0;
This might work, since returns a List of GraphNodes that exist on a given bound

3 Likes

@paradiddle
If you want to test if the closest node to a point is reachable you can do this:

var node = AstarPath.active.GetNearest(player.position).node;
var otherNode = AstarPath.active.GetNearest(queryPoint).node;
if (PathUtilities.IsPathPossible(node, otherNode)) {
    Debug.Log("The node can be reached from the player's position");
}

You can also find the closest node that can be reached from a given point like this (at least up to the limit defined in A* Inspector -> Settings -> Max Nearest Node Limit).

var node = AstarPath.active.GetNearest(player.position).node;
NNConstraint nn = NNConstraint.Default;
nn.constrainArea = true;
nn.area = (int)node.Area;
var closestReachable =  AstarPath.active.GetNearest(queryPoint, nn).node;
if (closestReachable != null) {
    Debug.Log("Found a node that can be reached from the player");
}
1 Like

Thank you, guys. I’m switching from another navigation module to this. Have to implement this function to keep my API consistent.

1 Like