Getting a direction based on coordinate differences

I think the title might be misleading, but I couldn’t do better.

I have a tiled map, containing monsters. These monsters will walk wherever they please, based on
A*. When they figure out where they want to go, their “path” field will be set with whatever path they should follow.

However, their move method takes these parameters: Direction direction, Tilemap map.

Directions work like this: (this is a little simplified, so it’s easier to read)


public enum DIRECTION {
	NORTH(0, -1),
	SOUTH(0, 1),
	WEST(-1,0),
	EAST(1,0),
	;
	
	private DIRECTION(int dx, int dy){
	}
	
	public int DX()
	{
		return this.dx;
	}
	
	public int DY()
	{
		return this.dy;
	}
}

Now, when they want to move to the next Node in their Path, I can calculate the differences in coordinates.
This gives me the DX and DY, for the direction it needs to go in, but not the actual DIRECTION enum.
How do I get this?

So far, I’ve used a horrible helper, like this:


public static DIRECTION getDirection(int x, int y) {
		if (x == 1) {
			if (y != 0) {
				return null;
			}
			return DIRECTION.EAST;
		} else if (x == -1) {
			if (y != 0) {
				return null;
			}
			return DIRECTION.WEST;
		} else if (x == 0) {
			if (y == 1) {
				return DIRECTION.SOUTH;
			} else if (y == -1) {
				return DIRECTION.NORTH;
			} else { // Both x and y are 0, or invalid arguments passed
				return null;
			}
		} else { // Invalid arguments
			return null; 
		}
	}

However, this is very ugly and quite a bit of a cheap hack. I’m thinking there is probably a better way of getting the DIRECTION, but
I just can’t think of one!

This is where the code is needed:


public void update(GameContainer container, int delta) {
		updateMoveTimer(delta);
		if (getReadyToMove()) {
			if (route != null) {
				//System.out.println(route.getStep(1).getX() + " " + route.getStep(1).getY());
				int desX = route.getStep(stepIndex + 1).getX();
				int desY = route.getStep(stepIndex + 1).getY();
				int diffX = desX - tileX;
				int diffY = desY - tileY;
				/*
				 * I want to use the move method from the superclass,
				 * but I don't know how to convert the differences to directions
				 */
			}
		}
	}

Thanks in advance for all your help :slight_smile:

How could you translate 2 integers to an enum? Your helper method is the only way, but let’s clean it up a bit.


public static Direction get(int dx, int dy) {
    if(dx == 0) {
        if(dy == -1) return NORTH;
        if(dy == 1) return SOUTH;
    }
    if(dy == 0) {
        if(dx == -1) return WEST;
        if(dx == 1) return EAST;
    }
    
    return null;
}

And put that code inside Direction.

Thank you so much :slight_smile:

not the only way (not that it makes a difference)

private static final Direction[] directions = {
     EAST, SOUTH, WEST, NORTH
};

public static final Direction get(int dx, int dy) {
     return directions[dx & 2 | dy & 3];
}

Personally I’d skip on enums in a situation like this. I’d define the directions in a ordered manner and personally would use a logical extension of the mathematical convention. So I’d start with east=0, north (one step couterclockwise) = 1, etc. This way alot of your manipulations become very simple. A bad sketch of what I’m talking about:


  public static final int EAST  = 0;
  public static final int NORTH = 1;
  public static final int WEST  = 2;
  public static final int SOUTH = 3;

  public static int dirLeft(int d)   { return (d+1)&3; }
  public static int dirRight(int d)  { return (d-1)&3; }
  public static int dirBehind(int d) { return (d+2)&3; }

I think you are overdoing it.
When i understood you correctly
-You first calculate the next node a monster should visit to reach its goal
-then you calculate the delta between the next and the current node of the monster
-with this delta you calculate a direction the monster has to go
-and finally depending on the direction you move the monster to another node

so:
node -> delta -> direction -> delta -> node

Why don’t you just move the monster to the node you first calculated?

I specifically made the decision to not just move it, because I have a move-method in the superclass of the monsters, and I’d like to use that, instead of just recklessly moving them around according to pathfinding. There is a chance the pathfinding has bugs, and this is a nice way to
make sure the destination is actually reachable for the monster, before moving it. I don’t know how else I’d do it, without relying on the pathfinding.

you should just rely on the pathfinding, that’s whats it there for.
All the checks should be there. You have to trust your own code^^
Write Unit tests exactly for this kind of bugs and test every time you change your pathfinding