world coordinate patch
[debian/openrocket] / src / net / sf / openrocket / models / gravity / WGSGravityModel.java
1 package net.sf.openrocket.models.gravity;
2
3 import net.sf.openrocket.util.MathUtil;
4 import net.sf.openrocket.util.WorldCoordinate;
5
6 /**
7  * A gravity model based on the WGS84 elipsoid.
8  * 
9  * @author Richard Graham <richard@rdg.cc>
10  */
11 public class WGSGravityModel implements GravityModel {
12         
13         private WorldCoordinate lastWorldCoordinate;
14         private double lastg;
15         
16
17         private static int hit = 0;
18         private static int miss = 0;
19         
20         
21         @Override
22         public double getGravity(WorldCoordinate wc) {
23                 
24                 // This is a proxy method to calcGravity, to avoid repeated calculation
25                 if (wc != this.lastWorldCoordinate) {
26                         this.lastg = calcGravity(wc);
27                         this.lastWorldCoordinate = wc;
28                         
29                         miss++;
30                 } else {
31                         hit++;
32                 }
33                 System.out.println("GRAVITY MODEL:  hit=" + hit + " miss=" + miss);
34                 
35                 return this.lastg;
36                 
37         }
38         
39         
40         @Override
41         public int getModID() {
42                 // The model is immutable, so it can return a constant mod ID
43                 return 0;
44         }
45         
46         
47         private double calcGravity(WorldCoordinate wc) {
48                 
49                 double sin2lat = MathUtil.pow2(Math.sin(wc.getLatitudeRad()));
50                 double g_0 = 9.7803267714 * ((1.0 + 0.00193185138639 * sin2lat) / Math.sqrt(1.0 - 0.00669437999013 * sin2lat));
51                 
52                 // Apply correction due to altitude. Note this assumes a spherical earth, but it is a small correction
53                 // so it probably doesn't really matter. Also does not take into account gravity of the atmosphere, again
54                 // correction could be done but not really necessary.
55                 double g_alt = g_0 * Math.pow(WorldCoordinate.REARTH / (WorldCoordinate.REARTH + wc.getAltitude()), 2);
56                 
57                 return g_alt;
58         }
59         
60 }