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

web3d-vr-race.vr

web3d-vr-race.vr (wrl ) [ flux / bitmanagement / cortona / octaga ]

Web3D/VR

scene


  
  NavigationInfo {
      type ["NONE"]
  }
  
  DEF DEFAULT_VIEWPOINT Viewpoint {
      position 0 40 0
      orientation 1 0 0 -1.57
  }
  
  
  Shape {
      geometry Box { size 40 0.1 40 }
      appearance Appearance {
          texture ImageTexture { url "local/game/images/grid.gif" }
      }
  }
  
  

object :


  
  DEF OBJECT Transform {
      children Transform {
           rotation 1 0 0 1.57
           children [
               Shape {
                   geometry Cone {}
                   appearance Appearance {
                       material Material {
                           emissiveColor 1 0 0
                       }
                   }
               }
          ]
      }
  }
          
  
  

script


  
  DEF SCRIPT Script {
      directOutput TRUE
  
      field    SFNode  me        USE SCRIPT
  
      field    SFTime  oldTick   0
      eventIn  SFTime  tick
  
      field    SFInt32 oldMask   0
      eventIn  SFNode  onEvent
  
      eventOut SFVec3f    position_changed
      eventOut SFRotation orientation_changed
      eventOut SFInt32    hide_text
  
      field SFFloat    speed       0 
      field SFVec3f    position    0 0 0
      field SFVec3f    direction   0 0 1
      field SFRotation orientation 0 0 1 0
  
      field SFRotation rotA         0 0 1 0
      field SFRotation rotB         0 0 1 0
      field SFVec3f    defDirection 0 0 1
      field SFVec3f    yAxis        0 1 0
      field SFVec3f    up           0 1 0
  
  

keys


  
      field SFBool upk   FALSE
      field SFBool down  FALSE
      field SFBool left  FALSE
      field SFBool right FALSE
      field SFBool space FALSE
  
      field SFInt32 k_upk   1004 ##38
      field SFInt32 k_down  1005 ##40
      field SFInt32 k_left  1006 ##37
      field SFInt32 k_right 1007 ##39
      field SFInt32 k_space 32
  
  

initialize


  
      url "javascript:
          function initialize() {
              connectKeyboard();
          }
          function shutdown() {
              disconnectKeyboard();
          }
  
          function connectKeyboard() {
              // tell what events
              oldMask = Browser.eventMask;
              Browser.eventMask = ((1<<5) | (1<<6)); // KeyUp&Down Only...
              // add event observer
              Browser.addRoute(Browser,'event_changed',me,'onEvent');
          }
          function disconnectKeyboard()
          {
              // remove event observer
              Browser.deleteRoute(Browser,'event_changed',me,'onEvent');
              Browser.eventMask = oldMask;
          }
  
  

onEvent


  
          function onEvent(e,t) {
              if (e.type == 'keydown') {
                  if(e.keyCode == k_left) {
                      left = TRUE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_upk) {
                      upk = TRUE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_right) {
                      right = TRUE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_down) {
                      down = TRUE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_space) {
                      space = TRUE;
                     e.returnValue = false;
                  }
              } else if (e.type == 'keyup') {
                  if(e.keyCode == k_left) {
                      left = FALSE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_upk) {
                      upk = FALSE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_right) {
                      right = FALSE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_down) {
                      down = FALSE;
                     e.returnValue = false;
                  } else if(e.keyCode == k_space) {
                      space = FALSE;
                     e.returnValue = false;
                  }
              } else {
                  Browser.print('Unknown event : '+e.type);
              }
          }
  
  

tick & update


  
          function tick(v,t) {
              if(oldTick == 0) {
                  oldTick = v;
              }
              while (oldTick < v) {
                  update();
                  oldTick = oldTick + 0.05;
              }
          }
          function update() {
              // Y-as wordt eigenlijkniet gebruikt, beweging
              // i beperkt tot x/z vlak,
              // maar voor de volledigheid wordt het wel
              // allemaal netjes berekend...
  
              if(space) {
                  hide_text = -1;
              }
  
              // Up/Down = gas/rem
              if(upk) {
                  speed = speed + 0.01;
              } 
              if(down) {
                  speed = speed - 0.01;
              } 
  
              // Left/Right = sturen
              // Eigenlijk zou snelheid sturen moeten beinvloeden
              if(left) {
                  direction = (new SFRotation(0,1,0, 0.1)).multVec(direction);
              }
              if(right) {
                  direction = (new SFRotation(0,1,0,-0.1)).multVec(direction);
              }
  
              // verplaats object
              position[0]      = position[0] + direction[0]*speed;
              position[1]      = position[1] + direction[1]*speed;
              position[2]      = position[2] + direction[2]*speed;
              position_changed = position;
  
              // output de juiste updates
              rotA  = new SFRotation(defDirection,direction);
              up    = rotA.multVec(yAxis);
              rotB  = new SFRotation(direction.cross(up),
                                     direction.cross(yAxis));
              orientation         = rotA.multiply(rotB);
              orientation_changed = orientation;
          }
     "
  }
  
  

sensor and routing


  
  DEF TIMER TimeSensor {
      enabled TRUE
      loop TRUE
  }
  
  ROUTE TIMER.time_changed         TO SCRIPT.tick
  ROUTE SCRIPT.position_changed    TO OBJECT.translation
  ROUTE SCRIPT.orientation_changed TO OBJECT.rotation
  
  

a little manual...


  
  EXTERNPROTO STText [
      exposedField MFString string
  ] [ "@vr-example-text-proto.wrl" ]
  
  DEF HIDE_TEXT Switch {
      whichChoice 0
      choice [
          Group {
              children [
          Transform {
              translation 0 1.8 0
              children [
                  Shape {
                      appearance Appearance {
                          material Material {
                              diffuseColor  0 0 0
                              emissiveColor 0 0 0
                              specularColor 0 0 0
                              transparency  0.4
                          }
                      }
                      geometry Box { size 27 0.1 20 }
                  }
              ]
          }
          
          Transform {
              translation -13.5 2 -9
              rotation 1 0 0 -1.57
              children [
                  STText { 
                      string [
                       "  BOGUS LITTLE GAME  ",
                       "   (RACING MOTION)   ",
                       "",
                       "CONTROLS",
                       "---------------------",
                       "UP        :ACCELERATE",
                       "DOWN      :BRAKE     ",
                       "LEFT/RIGHT:TURN",
                       "---------------------",
                       "",
                       "SHOWS",
                       "---------------------",
                       "BLAXXUN KEYBOARD     ",
                       "SCRIPT BASED MOTION  ",
                       "GAME LOOP            ",
                       "BLAXXUN TEXTURE-TEXT ",
                       "---------------------",
                       "",
                       "PRESS SPACE TO START!"
                      ] 
                  }
              ]
          }
              ]
          }
      ]
  }
  
  ROUTE SCRIPT.hide_text TO HIDE_TEXT.whichChoice
  
  


(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.