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

LSL Wiki : llGetSunDirection

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ec2-54-87-88-52.compute-1.amazonaws.com
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
There are 17 comments on this page. [Display comments/form]