Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (683) Games in Android Showcase (196) games submitted by our members Games in WIP (751) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Getting a direction based on coordinate differences  (Read 2812 times) 0 Members and 1 Guest are viewing this topic.

JGO Ninja

Medals: 26
Projects: 3
Exp: 6 years

One for all!

 « Posted 2012-03-18 18:49:53 »

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)
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `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:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `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:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `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

ra4king

JGO Kernel

Medals: 435
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #1 - Posted 2012-03-18 19:09:40 »

How could you translate 2 integers to an enum? Your helper method is the only way, but let's clean it up a bit.
 1  2  3  4  5  6  7  8  9  10  11  12 `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.

JGO Ninja

Medals: 26
Projects: 3
Exp: 6 years

One for all!

 « Reply #2 - Posted 2012-03-18 20:08:54 »

How could you translate 2 integers to an enum? Your helper method is the only way, but let's clean it up a bit.
 1  2  3  4  5  6  7  8  9  10  11  12 `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

 Games published by our own members! Check 'em out!
counterp

Senior Devvie

Medals: 11

 « Reply #3 - Posted 2012-03-19 05:18:27 »

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

 1  2  3  4  5  6  7 `private static final Direction[] directions = {     EAST, SOUTH, WEST, NORTH};public static final Direction get(int dx, int dy) {     return directions[dx & 2 | dy & 3];}`
Roquen
 « Reply #4 - Posted 2012-03-19 10:26:02 »

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:

 1  2  3  4  5  6  7  8 `  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; }`
Danny02
 « Reply #5 - Posted 2012-03-19 14:05:38 »

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?

JGO Ninja

Medals: 26
Projects: 3
Exp: 6 years

One for all!

 « Reply #6 - Posted 2012-03-20 03:04:44 »

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.

Danny02
 « Reply #7 - Posted 2012-03-20 06:23:25 »

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
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 orrenravid (199 views) 2016-07-16 03:57:23 theagentd (271 views) 2016-07-11 14:28:54 Hydroque (355 views) 2016-07-06 05:56:57 Hydroque (511 views) 2016-07-03 08:52:54 GrandCastle (382 views) 2016-07-01 09:13:47 GrandCastle (375 views) 2016-07-01 09:09:45 CopyableCougar4 (429 views) 2016-06-25 16:56:52 Hydroque (407 views) 2016-06-22 02:17:53 SwampChicken (372 views) 2016-06-20 13:22:57 SwampChicken (300 views) 2016-06-20 13:22:49
 Archive 29x Brynn 23x EgonOlsen 22x Slyth2727 22x orangepascal 22x TritonDreyja 22x orange451 20x DavidBVal 19x Spasi 18x theagentd 13x KaiHH 12x LiquidNitrogen 11x Phased 11x Opiop 11x princec 11x Longor1996 10x
 Making a Dynamic Plugin Systemby Hydroque2016-06-25 00:13:25Java Data structures2016-06-13 21:22:09Java Data structures2016-06-13 21:20:42FPS Camera Tutorialby Hydroque2016-05-22 05:40:58Website offering 3D Models specifically for games for freeby vusman2016-05-18 17:23:09Website offering 3D Models specifically for games for freeby vusman2016-05-09 08:50:56Website offering 3D Models specifically for games for freeby vusman2016-05-06 11:10:21Website offering 3D Models specifically for games for freeby vusman2016-04-29 12:56:17
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org