Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Advice on working with Sun Position

nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
08-02-2006 13:27
From: Si Neumann
Wish they'd just give us the rules and save us the pain!!!!!!!


Ah, but where would the fun in that be?

I propose that the sun rotates around SL.

Anyone care to comment on the shape of the waves, they're not quite sinusoidal. Anyone think they can come up with a close relationship? Would anyone like a copy of my test data so far?
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-02-2006 13:33
From: Armandi Goodliffe
My goal was to come up with some kind of way of getting:

Sun up: 0 degress
Noon : 90 degress
Sun down: 180 degrees
Midnight : 270 degrees

and, of course, finding various angles between those times.

Try this:
CODE
float gLastSunZ;

...

vector sunPos = llGetSunDirection();
float angleFromNoon = llAcos(sunPos.z);

if (sunPos.z >= gLastSunZ) // Eh. Might have a tiny bit of trouble around noon/midnight, but whatever
{
// We're in the AM
angleFromNoon = TWO_PI - angleFromNoon;
}
gLastSunZ = sunPos.z;

float angleFromSunrise_rad = angleFromNoon - PI_BY_TWO;
if (angleFromSunrise_rad < 0)
{
angleFromSunrise_rad += TWO_PI;
}

float angleFromSunrise_deg = angleFromSunrise_rad * RAD_TO_DEG;
...

Note that if the sun doesn't actually reach the zenith, it might appear to suddenly go from 10AM to 2PM or somesuch. I'll post a solution shortly.

Anyway, enjoy.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-02-2006 13:49
This code should be much more accurate, BUT it assumes the x-direction points to the east, that the sun moves from east to west during the day, and that its path is roughly symmetrical about the y-axis during the period of one day (as is very, very nearly the case in the real world). I've been too lazy to actually confirm these assumptions in SL; if you'd like you can do so and post any corrections (or your observations and I can try to fix it):
CODE
vector sunPos = llGetSunDirection();
float angleFromSunrise_rad = llAtan2(sunPos.z, sunPos.x);
float angleFromSunrise_deg = angleFromSunrise_rad * RAD_TO_DEG;
...
Si Neumann
Registered User
Join date: 26 Apr 2006
Posts: 12
Hmmm
08-02-2006 14:08
Hewee are you sure?

Using your last method at approx midday I get 94 degrees! Which seems to be increasing - now at 97.

Using:

vSunPos = llGetSunDirection();
float angle = llAcos( vSunPos * llVecNorm( <vSunPos.x, vSunPos.y, 0.0> ) );
angle *= 57.2957795; // Convert to degrees.

Which i got from another thread (Nand Nerd) /54/4b/125628/1.html

I get about 61 degrees at "Midday"

Puzzled now!
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
08-02-2006 14:10
From: Si Neumann

Which i got from another thread (Nand Nerd)


credit should go to Joannah Cramer
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-02-2006 14:54
From: Si Neumann
Hewee are you sure?

Using your last method at approx midday I get 94 degrees! Which seems to be increasing - now at 97.

Using:

vSunPos = llGetSunDirection();
float angle = llAcos( vSunPos * llVecNorm( <vSunPos.x, vSunPos.y, 0.0> ) );
angle *= 57.2957795; // Convert to degrees.

Which i got from another thread (Nand Nerd) /54/4b/125628/1.html

I get about 61 degrees at "Midday"

Puzzled now!

That logic is about the same as my first example, but doesn't differentiate between AM and PM (or night and day for that matter!).

Which logic were you trying? As I said, my first example assumes the sun has no angle of inclination, so you may suddenly jump from 10 AM to 2PM or whatever. Has anyone tested the second method? I'm actually not sure how the world coordinates are aligned. :-o
Si Neumann
Registered User
Join date: 26 Apr 2006
Posts: 12
08-02-2006 16:09
Hewee, i was using:

From: Hewee Zetkin
CODE
vector sunPos = llGetSunDirection();
float angleFromSunrise_rad = llAtan2(sunPos.z, sunPos.x);
float angleFromSunrise_deg = angleFromSunrise_rad * RAD_TO_DEG;
...


Yep, you're correct about Joanah's method not differentiating between AM/PM and NIGHT/DAY, but that's what i personally need - just the angle as i can get AM/PM NIGHT/DAY using different methods.

I'm no expert on radians, 3D rotions etc in LSL so can't be 100% I tested your script correctly so I can't be sure! I also don't know how to record the results over a 4 hour period - praps someone with more knowledge could do that?

Interestingly at SL "Midnight" your script showed -90.7 degrees and Joanah's showed 24.3 degrees. This sun sure has a strange orbit!!!
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
08-02-2006 16:17
From: Si Neumann
Interestingly at SL "Midnight" your script showed -90.7 degrees and Joanah's showed 24.3 degrees. This sun sure has a strange orbit!!!

Think that would be a side effect of arccos() function -- when used on dot product of two vectors, it can only recognize how far 'apart' these vectors are, but not 'direction' of it, so to speak. So you get the same result no matter if sun is above or below horizon... it can be easily fixed by testing raw value of z component of sun direction vector, though. I.e. add something like following as last line:

if( sun.z < 0.0 ) angle *= -1.0;
Si Neumann
Registered User
Join date: 26 Apr 2006
Posts: 12
Yup
08-11-2006 17:20
Brilliant - I think that's the final touch Joanna. ty!
Nyoko Salome
kittytailmeowmeow
Join date: 18 Jul 2005
Posts: 1,378
08-11-2006 18:44
hey there! :) i'm just making myself a 'weather gauge' tonight... and just for sake of cross-reference, i thought i'd post my lil' one-in-all script (it's a royal bee-otch searching the forums, y'know - maybe that's one reason to close 'em, but at the same time, that's losing a valuable resource:\

i'm not sure i got the final advice for sun-tracking straight... please advise, if i'm off on my copy/paste. ;) btw, this has some extras to it - usually this is my 'starting point' script for most stuff, so use away:)

CODE
integer BLNtesting = TRUE;
testMessage(string STRmessage) { // test message handler... \\\\\\\\\\\\\\
float lifetime = llGetTime();
if (BLNtesting == TRUE) {
llOwnerSay((string)lifetime+" "+STRmessage);
}
}

resetAllDefault() {
llSetSitText("");
llSetTouchText("");
llSetText("",<1,1,1>,0); // kill floating text

llTargetOmega(<0,0,1>, 0, 1.0); // kill spin
llParticleSystem([]); // kill particles
llSetTextureAnim(FALSE, ALL_SIDES, 1, 1, 1.0, 1, 0); //kill texture animations

llStopSound();

//llListenRemove(listenHandlePrivate); // Remove the old list
//llListenRemove(); // Remove the old list
}

float FLTseconds = 1;

setTheText() {

string STRmessage;

// SUN 2 - ONGOING DISCUSSION AT:
// http://forums.secondlife.com/showthread.php?t=125628
// http://forums.secondlife.com/showthread.php?t=107757
STRmessage = STRmessage +"Sun positioning: ";
vector VECsunPos = llGetSunDirection();
float FLTsunAngle = llAcos( VECsunPos * llVecNorm( <VECsunPos.x, VECsunPos.y, 0.0> ) );
FLTsunAngle *= RAD_TO_DEG;
string STRsunAngle = (string)FLTsunAngle;
STRsunAngle = llDeleteSubString(STRsunAngle, 5, 9);
STRmessage = STRmessage + STRsunAngle;

// SUN
STRmessage = STRmessage +"\n";
vector VECsun = llGetSunDirection();
float FLTx = VECsun.x;
float FLTy = VECsun.y;
float FLTz = VECsun.z;
if (FLTz < 0) { //night??
STRmessage = STRmessage + "Night";
} else { //day??
STRmessage = STRmessage + "Day";
}

string STRx = (string)FLTx;
STRx = llDeleteSubString(STRx, 4, 9);
string STRy = (string)FLTy;
STRy = llDeleteSubString(STRy, 4, 9);
string STRz = (string)FLTz;
STRz = llDeleteSubString(STRz, 4, 9);
STRmessage = STRmessage + " (" + STRx+"|"+STRy+"|"+STRz+")"; // + "m/s";

// -----
STRmessage = STRmessage +"\n";
STRmessage = STRmessage + "-----";

// WIND
STRmessage = STRmessage +"\n";
vector VECwind = llWind(<0,0,0>);
float FLTwind = llVecMag(VECwind);
string STRwind = (string)FLTwind;
STRwind = llDeleteSubString(STRwind, 3, 9);
//llSay(0,"Wind speed: " + (string)llVecMag(wind));
STRmessage = STRmessage + "Wind speed: " + STRwind + "m/s";

// CLOUDS
STRmessage = STRmessage +"\n";
float FLTcloud = llCloud(<0,0,0>);
string STRcloud = (string)FLTcloud;
STRcloud = llDeleteSubString(STRcloud, 4, 9);
//(clear, cloudy, very_cloudy, raining
if (FLTcloud > 1) {
STRmessage = STRmessage + "'Raining'";
} else if (FLTcloud > .66) {
STRmessage = STRmessage + "Very Cloudy";
} else if (FLTcloud > .33) {
STRmessage = STRmessage + "Cloudy";
} else {
STRmessage = STRmessage + "Clear";
}
STRmessage = STRmessage + " (" + STRcloud + ")";

// -----
STRmessage = STRmessage +"\n";
STRmessage = STRmessage + "-----";

// DILATION
STRmessage = STRmessage +"\n";
integer INTtd = (integer)(llGetRegionTimeDilation() * 100);
STRmessage = STRmessage + "Dilation: "+(string)INTtd+"%";

// SIM FPS
STRmessage = STRmessage +"\n";
float FLTfps = llGetRegionFPS();
string STRfps = (string)FLTfps;
STRfps = llDeleteSubString(STRfps, 4, 9);
STRmessage = STRmessage + "Sim FPS: " + STRfps;

llSetText(STRmessage,<1,1,1>,1);
}

spinMe(integer BLNspinFaster) {
float FLTspin = .25;
if (BLNspinFaster == TRUE) {
FLTspin = 7.5;
}
llTargetOmega(<0,0,1>, FLTspin, 1.0);
//llTargetOmega(vector axis, float spinrate, float gain)
}

default {
on_rez(integer start_param) {
testMessage("on_rez");
llResetScript();
}
state_entry() {
resetAllDefault();
setTheText();
llSetTimerEvent(FLTseconds);
}

touch_start(integer total_number) {
//if (llGetParcelMusicURL) {
}

timer() {
//testMessage("timer");
setTheText();
}

}
_____________________

Nyoko's Bodyoils @ Nyoko's Wears
http://slurl.com/secondlife/Centaur/126/251/734/
http://home.comcast.net/~nyoko.salome2/nyokosWears/index.html

"i don't spend nearly enough time on the holodeck. i should go there more often and relax." - deanna troi
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
08-12-2006 21:36
You know, what would be really cool is if we had a function like:
float llGetLightMeter(vector offset)

which would simply return the light level at a point. At first it could just be calculated based on sun position and local lighting. Later it could take into consideration clouds and even object/avatar shadows.

This would be perfect for little photoelectrically controlled devices/effects.
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
08-13-2006 05:30
That would be a pretty interesting function Hewee, could make a scanning camera with an array of such sensors. Problem is that it would require ray-tracing, which isn't implemented in SL at the moment and (I think) is pretty computationally intensive.

Phillip Linden mentioned that early in SL's development they had proper shadows and specifically a massive shadow was cast by a large (tree-house?) build, maybe some day we'll get to experience these effects again.
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Veriane Morellet
Registered User
Join date: 11 Aug 2006
Posts: 4
08-18-2006 15:18
I haven't read all the posts in this thread in details, but just want to add that simplest way to find the elevation angle of the sun is
CODE

vector sun = llGetSunDirection();
float elevation = 90.0 - llAcos(sun.z)*RAD_TO_DEG;

This works because the inner product of a unit vector pointing to the sun and a unit vector pointing up is simply "sun.z" and arc-cos of that gives the suns zenith angle. Elevation angle is then found from 90 minus zenith angle, with the angles measured in degrees.

If you just need to decide whether the sun is above or below a certain fairly fixed elevation limit you can keep the trigonometry in the initialization:
CODE

float elevationLimit = -6;
float zLimit = llCos((90-elevationLimit)*DEG_TO_RAD);
...
vector sun = llGetSunDirection();
if (sun.z >= zLimit) { // Sun is above elevationLimit
}
Sawdust Fossil
Registered User
Join date: 3 Aug 2006
Posts: 28
08-19-2006 17:16
I have been reading this thread with interest.

I made some observations just now that I'll share:
My Location: Natoma 31, 132, 26
Date: 8/19/06

At sunrise, the Z-value of the rising sun passed 0 at 16:29:30...30 seconds before 16:30
The sun rose approximately Notheast of this position! Interesting.
At 16:59:00 (29 minutes, 30 seconds later), the sun was due East (and higher)!

I wonder if the sun will pass directly overhead...I'm not sure that it will. Of course, in real life, the sun never passes overhead if you are outside the Tropics of Cancer and Capricorn.

Would measurements made on the northern continent match those on the southern continent? I would be cool to have two people in am IM session coordinate this. Let me know if you want to try. :-)
1 2