Don't click here unless you want to be banned.

LSL Wiki : llGetSunDirection

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl836.us.archive.org
vector llGetSunDirection()

Returns a normalized vector that points toward the sun's current position (and angle of the sim's directional light source). This vector is updated once every 10 seconds.

(As of 21 Nov 2007, when using the FirstLook WindLight viewer, llGetSunDirection will point in the general direction of the "sun" but not precisely towards it. One assumes this will be addressed eventually, but in what form is yet to be clarified. If someone IS aware of it, it would be a good idea to add details and also perhaps to link to any ongoing reference to new or revised functions that may become essential once WindLight rolls out officially? Please revise this statement for clarity. I have heard statements to the effect that solar motion under WindLight is expected to more closely simulate real world celestial mechanics, but have yet to find official confirmation of that rumor.)

The moon is directly opposite the sun at all times, so the negation of the vector returned by this function points towards the moon. (This implies that the moon is always full in SL, which is rather strange, but given the orbital oddities below, this is fairly ignorable.)

A Second Life day is 4 Earth hours long. 0:00 is midnight and 2:00 is high noon. Sunrise is at approximately 0:30 and sunset around 3:30, which results in 3 hours of daylight and only 1 hour of night-time. Refer to the chart below for PST, PDT and GMT times. There is no guarantee this data is accurate:

Phase PST PDT GMT
Sunset 2:30 AM 3:30 AM 10:30
Midnight 3:00 AM 4:00 AM 11:00
Sunrise 3:30 AM 4:30 AM 11:30
Sunset 6:30 AM 7:30 AM 14:30
Midnight 7:00 AM 8:00 AM 15:00
Sunrise 7:30 AM 8:30 AM 15:30
Sunset 10:30 AM 11:30 AM 18:30
Midnight 11:00 AM 12:00 PM 19:00
Sunrise 11:30 AM 12:30 PM 19:30
Sunset 2:30 PM 3:30 PM 22:30
Midnight 3:00 PM 4:00 PM 23:00
Sunrise 3:30 PM 4:30 PM 23:30
Sunset 6:30 PM 7:30 PM 2:30
Midnight 7:00 PM 8:00 PM 3:00
Sunrise 7:30 PM 8:30 PM 3:30
Sunset 10:30 PM 11:30 PM 6:30
Midnight 11:00 PM 12:00 AM 7:00
Sunrise 11:30 PM 12:30 AM 7:30

Effectively, the sun moves faster at night than during the day. However, an equivalent model is that the sun's orbit is uniform and circular but not centered on the world (yes, this implies that in Second Life the sun orbits the world rather than the other way around). The true center of the sun would then be at a point well above the horizon, such that the ratio of day-to-night is approximately 3:1. The normal of the sun's orbital plane would appear to be tilted about 45 degrees on the global x-axis, although it varies more than +/- 5 degrees over a Second Life year (which is approximately 10 days long).

For all intents and purposes, the sun can be considered infinitely far away compared to the scale of the world. That is, its direction (and light) is uniform not only over the entire simulator but over the entire world. The exception is that owners of private islands can fix the position of the sun so that its direction may be different from the direction seen in the majority of the world.

Although a rare occurrence, it is possible for the Lindens to override the phase offset of the sun's orbit, and to even lock it in place. Since this happens worldwide, it is likely that the sun's location is actually controlled from a central source rather than by a deterministic model that is duplicated on each simulator.

One possible application for this function is to determine whether it's day- or nighttime. The following script simply uses the z-axis of the sun's vector to find out if the sun is above or below the horizon:

integer night=0; // 0 = daytime, 1 = nighttime

default
{
    state_entry()
    {
        llSetTimerEvent(300); // Check every 5 minutes
    }
    timer()
    {
        vector sun = llGetSunDirection();
        if (sun.z <= 0) night = 1; // Sun is below the horizon
        else if (sun.z > 0) night = 0; // Sun is above the horizon
    }
    touch_start(integer total_number)
    {
        if (night == 1) llSay(0, "It's nighttime.");
        else if (night == 0) llSay(0, "It's daytime.");
    }
}

If the complete script is to behave different at night, an additional script state may be a better solution than checking a variable:

default // daytime state
{
    state_entry()
    {
        llSetTimerEvent(180); // Check every 3 minutes
    }
    timer() 
    { 
        vector sun = llGetSunDirection(); 
        if (sun.z < 0) state night; 
    }
    touch_start(integer total_number)
    {
        llSay(0, "It's daytime.");
    }    
}

state night
{ 
    state_entry() 
    { 
        llSetTimerEvent(180); // Check every 3 minutes
    } 
    timer() 
    { 
        vector sun = llGetSunDirection(); 
        if (sun.z > 0) state default; // Change back to daytime state
    } 
    touch_start(integer total_number)
    {
        llSay(0, "It's nighttime.");
    }
}

Rotating the object to follow the path of the sun during day time and return to a default position at night

vector sun;

default
{
    state_entry()
    {
        llSetTimerEvent(300); // Check every 5 minutes
    }
    timer()
    {
        vector sun = llGetSunDirection();

        if (sun.z <= 0) { // If it is night, return to default position
            llSetRot( <0, 0, 0, 0> );
        } else if (sun.z > 0) {
            rotation sunRot = ZERO_ROTATION; // Set the rotation to NULL 

            if (sun.z >= 0.0) { // If it is day, set rotation towards the sun
                sunRot = llAxes2Rot(<sun.x, sun.y, 0.0>, <-sun.y, sun.x, 0.0>, <0.0, 0.0, 1.0>);
            }
            llSetRot(sunRot);
        }        
    }
}

Q: Can I set the sun position with a script? Debug mode lets you move the sun.
A: No. Debug mode only changes what you see, not what other people see, or what llGetSunDirection sees. If you own a private island, the estate tools can let you change the sun position within your island, but you can't do that with a script either. llGetSunDirection will get the correct sun position in a private sim.

There was once a footnote here claiming that a Linden said the sun and moon actually do move independently but that an eclipse would not occur for at least 500 RL years. It turns out that this is incorrect; the sun and moon are, in fact, always directly opposite each other.


This article wasn't helpful for you? Maybe the related article at the LSL Portal is able to bring enlightenment.

Functions | Simulator | Weather | Time | Light | WindLight
Comments [Hide comments/form]
well that sucks, it would be pretty cool to see an eclipse. It could be this whole big ordeal like that book signing.
-- SchitsoMonkey (2005-08-06 11:49:32)
I've done extensive research into this function, I'll upload an example script when I've finished debugging it.
-- SevenEightTwo (2005-08-24 09:48:57)
I can see little point for this, save for solar panels facing the sun's direction, or sundials. Anyone here actually done anything with this function?
-- CirrMarat (2005-08-29 10:10:04)
yes, use it to detect sim midnight for use with a simcrash detecter.
-- BlindWanderer (2005-08-29 14:16:21)
Yes! A vampire module for Azelda's MMOGToolkit and a clock :D
-- SevenEightTwo (2005-09-02 03:55:40)
Since I happen to love sundials & solar panels... Here's a sequence which will point the TOP of a prim (the point of a cone, for example) at the sun.
My vector 'rithmetic is pretty rusty. Surely someone has a more concise way to do this? Post it, and in return I'll delete this mess. :) meanwhile, it works.

vector sunV = llGetSunDirection();
vector directionV = sunV;
directionV.z = 0;
directionV = llVecNorm(directionV); drop the vector
float direction = llAtan2(directionV.y, directionV.x);

float elevationCos = sunV * directionV;
dot product is lengths * cos of angle
float elevation = llAcos(elevationCos);

elevation = PI/2 - elevation; It's relative to vertical, you see...
rotation directionR = llEuler2Rot(<0,0,direction>);
rotation elevationR = llEuler2Rot(<0,elevation,0>);
llSetLocalRot(elevationR * directionR);
-- DavanCamus (2005-10-23 02:42:58)
To get a the prim to remain flat/horizontal, yet rotate towards the sun, just remove "rotation elevationR = llEuler2Rot(<0,elevation,0>);" and "elevationR * " from the llSetLocalRot function.
-- EepQuirk (2005-10-23 09:33:10)
I returned the Phase-PST-GMT chart, I got a lot of positive feedback on having it there. It's imformative, accurate and not extraneous.
-- CidJacobs (2005-11-30 23:23:14)
Saw something great today (Easter Sunday), the sun and moon were easter eggs... took pics if anyone missed out, just post a comment on HazVega
-- HazVega (2006-04-16 15:10:26)
I removed the example that was added because there was really no reason for it.
-- DolusNaumova (2006-07-21 08:26:51)
If you want an eclipse, please make a cylinder prim and have it hover over you.

oh! Better yet. World / Force Sun / Midnight. o.O
-- BirdRaven (2006-07-21 13:57:07)
I repeated Eratosthenes experiment to find the diameter of the Earth.
With two observatories 29km apart (North-South), I found no difference in the sun's position.
The world is flat!
-- ChambersCharles (2006-11-20 05:13:08)
Yep, it's flat, and the sun has in infinite size with an infinite distance.
-- BlindWanderer (2006-11-20 06:37:12)
And infinite mass, and somehow removes the helium created fom its fusion processes.
-- CidJacobs (2006-12-15 21:20:50)
Much simpler way to follow the sun:

PointAtSun()
{
vector vecSun = <0.0, 0.0, 0.0>;
vector vecMyPos = <0.0, 0.0, 0.0>;
vecMyPos = llGetPos();
vecSun = llGetSunDirection ();
vecSun.x += vecMyPos.x;
vecSun.y += vecMyPos.y;
vecSun.z += vecMyPos.z;
llLookAt(vecSun, 1.0, 1.0);
}

default
{
state_entry()
{
llSetText("Sun Follower v0.3\nby DangerDave Writer", <1,1,1>, 1.0);
llSetTimerEvent(10);
}

timer()
{
llSetRot(ZERO_ROTATION);
PointAtSun();
}

touch_start(integer total_number)
{
PointAtSun();
}
}
-- DangerDaveWriter (2007-04-19 05:10:27)
Yikes. Needlessly complex and wasteful of code/memory. How about:

default
{
state_entry()
{
llSetTimerEvent(10.0);
}

timer()
{
llLookAt(llGetPos() + llGetSunDirection(), 1.0, 1.0);
}
}
-- GaiusGoodliffe (2007-04-23 03:26:53)
Did LL only ever hire dyslexic people?
The sun goes the wrong way round and when reversing a car, it turns the wrong way!
-- CartmanNoel (2007-07-03 22:37:13)
Attach a comment to this page: