Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 2218 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Smiley

Offline ra4king

JGO Kernel


Medals: 356
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.

Offline 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 Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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];
}
Offline 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; }
Offline 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?
Offline 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.

Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!