Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How do I calculate the position of the sun

Author
Message
Guido Italy
20
Years of Service
User Offline
Joined: 25th Dec 2005
Location:
Posted: 15th Apr 2010 13:30
How do I calculate the position of the sun knowing
latitude / longitude and time?

I have a map, and i want locate the "sunshine" (dbprolight) into correct (really) place ( for correct shadows )

can someone write a sample code ?
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Apr 2010 13:49
The last time I did this, I just had it spin around the center of the game world at a really long range - then move it forward afterwards.

Maybe try converting the time to an angle, then position the sun with trig...

ang#=wrapvalue(timer()/10000.0)
sun_x#=sin(ang#)*100000.0
sun_y#=-50000.0+(cos(ang#)*100000.0)
sun_z#=0.0

Then position a sphere for example at sun_x#,sun_y#,sun_z# - and point it towards the centre of the game world (0,-50000,0 for example). It depends on how your positioning things, like usually it's a good idea to work out an estimate radius for your game world 'planet', so that the sun positioning can take that into account on the Y axis.

It's very basic and you probably have something more elaborate in mind, but it's ok for projects that don't need a lot of accuracy. It might be a good start that you can expand on.


Health, Ammo, and bacon and eggs!
Guido Italy
20
Years of Service
User Offline
Joined: 25th Dec 2005
Location:
Posted: 15th Apr 2010 13:57
Ok ...

But my map is in Really Word , Example :

Latit. =45.3872224
Langit.= 9.1222265

time (example) :16.15 Pm

were i put my sun ?
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Apr 2010 14:13
I see, really you need the MPL3D guy or something - people have done solar system models in DBPro, so someone will be able to provide decent help but you might have to wait a bit on them finding the thread.


Health, Ammo, and bacon and eggs!
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Apr 2010 15:38
Maybe you should ask these guys?
http://www.srrb.noaa.gov/highlights/sunrise/azel.html

...or this might help for a close approximation:
http://www.sunearthtools.com/dp/tools/pos_sun.php

...or you might be able to work it out from this spreadsheet:
http://www.docstoc.com/docs/17807456/Sun-Position-Calculator-Excel-Spreadsheet---Precision-Sundials-LLC-/

...isn't Google great!!

sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 15th Apr 2010 18:04
Tweaked my interest and I found best Google results by searching "sun position algorithm".

Possibly interesting one at:
http://stackoverflow.com/questions/257717/position-of-the-sun-given-time-of-day-and-lat-long

Mmmmmmm maths...
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Apr 2010 18:28
That looks relatively easy to translate...


Guido Italy
20
Years of Service
User Offline
Joined: 25th Dec 2005
Location:
Posted: 15th Apr 2010 22:05
can you translate this code ( from http://stackoverflow.com/questions/257717/position-of-the-sun-given-time-of-day-and-lat-long) ?


sunPosition <- function(year, month, day, hour=12, min=0, sec=0,
lat=46.5, long=6.5) {
twopi <- 2 * pi
deg2rad <- pi / 180

# Get day of the year, e.g. Feb 1 = 32, Mar 1 = 61 on leap years
month.days <- c(0,31,28,31,30,31,30,31,31,30,31,30)
day <- day + cumsum(month.days)[month]
leapdays <- year %% 4 == 0 & (year %% 400 == 0 | year %% 100 != 0) & day >= 60
day[leapdays] <- day[leapdays] + 1

# Get Julian date - 2400000
hour <- hour + min / 60 + sec / 3600 # hour plus fraction
delta <- year - 1949
leap <- trunc(delta / 4) # former leapyears
jd <- 32916.5 + delta * 365 + leap + day + hour / 24

# The input to the Atronomer's almanach is the difference between
# the Julian date and JD 2451545.0 (noon, 1 January 2000)
time <- jd - 51545.

# Ecliptic coordinates

# Mean longitude
mnlong <- 280.460 + .9856474 * time
mnlong <- mnlong %% 360
mnlong[mnlong < 0] <- mnlong[mnlong < 0] + 360

# Mean anomaly
mnanom <- 357.528 + .9856003 * time
mnanom <- mnanom %% 360
mnanom[mnanom < 0] <- mnanom[mnanom < 0] + 360
mnanom <- mnanom * deg2rad

# Ecliptic longitude and obliquity of ecliptic
eclong <- mnlong + 1.915 * sin(mnanom) + 0.020 * sin(2 * mnanom)
eclong <- eclong %% 360
eclong[eclong < 0] <- eclong[eclong < 0] + 360
oblqec <- 23.429 - 0.0000004 * time
eclong <- eclong * deg2rad
oblqec <- oblqec * deg2rad

# Celestial coordinates
# Right ascension and declination
num <- cos(oblqec) * sin(eclong)
den <- cos(eclong)
ra <- atan(num / den)
ra[den < 0] <- ra[den < 0] + pi
ra[den >= 0 & num < 0] <- ra[den >= 0 & num < 0] + twopi
dec <- asin(sin(oblqec) * sin(eclong))

# Local coordinates
# Greenwich mean sidereal time
gmst <- 6.697375 + .0657098242 * time + hour
gmst <- gmst %% 24
gmst[gmst < 0] <- gmst[gmst < 0] + 24.

# Local mean sidereal time
lmst <- gmst + long / 15.
lmst <- lmst %% 24.
lmst[lmst < 0] <- lmst[lmst < 0] + 24.
lmst <- lmst * 15. * deg2rad

# Hour angle
ha <- lmst - ra
ha[ha < -pi] <- ha[ha < -pi] + twopi
ha[ha > pi] <- ha[ha > pi] - twopi

# Latitude to radians
lat <- lat * deg2rad

# Azimuth and elevation
el <- asin(sin(dec) * sin(lat) + cos(dec) * cos(lat) * cos(ha))
az <- asin(-cos(dec) * sin(ha) / cos(el))
elc <- asin(sin(dec) / sin(lat))
az[el >= elc] <- pi - az[el >= elc]
az[el <= elc & ha > 0] <- az[el <= elc & ha > 0] + twopi

el <- el / deg2rad
az <- az / deg2rad
lat <- lat / deg2rad

return(list(elevation=el, azimuth=az))
}
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Apr 2010 23:09
Not tonight but I'll have a fumble with it tomorrow for you

...but I dare anyone to beat me to it!

HowDo
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 15th Apr 2010 23:12
This finds the Julian date and find which moon phases is at.



Dark Physics makes any hot drink go cold.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Apr 2010 00:17
How do @HowDo!

By the way WTF is a Julian date?

Sounds like a date with a very dodgy character...

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 16th Apr 2010 00:45
Google it.... It's simply how many days since Jan 1 for that year so that Jan 5th is Julian day 5.

The fastest code is the code never written.
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 16th Apr 2010 02:55
Quote: "
Google it.... It's simply how many days since Jan 1 for that year so that Jan 5th is Julian day 5.
"


Not quite, Wikipedia (!!) defines it as: "the interval of time in days and fractions of a day since January 1, 4713 BC Greenwich noon", which looks a similar concept to Unix timestamps, ie. A single number to represent time that is easy for computers to add/subtract.

Anyway, I looked some more and this site
http://www.stargazing.net/kepler/sun.html
has an example breakdown of the algorithm, with qbasic source. Easier to understand than that other example, written in `R` which looks to be a maths/science specific language.

Have fun, It was interesting reading about it all, and reinforced my belief that I suck at maths!

Login to post a reply

Server time is: 2026-07-26 12:34:21
Your offset time is: 2026-07-26 12:34:21