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-x-ball-6.vr

web3d-x-ball-6.vr (wrl ) [ flux / bitmanagement / cortona / octaga ]

Web3D/VR

bouncing ball (6)

object


  
        
  PROTO object [
  exposedField SFFloat radius 1
  exposedField SFVec3f translation 0 0 0
  exposedField SFVec3f scale 1 1 1
  exposedField SFRotation rotation 0 0 0 0
  exposedField SFColor color 1 0 0
  ]{
  Transform {
  translation IS translation
  scale IS scale
  rotation IS rotation
  children [ 
  Shape  {
          geometry Box { size 1 1 1 } ##  Sphere { radius 1 }
          appearance Appearance {
                  material Material {
                          diffuseColor IS color
                          }
                  }
  }
  ] }
  }
  
  

bounce


  
  PROTO bounce [
  exposedField SFFloat accel 0.1
  exposedField SFNode object NULL
  ]{
  DEF GROUP Transform {
  translation 0 0 0
  children [
  Transform {
  translation 0 -10 0
  children [
  Shape {
           geometry Box { size 5 0.2 5 }
           appearance Appearance {
                  material DEF GROUND Material {
                          diffuseColor 1  0 0
                          }
                  }
  }
  ]}
  ]}
  
  

sensor


  DEF TIME TimeSensor { loop TRUE cycleInterval 5 } #<l++>
  
  

script


  DEF SCRIPT Script {   
          directOutput TRUE
  
          field SFFloat accel IS accel
  
          field  SFTime  tick   0
  
          field SFNode group USE GROUP
  
          field SFNode object IS object
          field SFNode ground USE GROUND
          field SFFloat position 0
          field SFFloat color 0.5
          field SFFloat rotation 0
  
          field SFInt32 sign 1
          field SFFloat speed 1
  
          eventIn SFFloat set_fraction    
          url "javascript:
  
  

initialize


          function initialize() {
          group.addChildren = new MFNode(object);
          }
  

fraction


          
          function set_fraction(value) { 
          if (tick == 0) {
                    tick = value;
                    position = 0;
                    color = 0.5;
                }
                while (tick < value) {
                    update(value);
                    tick = tick + 0.01;
                }
          tick = value;
          }
  
  

update


  
          function update(value) {
                  check(value);
  
                  position += 0.01 * sign * speed;
                  rotation += 0.1;
                  color = value;
  
                  speed = speed * ( 1  - sign * accel); 
  
                  object.translation.y = position;
                  object.color = new SFColor(1,color,color);
                  object.rotation = new SFRotation(0.5,0.5,0,rotation);
                  }
  
  

check


  
          function check(value) {
                  if (speed < 0.1 ) sign = -1; 
                  if ((position - object.radius) < -10 ) {
                           effect(value);
                           sign = 1;
                           } 
                  }
  
  

effect


          function effect(value) {
                  ground.diffuseColor = new SFColor(1,color,color);
                  }
                  "
          }#<l++>
  

routing


  ROUTE TIME.fraction_changed TO SCRIPT.set_fraction
  }
  
  

prepare objects


  
  Switch {
  whichChoice -1
  choice [
  DEF OBJECT1 object { }
  DEF OBJECT2 object { }
  DEF OBJECT3 object { }
  DEF OBJECT4 object { }
  DEF OBJECT5 object { }
  ] }
  
  

bounce instances


  Transform {
  translation -12 0 0
  children [
  bounce { accel 0.1 object USE OBJECT1  }
  ] }
  
  Transform {
  translation 0 0 0
  children [
  bounce { accel 0.2 object USE OBJECT2  }
  ] }
  
  Transform {
  translation 12 0 0
  children [
  bounce { accel 0.3 object USE OBJECT3  }
  ] }
  
  Transform {
  translation -6 0 -10
  children [
  bounce { accel 0.4 object USE OBJECT4  }
  ] }
  
  Transform {
  translation 6 0 -10
  children [
  bounce { accel 0.5 object USE OBJECT5  }
  ] }
  
  

environment


  
  DirectionalLight { direction         0 -1 -1  }
  Viewpoint { position 0 0 30 }
  Background { skyColor 0.2 0.2 0.4 }
  
  


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