How to calculate sun position?

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
User avatar
Moohasha
Gnoll
Posts: 672
Joined: Fri Dec 07, 2007 7:37 pm
x 8

How to calculate sun position?

Post by Moohasha »

I would like to be able to take the time of day and the calendar date and be able to get the exact position of the sun relative to the earth (that is, the latitude/longitude of its zenith). I know that the Earth's axial tilt is ~23.5 degrees and can look up the date and time of the summer solstice, so that would give me a baseline, but is there a better way to do this? I can find all sorts of calculators online that will do this, but I need to do it in code.
Black holes are where God divided by 0
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: How to calculate sun position?

Post by Kojack »

No idea what the license is, but here's some c++ code:
http://www.psa.es/sdg/sunpos.htm
(From the Plataforma Solar de Almeria solar research center)

It calculates sun pos based on time, date and lattitude/longitude.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: How to calculate sun position?

Post by jacmoe »

I believe that there's code for that in Caelum - I could be wrong, but do check.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: How to calculate sun position?

Post by lingfors »

Here's some info for you: http://stjarnhimlen.se/comp/ppcomp.html
User avatar
Moohasha
Gnoll
Posts: 672
Joined: Fri Dec 07, 2007 7:37 pm
x 8

Re: How to calculate sun position?

Post by Moohasha »

jacmoe wrote:I believe that there's code for that in Caelum - I could be wrong, but do check.
I checked there, and it calculates sun position based on time of day. I also need it based on time of year since days are shorter in the winter and longer in the summer. Those other two links were way too complicated, so I made a compromise.

The Earth's axial tilt is ~23.5 degrees, and the summer solstice (for the northern hemi) is usually June 21. So I just assumed that at 12:00 UTC, June 21 of any given year, the sun's zenith would be 23.5 latitude, 0 longitude. From there I interpolate longitude based on time of day (linear interpolation), and latitude based on days from June 21 using a cosine function. Thus, the sun should fluctuate between 23.5 latitude (summer solstice) and -23.5 latitude (winter solstice). Doing this, I'm accurate to within 1 degree latitude and 3 degrees longitude according to timeanddate.com, which is an acceptable level of error to me.

Thanks for the links/tips, they got me going in the right direction.
Black holes are where God divided by 0