# Simple Moon Position Calculation

This topic is 421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know of any very simple moon position calculator? currently I'm using the algorithm from Computing Planetary Positions which I found on vterrain.org. The code I got is:
	float m = JulianDay / 12; //month, approximation
float d = 367 * 2000 - 7 * ( 2000 + (m / 12 + 9) / 12 ) / 4 + 275 * m/9 + JulianDay - 730530;
d = d + (TimeOfDay / 3600.0f) / 24.0;

float N = (125.1228 - 0.0529538083 * d) / 180 * PI;
float i = 5.1454;
float w = (318.0634 + 0.1643573223 * d) / 180 * PI;
float e = 0.054900; //eccentricity
float M = (115.3654 + 13.0649929509 * d) / 180.0f * PI ; //mean anomaly

float E = M + e * sin(M) * ( 1.0 + e * cos(M) );
E = E - ( E - e * sin(E) - M ) / ( 1 - e * cos(E) );

float xv = ( cos(E) - e );
float yv = ( sqrt(1.0 - e*e) * sin(E) );

float v = atan2( yv, xv );

float xh = ( cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(i) );
float yh = ( sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(i) );
float zh = ( sin(v+w) * sin(i) );

MoonPhi = atan2( yh, xh ); //longitude
MoonTheta = atan2( zh, sqrt(xh*xh+yh*yh) ); //latitude


I'm not sure whether this is very correct, the moon does seem to move nicely and continuously but I don't know if the position in the sky is correct.

##### Share on other sites
Check:
http://www.stargazing.net/kepler/moon.html
http://njsas.org/projects/tidal_forces/altaz/pausch/ppcomp.html

##### Share on other sites

your value for i (inclination) is also in degrees and needs the 180  PI added

float i = 5.1454 / 180 * PI;

##### Share on other sites

haha, I used a moon rotation function similar to the one you used.  It moved elliptically which was neat, but I ended up opting for a simple circle rotation to save those few extra processing steps.

##### Share on other sites

This topic is 421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015538
×