Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Class for managing locations within a world.  (Read 2012 times)
0 Members and 1 Guest are viewing this topic.
Offline ap_kelly

Junior Devvie




Java rocks!


« Posted 2002-10-23 03:17:21 »

Here is a class I wrote to handle locations within my world. I use Latitude and Longitude and this class will let me work out the distance between 2 points as well. (Not sure the maths is 100% correct, but it could be useful to someone)

Andy.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
public class GPSLocation {

  private static final int worldRadius = 6378; // Earth radius in km.

  private float latDegrees;
  private int   latMinutes;
  private int   latSeconds;
  private char  latDirection;

  private float lonDegrees;
  private int   lonMinutes;
  private int   lonSeconds;
  private char  lonDirection;

  private int   altitude; // Measured in meters above sea level.

  public GPSLocation() {
    latDegrees = 0;
    latMinutes = 0;
    latSeconds = 0;
    latDirection = 'N';

    lonDegrees = 0;
    lonMinutes = 0;
    lonSeconds = 0;
    lonDirection = 'E';

    altitude = 0;
  }

  // Get methods.
  public float getLatDegrees() {
    return latDegrees;
  }

  public int getLatMinutes() {
    return latMinutes;
  }

  public int getLatSeconds() {
    return latSeconds;
  }

  public char getLatDirection() {
    return latDirection;
  }

  public float getLonDegrees() {
    return lonDegrees;
  }

  public int getLonMinutes() {
    return lonMinutes;
  }

  public int getLonSeconds() {
    return lonSeconds;
  }

  public char getLonDirection() {
    return lonDirection;
  }

  public int getAltitude() {
    return altitude;
  }

  public double getLatRadians() {
    double res1 = latDegrees + (latMinutes + latSeconds/60) /60;
    if (latDirection == 'S') {
      res1 *= -1;
    }
    return Math.toRadians(res1);
  }

  public double getLonRadians() {
    double res1 = lonDegrees + (lonMinutes + lonSeconds/60) /60;
    if (lonDirection == 'W') {
      res1 *= -1;
    }
    return Math.toRadians(res1);
  }

  // Set methods.
  public void setLatDegrees(float pLatDegrees) {
    latDegrees = pLatDegrees;
  }

  public void setLatMinutes(int pLatMinutes) {
    latMinutes =  pLatMinutes;
  }

  public void setLatSeconds(int pLatSeconds) {
    latSeconds = pLatSeconds;
  }

  public void setLatDirection(char pLatDirection) throws Exception {
    char dir = Character.toUpperCase(pLatDirection);
    if ((dir == 'N') || (dir == 'S')) {
      latDirection = dir;
    } else {
      throw new Exception("Invalid direction for Latitude, (N)orth or (S)outh only.");
    }
  }

  public void setLonDegrees(float pLonDegrees) {
    lonDegrees = pLonDegrees;
  }

  public void setLonMinutes(int pLonMinutes) {
    lonMinutes =  pLonMinutes;
  }

  public void setLonSeconds(int pLonSeconds) {
    lonSeconds = pLonSeconds;
  }

  public void setLonDirection(char pLonDirection) throws Exception {
    char dir = Character.toUpperCase(pLonDirection);
    if ((dir == 'E') || (dir == 'W')) {
      lonDirection = dir;
    } else {
      throw new Exception("Invalid direction for Longitude, (E)ast or (W)est only.");
    }
  }

  public void setAltitude(int pAltitude) {
    altitude = pAltitude;
  }

  // Utility methods.
  public double getDistance(GPSLocation otherLocation) {
    double a = this.getLatRadians();
    double b = this.getLonRadians();
    double c = otherLocation.getLatRadians();
    double d = otherLocation.getLonRadians();

    // This calculation should take into account the altitude of the 2 locations.

    if ((a == c) && (b == d)) {
      return 0;
    }

    double res1 = Math.sin(a) * Math.sin(c) + Math.cos(a) * Math.cos(c) * Math.cos(b-d);

    if (res1 > 1) {
      return worldRadius  * Math.acos(1);
    } else {
      return worldRadius  * Math.acos(res1);
    }
  }

} // Class

Offline ap_kelly

Junior Devvie




Java rocks!


« Reply #1 - Posted 2002-11-04 00:01:39 »

I posted this code a while ago, but no comments have been made about it, not that I expected loads of them, but I'd be interested to hear peoples thoughts on this class.

Each of the objects in my world has an attribute location, which is of type GPSLocation, its a nice handy utility especially for games that required real-world type locations, rather than just x,y,z.

Has anyone had a chance to verify the maths I'm using? I can't remeber the site where I got it from, but I have seen other bits of code that gives different answers.

Andy.

Offline budoray

Innocent Bystander




Java games rock!


« Reply #2 - Posted 2003-05-02 18:49:25 »

At first glance .. I would change

private char latDirection to

private short latDirection.

Same with lonDirection. Go on to declare your 'compass' in this manner ..

public static final short N = 0;
...etc

Refactor your sets and gets ..

This will save you time of comparing strings. Your new test will be similiar to

if(direction == N)

If you add more compass directions .. then you can modify the if/else tests to a switch statement. It will also allow you to scale fairly easy. Like the following

public static final short N         = 0;
public static final short NORTH = 0;

if(dir == N) // will handle N and NORTH.

now you would save yourself the trouble of

char dir = Character.toUpperCase(pLatDirection);

only to test

if((dir == "N") || (dir == "NORTH"))

and as you add more directions .. you would do

switch(dir){
  case N: // falls through to NORTH
  case NORTH:
       doSomething;
       break;
  case NE: // falls through to NORTHEAST
  case NORTHEAST:
       doSomething();
       break;
  etc..
}

where adding these as strings would result in

char dir = Character.toUpperCase(pLatDirection);

if((dir == "N") || (dir == "NORTH")) {
   doSomething();
} else if((dir == "NE") || (dir == "NORTHEAST")) {
      doSomething();
} etc ...

I hope I haven't confused you by overexplaining. In java .. short and char are both 2 bytes. The char will not allow you to account for NORTHEAST clearly where a String would, but the compares are somewhat expensive. That's why I am suggesting the use of a short instead of an int. If I still have you in the dark .. shoot me an email and I will send you some code that demonstrates the use of shorts over a char or String.

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

Elsealabs (13 views)
2014-12-28 10:39:27

CopyableCougar4 (17 views)
2014-12-28 02:10:29

BurntPizza (21 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

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

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

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

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

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
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!