Entity skipping path - tower defense

Hi guys, I have a weird problem right now where the entity will follow the path perfectly, that is until he has to go down. I’ve included a picture to show what I mean.

Pink = path the entity HAS taken
blue = path entity should follow (This is just a path made in Tiled)
red = entity
For some reason he skips a large section and then stops moving at that spot. This happens whenever the path goes down. I think my problem might be somewhere in the path creation area? Though i’m not sure D:

http://s8.postimg.org/dc0w9zsg5/Path_Example.png

Below is the code that sets the path to follow:


TiledMapTileLayer layer = (TiledMapTileLayer) levelMap.getLayers().get("Path Layer");
		
		for(int x=0; x < layer.getWidth(); x++){
			for(int y=0; y < layer.getHeight(); y++){
				Cell cell = layer.getCell(x, y);
				
				if(cell != null){
					
					TiledMapTile currentTile = cell.getTile();
					
					if(currentTile != null && currentTile.getProperties().containsKey("Path")){
						float tx = x * layer.getTileWidth();
						float ty = y * layer.getTileHeight();
						
						levelPath.addPathTile(tx, ty);
					}
				}
			}
		}

And this is the code that makes the entity follow the path:



			Vector2 direction = new Vector2(path.getPath().get(entity.getPathIndex()).getX() - entity.getX(), path.getPath().get(entity.getPathIndex()).getY() - entity.getY());
			direction.nor();
			
			entity.setX(entity.getX() + direction.x);
			entity.setY(entity.getY() + direction.y);
				
			if(entity.getX() == path.getPath().get(entity.getPathIndex()).getX() && entity.getY() == path.getPath().get(entity.getPathIndex()).getY()){
					entity.setPathIndex(entity.getPathIndex() + 1);
			}

I’m not sure why the issues are specifically happening, perhaps you have other code doing things. However, it looks to me like you’re constructing your path array in a manner that would result in this flow, assuming your +y is upwards:

http://s24.postimg.org/h7rurhi9x/Path_Example.png

It may be best to find a solution to that first.

Hmm. I’ll try and come up with a new way of creating the path.

I actually took the first thing I thought of so that might by why i’m getting issues. Maybe I could make the entity look for the path on the fly? If the cell above him is a path, etc?

Sure, you can do that. Although it can be precomputed, and every entity can use the same path. The movement method won’t be the most reliable approach either way, particularly if you start scaling by delta times, but it’s a starting point. I’m not sure on the best approach for that.

I was thinking of the style A* uses and keeping the method I have now. I could give each a H, F and G cost and then order the path with the lowest F cost to the highest :slight_smile:

Hmmm, still stuck. I tried giving each tile a value and sorting them but I got the same result. I think the only way I can do this is to check the surrounding tiles, set parents and then trace the path back

If you only plan on having a single path, it’s easy enough to get the path in the order you need. Something like this:

set current tile to starting tile
while path not complete
	side path found = false
	for each adjacent tile of the current tile
		if adjacent tile is a path tile and not the same as the last tile
			push tile to path list
			set current tile to tile
			side path found = true
	if side path not found
		path is complete

If you’re using A*, it should already return the path in the order you need. I’d only use A* if you intend to have multiple possible paths through your level.

This is literally what I just started coding about 35 mins ago :o weird xD

Fixed! :point: I did most of the code but I ended up getting really annoyed so I popped over to CokeAndCode (Kevin Glass) and sorted the problem out.

One of the big issues was that I was using an arraylist for the nodes on the fly, which made it a nightmare. So I created a 2D array that holds all the possible nodes and creates steps at each one. The code then gets each neighbour and checks if it is on the closed list/contains the property “Path” and then adds it to the path list. The code is extremely similar to the A* pathfinding I’ve built before, but without all the value calculations.

All in all, I’m pretty happy as I feel I’ve learnt something and can hopefully apply it at a later date ;D