Skip to content

Commit

Permalink
ai pathfinding: raising the precision of graph node selection
Browse files Browse the repository at this point in the history
  • Loading branch information
nrsharip committed Jul 25, 2021
1 parent 3a2f0b8 commit 4db1db4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
25 changes: 10 additions & 15 deletions core/src/com/hammergenics/ai/pfa/HGGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,15 @@ public boolean searchConnectionPath(Vector3 start, Vector3 end, GraphPath<Connec
// + " end: " + end
//);
for (HGGraphNodesGrid grid: grids) {
// float startX = (start.x / globalScale) - grid.getX0();
// float startZ = (start.z / globalScale) - grid.getZ0();
// float endX = (end.x / globalScale) - grid.getX0();
// float endZ = (end.z / globalScale) - grid.getZ0();
float startX = (start.x / globalScale) - grid.getX0();
float startZ = (start.z / globalScale) - grid.getZ0();
float endX = (end.x / globalScale) - grid.getX0();
float endZ = (end.z / globalScale) - grid.getZ0();

float startX = start.x / globalScale;
float startZ = start.z / globalScale;
float endX = end.x / globalScale;
float endZ = end.z / globalScale;

boolean startXBelongsToGrid = startX - grid.getX0() >= 0 && startX - grid.getX0() < grid.getWidth(true);
boolean startZBelongsToGrid = startZ - grid.getZ0() >= 0 && startZ - grid.getZ0() < grid.getHeight(true);
boolean endXBelongsToGrid = endX - grid.getX0() >= 0 && endX - grid.getX0() < grid.getWidth(true);
boolean endZBelongsToGrid = endZ - grid.getZ0() >= 0 && endZ - grid.getZ0() < grid.getHeight(true);
boolean startXBelongsToGrid = startX >= 0 && startX < grid.getWidth(true);
boolean startZBelongsToGrid = startZ >= 0 && startZ < grid.getHeight(true);
boolean endXBelongsToGrid = endX >= 0 && endX < grid.getWidth(true);
boolean endZBelongsToGrid = endZ >= 0 && endZ < grid.getHeight(true);

//Gdx.app.debug("graph", "search: "
// + " globalScale: " + globalScale
Expand All @@ -131,10 +126,10 @@ public boolean searchConnectionPath(Vector3 start, Vector3 end, GraphPath<Connec
//);

if (startNode == null && startXBelongsToGrid && startZBelongsToGrid) {
startNode = grid.getGraphNode(startX - grid.getX0(), startZ - grid.getZ0());
startNode = grid.getGraphNode(Math.floor(startX), Math.floor(startZ));
}
if (endNode == null && endXBelongsToGrid && endZBelongsToGrid) {
endNode = grid.getGraphNode(endX - grid.getX0(), endZ - grid.getZ0());
endNode = grid.getGraphNode(Math.floor(endX), Math.floor(endZ));
}

if (startNode != null && endNode != null) { break; }
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/hammergenics/ai/pfa/HGGraphNodesGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void recalculate() {
}
}

public HGGraphNode getGraphNode(float x, float z) { return getGraphNode(Math.round(x), Math.round(z)); }
public HGGraphNode getGraphNode(double x, double z) { return getGraphNode((int)Math.round(x), (int)Math.round(z)); }
public HGGraphNode getGraphNode(int x, int z) {
int index = getGraphNodeIndex(x, z);
//Gdx.app.debug("grid", " x: " + x + " z: " + z + " index: " + index + " size: " + graphNodes.size);
Expand Down

0 comments on commit 4db1db4

Please sign in to comment.