Moving entity to waypoint

I have implemented A* into my game, it shows the correct path on several different layouts and works perfectly.

However I am having a little trouble moving my test entity, each node is a small square inside the center of the tile, basically when the center point of the entity lands in this square, we move to the next mode.

The way I am doing this is

  • Get the next node position and the entity position, use atan2 to return the angle between them
  • Move the player to that node using sin/cos and the angle
  • Once reached, select next node in list, repeat

Problem I seem to have is that my entity can move along the path flawlessly if going negative in the y axis or position in the x axis, when I am moving in the other directions the entity seems to skip several nodes on the path, this results in it trying to cut across a wall or something.

I have not done anything special with my code, it is all hardwired for now so that could be the problem, lack of OO design.

However here is my update method in the entity:



public void update() {

		if (isMoving) {
			if (nextNode < pathFinder.getShortestPath().waypoints.size) {
				Node node = pathFinder.getShortestPath().waypoints
						.get(nextNode);
				angle = MathUtils.atan2(
						node.getPosition().y - body.getPosition().y,
						node.getPosition().x - body.getPosition().x);
				if ((body.getPosition().x < node.getPosition().x - node.SIZE / 5)
						|| (body.getPosition().x > node.getPosition().x
								+ node.SIZE / 5)
						&& (body.getPosition().y < node.getPosition().y
								- node.SIZE / 5)
						|| (body.getPosition().y > node.getPosition().y
								+ node.SIZE / 5)) {
					body.applyLinearImpulse(speed * MathUtils.cos(angle), speed * MathUtils.sin(angle), 0, 0, true);
					body.setTransform(body.getPosition(), angle);
				} else {
					 nextNode++;
				}
			}
		}
		

	}


When it starts to move left or down, it seems to get stuck in the else block for 3-4 nodes, ultimately skipping a few and creating a weird path.

Well I fixed it, seriously been sitting staring at this screen for about 10 hours trying to figure it out.

Anyway, rather than checking the x and y seperate, it seemed like a better idea to use THE BUILT IN FUNCTIONS /slap.

Here is what I do now to calculate the position, compare it to the node and decide if we have reached it yet.



/**
	 * Move this enemy to a destination given by the pathfinder
	 */
	public boolean moveToDest() {
		node = path.getWaypoint(nextNode);
		angle = MathUtils.atan2(node.getPosition().y - body.getPosition().y,
				node.getPosition().x - body.getPosition().x);
		if(node.getPosition().dst(body.getPosition()) <= 0.1f){
			nextNode++;
			
		}
		if (body.getPosition() != path.getLastWaypoint().getPosition()) {
			return true;
		}
		return false;
	}



Basically in my update method, if the entity has a path, run this method until it returns false.