Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (605) 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 2285 times) 0 Members and 1 Guest are viewing this topic.
Mads

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: 367
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.

Mads

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?
Mads

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.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (35 views) 2015-03-13 17:50:16 Olo (41 views) 2015-03-13 17:47:07 ClaasJG (40 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 LiquidNitrogen 23x KevinWorkman 21x BurntPizza 21x basil_ 20x EgonOlsen 17x theagentd 16x Roquen 16x Riven 12x princec 12x Varkas 10x SHC 9x Ashedragon 8x NegativeZero 8x lcass 7x ClaasJG 7x teletubo 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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