I'm writing an Android app that shows moon phases for any date. Currently I am showing a generalized view from the northern hemisphere (sunlit part of the moon moves from right to left with 0º viewing rotation). However, this motion is reversed when viewing the moon from the southern hemisphere, and near the equator, something else happens entirely. Some of this information can be found on this webpage on moon phases.
Instead of using just three simplified cases (northern, southern, and equator), I would ideally like to be able to calculate the exact angle of moon viewing rotation as a function of a latitude and longitude on Earth:
double viewingRotationAngle = calculateAngle(earthLat, earthLng);
Does anyone know how to do this math, or where I could find more information to potentially figure it out myself? I have searched for several days and haven't yet found what I'm looking for. Thanks in advance.
Just to clarify, the angle of rotation I seek is the 2D rotation of the visible disk of the moon.
Update 3/17/14
I did more research and realized this questions may have more to do with the celestial sphere in general rather than the moon specifically. All celestial bodies visible from Earth, including our moon, are fixed on the celestial sphere, and this is what rotates relative to our position on Earth. So instead of focusing on a lunar-centric solution, I need to figure out how to calculate the rotation of the celestial sphere itself. Still haven't wrapped my head around this one.
Update 4/13/14
I found an article on Crescent Moon Tilt which may be the key to solving my problem. The math is obviously out there/can be derived, since most astronomical simulation software can show the exact location and perspective from earth for any celestial body. I think the hard part is that the single piece I want is wrapped into much more complicated equations that cover overall positioning. I will work to derive a simpler version by reading and re-reading this helpful information.