Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Basic Mechanical Clock Script

Phoenix Ristow
Registered User
Join date: 11 Sep 2006
Posts: 9
05-19-2007 01:33
CODE
//HINT:  Set up your clock face then dupliate that prim and reshape for a clock hand
//Use a hand texture that is the hand on one side and total invis on the other so your
//hand rotates around the center of the face and you avoid rotation issues. Set your
// hands to 12am...turn on the script and it iwll automatically set the hands to SL time on
// startup. Enjoy.

//uncomment the type of hand this piece is - second,minute, hour

//integer hand_type =1;//seconds
//integer hand_type =2;//minutes
//integer hand_type =3;//hours

//do not edit below
integer t;
integer hours;
integer minutes;
integer seconds;
integer time_interval;
integer rot_num;

rotation hand_rot;
rotation start_rot;
Set_Hand(){
integer x = time_interval;
for(x=0;x < time_interavl + 1;x++)
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0,0, PI/rot_num>);
rot = delta * rot;
llSetLocalRot(rot);
}
hand_rot = llGetLocalRot();
llSetTimerEvent(60.0);
}


default
{
state_entry()
{
llListen(0,"",llGetOwner(),"");
hand_rot = llGetLocalRot();
start_rot = llGetLocalRot();
t = (integer)llGetWallclock();
hours = t / 3600;
minutes = (t % 3600) / 60;
seconds = t % 60;
if(hand_type==1)
{
time_interval = seconds;
rot_num = 30;
}
else if (hand_type-==2)
{
time_interval=minutes;
rot_num=30;
}
else
{
time_interval=hours;
rot_num = 6;
}

Set_Hand();
}

listen(integer channel,string name,key id,string message)
{
if(llToLower(message)=="resetclock")
{
llSetLocalRot(start_rot);
llResetScript();
}
}


timer()
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0,0, PI/rot_num>);
rot = delta * rot;
llSetLocalRot(rot);
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
05-23-2007 22:21
/15/86/184910/1.html
_____________________
i've got nothing. ;)
Erik Bligh
Registered User
Join date: 25 Feb 2006
Posts: 27
05-26-2007 10:24
this script has syntax errors
Mandukin Clark
Registered User
Join date: 14 Aug 2006
Posts: 3
06-28-2007 09:28
Yes it has several syntax errors.
Jabika Yifu
Registered User
Join date: 4 Jun 2007
Posts: 1
Hour hand not working
06-30-2007 19:51
I used this for a clock I built. I fixed the syntax errors I found but still have a problem with the hour hand. It moves frequently and does not show the correct hour. Minute hand works fine. Anyone have a fix for the hour hand? Or a better script for a clock?
Elmer Voss
Registered User
Join date: 23 Mar 2007
Posts: 1
07-07-2007 15:39
I managed to fix the errors too. But when I try to use the script, the prim rotates in a jerky fashion for a few seconds then stops cold. This happens regardless of the hand setting.
Kal Rau
Registered User
Join date: 26 Apr 2007
Posts: 2
08-22-2007 12:57
Please, if this script have syntax error, anyone can post fixed script??

ThX
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
08-28-2007 05:09
From: Jabika Yifu
I used this for a clock I built. I fixed the syntax errors I found but still have a problem with the hour hand. It moves frequently and does not show the correct hour. Minute hand works fine. Anyone have a fix for the hour hand? Or a better script for a clock?



I do.. but it's my business, so I won't be posting my scripts anytime soon, but one hint, it's alot easier to paint a clock hand onto the flat of a cylinder and rotate the texture instead of using prim hands.
Darko Lednev
Registered User
Join date: 20 Aug 2007
Posts: 31
08-28-2007 12:15
This one should work...works fine for me.

integer pointerType = 3; // 1 for Seconds, 2 for Minutes, 3 for Hours

float mainClock;
float hours;
float minutes;
float seconds;
integer timeInterval;
integer rotationNum;
float timerNum;

rotation pointerRot;
rotation startRot;

setPointer()
{
integer x = timeInterval;
for(x=0; x < timeInterval + 1; x++)
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0, 0, -TWO_PI / rotationNum>;);
rot = delta * rot;
llSetLocalRot(rot);
}
pointerRot = llGetLocalRot();
llSetTimerEvent(timerNum);
}

default
{
state_entry()
{
llListen(0, "", llGetOwner(), "";);
pointerRot = llGetLocalRot();
startRot = llGetLocalRot();
mainClock = llGetWallclock();
hours = llFloor(llFloor(mainClock / 60) / 60) - 1;
minutes = ((integer)mainClock % 3600) / 60;
seconds = (integer)mainClock % 60;

if (pointerType == 1)
{
timeInterval = (integer)seconds;
timerNum = 1.0;
rotationNum = 60;
}
else if (pointerType == 2)
{
timeInterval = (integer)minutes;
timerNum = 60.0;
rotationNum = 60;
}
else if (pointerType == 3)
{
timeInterval = (integer)hours;
timerNum = 3600.0;
rotationNum = 12;
}

setPointer();
}

listen(integer channel, string name, key id, string message)
{
if(llToLower(message) == "resetclock";)
{
llSetLocalRot(startRot);
llResetScript();
}
}

timer()
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0, 0, -TWO_PI / rotationNum>;);
rot = delta * rot;
llSetLocalRot(rot);
}
}
Dioxide DeSantis
Registered User
Join date: 2 Feb 2007
Posts: 63
12-09-2007 20:35
Thought I would add a bit to the script.
I wanted a way to reset the script on rez so I did not have to manually do this every time I rezed the clock.

Step 1
Build a simple face for your clock
Step 2
Add the hands then get the script in each hand set accordingly
Step 3
Set both hands to 12 O Clock (Basically set the hands pointing at the top)
Step 4
Link the clock use the face as the main prim
Step 5
Take the object into your inventory
Step 6
Rez the clock should set itself to SL time automatically


-------------------------------------------------------------
See code Below Formatted in a Quote for easy copy
-------------------------------------------------------------

From: someone

integer pointerType = 2; // 1 for Seconds, 2 for Minutes, 3 for Hours

float mainClock;
float hours;
float minutes;
float seconds;
integer timeInterval;
integer rotationNum;
float timerNum;

rotation pointerRot;
rotation startRot;

setPointer()
{
integer x = timeInterval;
for(x=0; x < timeInterval + 1; x++)
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0, 0, -TWO_PI / rotationNum>;);
rot = delta * rot;
llSetLocalRot(rot);
}
pointerRot = llGetLocalRot();
llSetTimerEvent(timerNum);
}

default

{
on_rez(integer start_param)
{
// Restarts the script every time the object is rezzed
llResetScript();
}
state_entry()
{
llListen(0, "", llGetOwner(), "";);
pointerRot = llGetLocalRot();
startRot = llGetLocalRot();
mainClock = llGetWallclock();
hours = llFloor(llFloor(mainClock / 60) / 60) - 1;
minutes = ((integer)mainClock % 3600) / 60;
seconds = (integer)mainClock % 60;

if (pointerType == 1)
{
timeInterval = (integer)seconds;
timerNum = 1.0;
rotationNum = 60;
}
else if (pointerType == 2)
{
timeInterval = (integer)minutes;
timerNum = 60.0;
rotationNum = 60;
}
else if (pointerType == 3)
{
timeInterval = (integer)hours;
timerNum = 3600.0;
rotationNum = 12;
}

setPointer();
}

listen(integer channel, string name, key id, string message)
{
if(llToLower(message) == "resetclock";)
{
llSetLocalRot(startRot);
llResetScript();
}
}

timer()
{
rotation rot = llGetLocalRot();
rotation delta = llEuler2Rot(<0, 0, -TWO_PI / rotationNum>;);
rot = delta * rot;
llSetLocalRot(rot);
}
}
Ceera Murakami
Texture Artist / Builder
Join date: 9 Sep 2005
Posts: 7,750
01-05-2008 15:02
There is still one problem with this script. If you rez the clock or reset the clock at any SL time other than the top of the hour, the hour hand doesn't increment until that number of minutes past the hour.

For example: If you rez the clock at 1:20 PST, the latest version of this script sets the clock to 1:20 just fine, and it keeps good time for the next 40 minutes. But when it should switch over to 2:00, the hour hand doesn't move. From 2:00 to 2:20, it stubbornly insists it's an hour slow, displaying 1:00 to 1:20. Then at 2:20 the hour hand finally catches up! This problem isn't obvious on the minute or second hands, because the difference in when it moves is so small. But it is there as well.

I think what is needed is a one-time correction in the hour timer, subtracting (60 * Minutes) from the starting count on the hour timer. Then at the first hour change, the start value for the hour timer should switch to 3600 again.

I haven't sat down with this long enough to figure out exactly what to change where for that correction, but I'll try eventually, if no one beats me to it.
_____________________
Sorry, LL won't let me tell you where I sell my textures and where I offer my services as a sim builder. Ask me in-world.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-05-2008 16:22
the modulus operator (%) would fix that right up
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Ceera Murakami
Texture Artist / Builder
Join date: 9 Sep 2005
Posts: 7,750
01-07-2008 07:49
Thanks Void. I am considering writing a complete re-work on this clock script idea.

One factor I was considering was to add a script in the root prim (the clock face) to get its rotation, so the hands could be reset to noon by the script reset process, instead of having to be manually processed. With that, and with a 'set the clock' function that doesn't care what time of day it is when you invoke it, I should be able to eliminate most of the inconsistencies inherent in this one. I'll also add the code I have worked up to make it chime the hour audibly.

If anyone wants to contribute ideas here, feel free. I'll put the revised script into the script library once I have time to write it.
_____________________
Sorry, LL won't let me tell you where I sell my textures and where I offer my services as a sim builder. Ask me in-world.
ab Vanmoer
Registered User
Join date: 28 Nov 2006
Posts: 131
01-07-2008 12:03
Unless I am missing something, it seems that one doesn't really need to rotate a hand relative to its previous position.
A minute hand needs to move 6 degrees every minute, so its absolute position is 6*NoOfMinutes.


float degrees=(360-6*minutes);
rotation handPos=llEuler2Rot(<0,0,degrees*DEG_TO_RAD>;);
llSetLocalRot(handPos);


ab
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-07-2008 15:46
I'm so tempted to post my sisters grandfater clock script.... I'll supply a hint though

llSetLocalRot( llEuler2Rot( <.0, .0, vIntTime % 60 * 6> * DEG_TO_RAD ) );
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Ceera Murakami
Texture Artist / Builder
Join date: 9 Sep 2005
Posts: 7,750
01-12-2008 06:29
Thank you all for your help and inspiration. My new clock script should be in the Library shortly, as "Ceera's Westminster Clock". My version makes a three-prim clock that auto-regulates to SL time within less than one minute of being rezzed, and chimes the hour with the Westminster clock chimes. The chimes can be silenced by commenting out two lines of code. It has no open listens at all, and should be very low lag.

An example of the clock itself can be found in the NE corner of the "HHP at UH" sim.
_____________________
Sorry, LL won't let me tell you where I sell my textures and where I offer my services as a sim builder. Ask me in-world.