[LIBGDX] Starting out on AI :)

Hi, guys! :slight_smile:

So right now Im starting to really add a real AI for the enemies in my game. Lucky for me, all the enemy is going to do is chase the player. I have used a simple โ€œif(player.getX() > enemy.getX()) speedX = 3;)โ€, but that easily gets the enemy stuck of course. I was wondering if any of you know any examples for AI in Libgdx :slight_smile: I have looked into the A* pathfinding and are considering using that one since it seems like the best choice out there. Does anyone happend to know how I can combine that with the use of Tiled Map Editor too? :slight_smile: Its a simple 2D overview game by the way :wink:

Thanks for reading :slight_smile:

If its a non random world, you can make waypoints that the entity will walk to. Then you can make predefined paths that lead you to these waypoints. Iโ€™d suggest viewing this article on A* though, as you really should include pathfinding instead of baked in paths.
http://www.policyalmanac.org/games/aStarTutorial.htm

So I have read through the site you sent me and Im trying to add it into my game which is using Libgdx and Tiled. I was wondering if you guys could take a look at what Ive got so far and say what you think? :slight_smile:

private Array<Cell> openList = new Array<Cell>();
	private Array<Cell> closedList = new Array<Cell>();
	private Vector2 startingPoint, endingPoint, currentPoint;
	
	private int g_movementCost;
	private int h_heuristic;
	private int f;
	
	private float tileWidth = 32;
	private float tileHeight = 32;
	
	private TiledMapTileLayer layer;
	
	private Cell currentCell;
	private Cell tempCell;
	
	public void createPath(float startX, float startY, float endX, float endY){
		startingPoint.set(startX / tileWidth, startY / tileHeight);
		endingPoint.set(endX / tileWidth, endY / tileHeight);
		
		currentCell = layer.getCell((int) startingPoint.x, (int) startingPoint.y);
		currentPoint.set(startingPoint.x, startingPoint.y);
		openList.add(currentCell);
		
		//Top-Left
		if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y + 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		//Top
		if((tempCell = layer.getCell((int) currentPoint.x, (int) currentPoint.y + 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		
		//Top-Right
		if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y + 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		//Left
		if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		//Right
		if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		//Bottom-Left
		if((tempCell = layer.getCell((int) currentPoint.x - 1, (int) currentPoint.y - 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		
		//Bottom
		if((tempCell = layer.getCell((int) currentPoint.x, (int) currentPoint.y - 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		
		//Bottom-Right
		if((tempCell = layer.getCell((int) currentPoint.x + 1, (int) currentPoint.y - 1)) != null){
			if(!isCellSolid(tempCell)){
				openList.add(tempCell);
			}
		}
		
	}

Im also wondering if the next thing I should do now is to make it not add the same cell again in the openList by checking if its in the closedList, but Im not quite sure on how to do this :frowning: Do I need to loop through the closedList everytime I try to add something in the openList? And also, theres no โ€œtempCell.getX or .getY()โ€ which can give me the coordinate, but I guess it could be possible to make another Vector2 Array that could hold that infomation? :stuck_out_tongue: