media @ VU
[] readme course preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthoughts appendix references examples resources _

talk show tell print

proto-clock.vr

proto-clock.vr (wrl ) [ flux / bitmanagement / cortona / octaga ]

Web3D/VR

clock proto

EXTERNPROTO MetaData [ exposedField MFString instructions exposedField MFString name exposedField MFString description exposedField MFString interface ] "@vr-web3d-vr-metadata-proto.wrl"

meta data


  
  MetaData {
     name ["ClockMechanism"]
     description [
        "simulates a clock motor--"
        "generates rotation values for clock hands."
     ]
     interface [
        "EXTERNPROTO ClockMechanism ["
        "   eventIn  SFInt32    set_hour"
        "   eventIn  SFInt32    set_minute"
        "   eventIn  SFInt32    set_second"
        "   field    SFBool     autoClock "
        "   field    SFInt32    hour"
        "   field    SFInt32    minute"
        "   field    SFInt32    second"
        "   field    SFVec3f    hourHandAxis"
        "   field    SFVec3f    minuteHandAxis"
        "   field    SFVec3f    secondHandAxis"
        "   field    SFBool     twentyFourHours"
        "   eventOut SFInt32    hour_changed"
        "   eventOut SFInt32    minute_changed"
        "   eventOut SFInt32    second_changed"
        "   eventOut SFRotation hourHand_changed"
        "   eventOut SFRotation minuteHand_changed"
        "   eventOut SFRotation secondHand_changed"
        "]\"@vr-web3d-vr-clock-proto.wrl\""
     ]
  }
  
  

Proto


  
  
  
  
  
  
  
  PROTO ClockMechanism [
          eventIn  SFInt32    set_hour
          eventIn  SFInt32    set_minute
          eventIn  SFInt32    set_second
          field    SFBool     autoClock       TRUE
          field    SFInt32    hour            12
          field    SFInt32    minute          0
          field    SFInt32    second          0
          field    SFVec3f    hourHandAxis    0 0 1
          field    SFVec3f    minuteHandAxis  0 0 1
          field    SFVec3f    secondHandAxis  0 0 1
          field    SFBool     twentyFourHours FALSE
          eventOut SFInt32    hour_changed
          eventOut SFInt32    minute_changed
          eventOut SFInt32    second_changed
          eventOut SFRotation hourHand_changed
          eventOut SFRotation minuteHand_changed
          eventOut SFRotation secondHand_changed
  ]
  {
     Group {
        children [
  
                DEF TS TimeSensor { loop TRUE }
           
                DEF SCRIPT Script {
                   eventIn  SFInt32    set_hour   IS set_hour
                   eventIn  SFInt32    set_minute IS set_minute
                   eventIn  SFInt32    set_second IS set_second
                   eventIn  SFTime     set_time
                   field    SFInt32    hr    IS hour
                   field    SFInt32    min   IS minute
                   field    SFInt32    sec   IS second
                   field    SFVec3f    hrAx  IS hourHandAxis
                   field    SFVec3f    minAx IS minuteHandAxis
                   field    SFVec3f    secAx IS secondHandAxis
                   field    SFBool     do24  IS twentyFourHours
                   field    SFBool     auto  IS autoClock
                   field    SFBool     first TRUE
                   eventOut SFRotation hourHand_changed   IS hourHand_changed
                   eventOut SFRotation minuteHand_changed IS minuteHand_changed
                   eventOut SFRotation secondHand_changed IS secondHand_changed
  
                   url["javascript:
  
                 // private:
                      function autoSetTime() {
                         currentDate = new Date(); 
                         hr  = currentDate.getHours();
                         min = currentDate.getMinutes();
                         sec = currentDate.getSeconds();
                      }
                      function time2Rot() {
                         // set some constants
                         PIPI = Math.PI * 2;
                         numHrs = 12;
                         if(do24) { numHrs = 24; }
                         // find radians (what portion of a full circle-- x * 2PI; x: 0 -> 1)
                         secRad = (sec / 60) * PIPI;
                         minRad = (min / 60) * PIPI;
                         hrRad  = (hr / numHrs) * PIPI;
                         // adjust minute and hour hands for smooth interpolation
                         minRad += secRad / 60;
                         hrRad  += minRad / numHrs;
                         // create the new rotations
                         secondHand_changed = new SFRotation(secAx, -secRad);
                         minuteHand_changed = new SFRotation(minAx, -minRad);
                         hourHand_changed   = new SFRotation(hrAx, -hrRad);
                      }
                      function initialize() { if(auto) { autoSetTime(); } }
  
  

public:


                      function set_time(tickTock) {
                         if(first || TRUE) {
                                 first = FALSE;
                                 // get time from computer, if autoClock is on
                                 if(auto) { autoSetTime(); }
                                 // or keep our own time
                                 else {
                                    sec ++;
                                    if(sec > 59) { sec = 0; min++; }
                                    if(min > 59) { min = 0; hr++;  }
                                    if(do24) { if(hr > 23) { hr = 0; } }
                                    else     { if(hr > 11) { hr = 0; } }
                                 }
                                 time2Rot();
                                 hour_changed = hr;
                                 minute_changed = min;
                                 second_changed = sec;
                         }
                         else { first = TRUE; }
                      }
                      function set_hour(h)   { hr  = h; }
                      function set_minute(m) { min = m; }
                      function set_second(s) { sec = s; }
                      "]
                }
             ]
     }
     ROUTE TS.cycleTime TO SCRIPT.set_time
  }
  
  


(C) A. Eliëns 21/5/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.