Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A* for libgdx that is simple but optimized, includes runnable demo. Uses a binary heap for the open list. Nodes are cached in a PathNode[], which is also used to make finding a node in the open list fast. A "run id" is used to avoid needing to clear the nodes for a new run.

View astar for libgdx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// A* for libgdx that is simple but optimized.
 
package test;
 
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.utils.BinaryHeap;
import com.badlogic.gdx.utils.BinaryHeap.Node;
import com.badlogic.gdx.utils.IntArray;
 
/** @author Nathan Sweet */
public class AstarTest extends ApplicationAdapter {
ShapeRenderer shapes;
Astar astar;
boolean[] map;
 
public void create () {
shapes = new ShapeRenderer();
 
map = new boolean[10 * 10];
map[5 + 5 * 10] = true;
map[6 + 5 * 10] = true;
map[7 + 5 * 10] = true;
map[7 + 6 * 10] = true;
map[8 + 6 * 10] = true;
 
astar = new Astar(10, 10) {
protected boolean isValid (int x, int y) {
return !map[x + y * 10];
}
};
}
 
public void resize (int width, int height) {
shapes.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
}
 
public void render () {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
 
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
int mapWidth = astar.getWidth();
int mapHeight = astar.getHeight();
int cellWidth = width / mapWidth;
int cellHeight = height / mapHeight;
 
shapes.setColor(Color.GRAY);
shapes.begin(ShapeType.Line);
for (int x = 0; x < mapWidth; x++)
shapes.line(x * cellWidth, 0, x * cellWidth, height);
for (int y = 0; y < mapHeight; y++)
shapes.line(0, y * cellHeight, width, y * cellHeight);
shapes.end();
 
shapes.setColor(Color.BLUE);
shapes.begin(ShapeType.Filled);
for (int x = 0; x < mapWidth; x++)
for (int y = 0; y < mapHeight; y++)
if (map[x + y * mapWidth]) shapes.rect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
 
int startX = Gdx.input.getX() / cellWidth;
int startY = (height - Gdx.input.getY()) / cellHeight;
shapes.setColor(Color.GREEN);
shapes.rect(startX * cellWidth, startY * cellHeight, cellWidth, cellHeight);
 
int targetX = 8;
int targetY = 9;
shapes.setColor(Color.RED);
shapes.rect(targetX * cellWidth, targetY * cellHeight, cellWidth, cellHeight);
 
if (startX >= 0 && startY >= 0 && startX < mapWidth && startY < mapHeight) {
shapes.setColor(Color.GREEN);
IntArray path = astar.getPath(startX, startY, targetX, targetY);
for (int i = 0, n = path.size; i < n; i += 2) {
int x = path.get(i);
int y = path.get(i + 1);
shapes.circle(x * cellWidth + cellWidth / 2, y * cellHeight + cellHeight / 2, cellWidth / 4, 30);
}
}
 
shapes.end();
}
 
public static void main (String[] args) throws Exception {
new LwjglApplication(new AstarTest());
}
 
/** @author Nathan Sweet */
static public class Astar {
private final int width, height;
private final BinaryHeap<PathNode> open;
private final PathNode[] nodes;
int runID;
private final IntArray path = new IntArray();
private int targetX, targetY;
 
public Astar (int width, int height) {
this.width = width;
this.height = height;
open = new BinaryHeap(width * 4, false);
nodes = new PathNode[width * height];
}
 
/** Returns x,y pairs that are the path from the target to the start. */
public IntArray getPath (int startX, int startY, int targetX, int targetY) {
this.targetX = targetX;
this.targetY = targetY;
 
path.clear();
open.clear();
 
runID++;
if (runID < 0) runID = 1;
 
int index = startY * width + startX;
PathNode root = nodes[index];
if (root == null) {
root = new PathNode(0);
root.x = startX;
root.y = startY;
nodes[index] = root;
}
root.parent = null;
root.pathCost = 0;
open.add(root, 0);
 
int lastColumn = width - 1, lastRow = height - 1;
int i = 0;
while (open.size > 0) {
PathNode node = open.pop();
if (node.x == targetX && node.y == targetY) {
while (node != root) {
path.add(node.x);
path.add(node.y);
node = node.parent;
}
break;
}
node.closedID = runID;
int x = node.x;
int y = node.y;
if (x < lastColumn) {
addNode(node, x + 1, y, 10);
if (y < lastRow) addNode(node, x + 1, y + 1, 14); // Diagonals cost more, roughly equivalent to sqrt(2).
if (y > 0) addNode(node, x + 1, y - 1, 14);
}
if (x > 0) {
addNode(node, x - 1, y, 10);
if (y < lastRow) addNode(node, x - 1, y + 1, 14);
if (y > 0) addNode(node, x - 1, y - 1, 14);
}
if (y < lastRow) addNode(node, x, y + 1, 10);
if (y > 0) addNode(node, x, y - 1, 10);
i++;
}
return path;
}
 
private void addNode (PathNode parent, int x, int y, int cost) {
if (!isValid(x, y)) return;
 
int pathCost = parent.pathCost + cost;
float score = pathCost + Math.abs(x - targetX) + Math.abs(y - targetY);
 
int index = y * width + x;
PathNode node = nodes[index];
if (node != null && node.runID == runID) { // Node already encountered for this run.
if (node.closedID != runID && pathCost < node.pathCost) { // Node isn't closed and new cost is lower.
// Update the existing node.
open.setValue(node, score);
node.parent = parent;
node.pathCost = pathCost;
}
} else {
// Use node from the cache or create a new one.
if (node == null) {
node = new PathNode(0);
node.x = x;
node.y = y;
nodes[index] = node;
}
open.add(node, score);
node.runID = runID;
node.parent = parent;
node.pathCost = pathCost;
}
}
 
protected boolean isValid (int x, int y) {
return true;
}
 
public int getWidth () {
return width;
}
 
public int getHeight () {
return height;
}
 
static private class PathNode extends Node {
int runID, closedID, x, y, pathCost;
PathNode parent;
 
public PathNode (float value) {
super(value);
}
}
}
}

hi nate!
is it possible to use this for diagonal moves too?

It looks like that would work for diagonal moves, see the 14 weight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.