How to read map for A*

Hey guys, i’m back to work on my pacman game…I’m trying to implement a star pathfinding for the ghosts to follow the player…I’m using Tiled to load a map…I’ve got a separate layer for blocked tiles and i’m loading that layer to an array of blocked tiles…


	for (int y = 0;y < blockedLayer.getHeight();y++ ) {
			for (int x = 0; x < blockedLayer.getWidth();x++) {
							
				//Set blocked tiles
				if (blockedLayer.getCell(x,y)!=null) {
				Rectangle tempRect = new Rectangle(x*16,y*16,16,16);
				blockedTiles.add(tempRect);
				
				if (blockedLayer.getCell(x, y)==null) continue;
				
				}

I’m trying to figure out how to get the blocked and non-blocked tiles together so I can start with the a*

I don’t understand the problem. Can’t you just add all the blocks to one array?

I’m thinking that may be what I need to do…but i’m not exactly sure how. I wrote this a couple months ago and am coming back to it.

Here is my map class.

http://pastebin.com/mde2gV0d

Honestly all you need to do is create a loop that loops through all your tiles in the map, and just add them to an arraylist or array. It shouldn’t be that hard.


 for (int y = 0;y < blockedLayer.getHeight();y++ ) {
         for (int x = 0; x < blockedLayer.getWidth();x++) {
                     
            //Set blocked tiles
            if (blockedLayer.getCell(x,y)!=null) {
            Rectangle tempRect = new Rectangle(x*16,y*16,16,16);
            blockedTiles.add(tempRect);
            
            if (blockedLayer.getCell(x, y)==null) continue;
            
            }

That’s basically what I do here, except I only add the blocked tiles to an arraylist. I guess I could make another list and add the blocked and non-blocked tiles…I just don’t see how I could implement that into the a* algorithm.

This is the first time i’ve ever attempted pathfinding, so please forgive me if I sound like an idiot. I just can’t figure out how to get started.

I think you’re over complicating things. The other thing is that in A*, you only want to search for possible paths. That means you really should only have a list of tiles that aren’t blocking your path. Any solid tiles don’t actually need to be in the list.
Read this:
http://www.policyalmanac.org/games/aStarTutorial.htm

That’s the page i’m getting my a* information from. I see what you’re saying. So I can make another array list just for the walkable tiles which makes sense now.

How would I just check the tiles close to the ghost? I can take the x and y of the ghost and then check through the arraylist and find the tile that the ghost is on…but i’m not sure how I would check the 8 tiles surrounding the ghost?

Well do you store the positions of the tiles in the tile class? If not, you’ll need an array, not an arraylist to store your tiles in. You can then loop through the tiles and get the current position of the loop. Example:


Tile up = null;
for(int x = 0; x < map_size; x++)
for(int y = 0; y < map_size; y++)
up = tiles[player.position.x][player.position.y + 1]

Obviously you’d need to check for out of bounds exceptions etc… But you really shouldn’t use an arraylist as you’ll never actually remove tiles. You may just change the tile type. That’s why an array is better for these situations.

See, that’s the problem i’m having. It’s a tiled tmx map. I’ve figured out how to add the blocked tiles to the arraylist…but i’m not so sure how to add all tiles to a multidimensional array as I would if I hardcoded the map.

Oh, well there’s where I’m of no help at all. I’ve never used tiled tmx map before sorry!

Thanks anyways for the help! lol

I’m just reading away…hopefully I can get it figured out…

Can I see how your tiles are loaded from the map file into the game? Maybe I could help then.

^ How is this in any way related to A* or pathfinding?

Yes.

I was wondering the same thing…Thanks for the attempt at helping though!