sun engine

hi,

here’s a script you can use to animate the sun in your game.
the location of the sun depends on latitude, longitude and the current time.
there’s a lot of options available (overlay date/time display, use localtime, switch between different dates, ‘true’ time etc…), and it’s based on the fiat_lux and sun_D scripts.

locate a sun object (like with Fiat_Lux) :
http://jerome.le.chat.free.fr/images/stories/scripts/sun_engine/step1.png

or use an axis (like with sun_D) :
http://jerome.le.chat.free.fr/images/stories/scripts/sun_engine/step14.png

in game display :
http://jerome.le.chat.free.fr/images/stories/scripts/sun_engine/final.png

… and it needs a community review, I suppose the sun location is exact, but there’s maybe some mistakes… also if there’s a need for aditionnal functions, please let me know.

documentation and download :
http://jerome.le.chat.free.fr/index.php/en/blender/scripts/sun-engine.html

hope this can help. :cool:

really great! thanks for the effort. will try it out soon

aw… u beat me to it… i was woking on something very similar to this… anyway nice work!

Yes Finally! I’ll keep bugging you after I finish some work :stuck_out_tongue:

thanks guys, I hope this can help. there’s a (not so) hidden demo file in the doc with a complete setup.
that would be great if by chance an astronomer could check the accuracy of the sun location :).

next update will add support for global dict variables, ( easier values call/update for your own script)
that would be cool also to have a moon…

thanks for posting looks interesting.

hi littleneo,

I’ve been playing around with this. It works really well. :yes: Great job.

Clark

that would be great if by chance an astronomer could check the accuracy of the sun location

not an astronomer…have been called a mathematician (and worse) :slight_smile:

have checked the astronomical calcs in fiat_lux and they are faithful to the source:

except for variations in sign conventions. (fiat_lux uses azimuth = 0 at north, and east longitudes are positive)

also, i have compared sunrise, sunset, transit times from fiat_lux with official data from five very different locations at four different times of year. the comparisons are excellent, max variation 8 minutes. average variation 4 minutes. generally fiat_lux calculates sunrises a few minutes later and sunsets a few minutes earlier than other reported data. who is going to notice?

so pretty good i would say!

will have a look through sun.py when i have some more time

not an astronomer…have been called a mathematician (and worse) :slight_smile:

:smiley:
thanks for the info steeve. this script use the fiat_lux core functions only.
I did some empiric comparaisons with the sunrise/sunset exact time given by meteo websites, it looks accurate :slight_smile:

another thing I’m wondering : location lookup websites display lat and long for a selected place.
but I guess we should also need the altitude no ? I also guess this parameter is not important in most of the case.

btw here’s a demo file, with the tutorial finished + some extras :
http://jerome.le.chat.free.fr/index.php/en/blender/scripts/sun-engine/sun-position-mecanism-alternative.html

littleneo, fiat_lux and sun.py use slightly different calculations. Any calc of the sun’s position is of course a series of approximations. some methods are a little less approximate than others. without looking too deeply, i would say the calcs in fiat_lux are perhaps slightly less approximate…but it really makes no difference at this level…plus or minus a few minutes. Anyway fiat_lux produces accurate sun positions, so great!

altitude (above sea-level) is of little importance. (sun-earth distance = 149 million km … highest mountain 8850 km)

of course if you happen to live in a deep valley, sunrise and sunset times will be different!

thanks for your work, this is great.

i am new to the GE…it seems that at present shadows in GE can only be produced by a script - is that right? because it would be cool if the sun could cast shadows. is it possible?

i am new to the GE…it seems that at present shadows in GE can only be produced by a script - is that right? because it would be cool if the sun could cast shadows. is it possible?

yes it’s possible and you don’t need a script to do so, just a spot :slight_smile:
but you’ll have to cheat if your world is big, as shadows require a lot of cpu time, and because only spots can generate shadows in the GE. (thus shadows are not parallels)

the trick I’ve found for now, is a only shadow spot parented to the current camera focus that copies the sun orientation. the spot is about 80-90 blender units high (I mean the distance between focus and spot is 80-90), so it tends to reduce the shadow distorsions (but it’s expensive, shadow buffer=4096). his location depend of the focus point as it is parented to it.

here’s a wip project that uses this technic :
http://jerome.le.chat.free.fr/3d/pics/tmp/geworldwip.png

starting from the .blend above, you can try :

from a front view,
select the sun shift-s cursor -> selection
add a spot
parent it to the sun lamp
select spot, g, z and locate between the sun and the ground
spot config : only shadow, buffer shadow, bias 0.1
… and add a cube

GLSL mode, 3d viewer drwtype textured (alt-Z)

thanks for that! it will take me some time to work through it

a question: in your “how to” on your web page, you say:

link the sensor to the actuator
i think this shoud be 'link the sensor to the controller"?

about the sun_engine.py script:
to simplify, some functions are not used and could be removed from the script. for example, “isBisestile”, “Julian”.
also, imo it doesn’t make sense to include seconds…the calcs are not that accurate…

now, about the moon. here is a little patch which calculates moon positions. one good thing is, the source is the same as for fiat_lux. i have kept variable, function names etc the same as in fiat_lux. the results are generally quite accurate (3 or 4 degrees) but in some cases, i have noticed a variation as high as 9 degrees. i will look into it a bit deeper. there’s a good javascript page here for calculating sun and moon positions as reference:
http://www.satellite-calculations.com/Satellite/suncalc.htm

# calculates moon positions.
# calcs sourced from http://www.astro.uu.nl/~strous/AA/en/reken/hemelpositie.html#maan
# intended as a patch for sun_engine.py
# the initial defs, variables etc are only required to run this as a stand-alone script.

import math
from math import floor, radians as rad, degrees as deg, sqrt

def cos(x):
    return math.cos(rad(x))
    
def sin(x):
    return math.sin(rad(x))

def arcsin(x):
    return deg(math.asin(x))
    
def tan(x):
    return math.tan(rad(x))
    
def atan(x):
    return deg(math.atan(x))
    
def atan2(x, y):
    return deg(math.atan2(x, y))
        
def AngleNorm(angle):
    '''Normalizes an angle between 0 and 360 degrees
    '''
    return angle % 360.0

def B60toB10(pris, mins):
    '''Converts from base 60 to base 10
    '''
    return pris + mins / 60.0

def Greg2J(Day, Month, Year, Hour, Minute):
    '''Converts from Gregorian dates to Julian dates
    '''
    time_dec = B60toB10(Hour, Minute) / 24.0
    day_dec = Day + time_dec
    if Month < 3:
        Month += 12
        Year -= 1
    c = 2 - floor(Year / 100) + floor(Year / 400)
    JD = floor(1461 * (Year + 4716) / 4) + floor(153 * (Month + 1) / 5) + day_dec + c - 1524.5
    return JD
    
Lat, Long = 48.87, -2.33
Day, Month, Year, Hour, Minute = 12, 12, 2009, 17, 0
JD2000 = 2451545
JD = Greg2J(Day, Month, Year, Hour, Minute)

# geocentric ecliptic longitude, equinox, mean anomaly
ML = 218.316 + 13.176396 * (JD - JD2000)
MM = 134.963 + 13.064993 * (JD - JD2000)
MF = 93.272 + 13.229350 * (JD - JD2000)

# geocentric ecliptic coordinate, moon longitude, moon latitude            
MLong = ML + 6.289 * sin(MM)
MLat = 5.128 * sin(MF)

#print AngleNorm(MLong), AngleNorm(MLat)

# Moon's equatorial coordinates
RA =  atan2(sin(MLong) * cos(23.45), cos(MLong))            # right ascension
Decl = arcsin(sin(MLong) * sin(23.45))        # declination

# Sidereal time at location
ST = 280.1600 + 360.9856235 * (JD - JD2000) + Long
ST = AngleNorm(ST)
H = ST - RA

# Moon's Azimuth
mazimuth = atan2(sin(H), cos(H) * sin(Lat) - tan(Decl) * cos(Lat)) + 180

# Moon's Altitude
maltitude = arcsin(sin(Lat) * sin(Decl) + cos(Lat) * cos(Decl) * cos(H))

print mazimuth, maltitude

moon !! nice !! it I will append your patch asap and test it :slight_smile:
I suppose another hard step would be to simulate the moon phasis…

i think this shoud be 'link the sensor to the controller"?

yes you’re right. controller was implied but I’ll correct it’s not clear.

to simplify, some functions are not used and could be removed from the script. for example, “isBisestile”, “Julian”.

you’re right there aren’t used, time functions does all the job, I forget to remove them after I used epoch time to check the accuracy of the configured date. it helped for a user value check in fiat_lux, but it’s more simple to convert to epoch time then back to ‘human readable’ time.
but time has a 1970-2100 limitations… wait and see.

it doesn’t make sense to include seconds…the calcs are not that accurate…

in fact, it’s useful for display when you use the ‘true’ time option. (END KEY toggle in the demo file).
one can play with the tic interval in the sensor (delay between pulses) to gain CPU time or to tweak the animation :
when tic to 60, sun location is updated one time/second.
when ‘true’ time, there’s a -relative- big cpu gain for the same result.
when forward/backward offset, you obtain one location/second, with hops.
something similar could be used to make a watch…

where do you live? i lives in the us and it says its sunset at 2. any idea?

Wow looks great. Not quite sure what I’ll do with it but either way, thanks.

Edit: To make it acurate do we need our own coordinates, or does it get them automatically?

where do you live? i lives in the us and it says its sunset at 2. any idea?

do you change the latitude/longitude parameters according to your location ?
( I live in Paris, France )

To make it acurate do we need our own coordinates, or does it get them automatically?

as there’s no GPS inside blender, yes, absolutely :wink:
maybe with the IP address… oh nevermind.
I live in a rectangle… lol ! me too !

here’s how :
http://jerome.le.chat.free.fr/index.php/en/blender/scripts/sun-engine/latitude-and-longitude.html

the next page is about date and time. the script retrieves your computer date/time automatically, but it can be useful. please let me know it the documentation is not clear.

Ok gottcha. I guess I missed that part.

Hi…
Thank you for sharing such a nice information. I like this site very much. I will surely bookmark it for future use. Good Work! Keep it up!

Nice! Do you have this wrapped up in a usable script yet, by any chance? This is exactly the sort of thing I have been looking for (the moon patch too!) and your work could save me an awful lot of experimentation as I’m fairly new to Blender and not much of pyhead either (yet).

Thanks in advance!

sure october001, the script is available from the link in the first post (+ a BIG doc from fast/simple setup to more complex/improved) :
http://jerome.le.chat.free.fr/index.php/en/blender/scripts/sun-engine.html
there’s a blend in the doc too. but no moon :confused:

note that for the sun aspect and shadows you’ll have to found your own tricks, it’s not provided in the blend or the script (only the ‘mecanism’), but maybe I wrote something. a gui sample is provided and there’s some hints in the doc about that.
to sum up, spots projected on a dome are very efficient to simulate sun colours, size and shape. :slight_smile:
for shadows it’s a bit more tricky.