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

x-ball-9.vr

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

bouncing ball (9)

environment


  
  PROTO state [
  eventIn SFNode effect
  exposedField MFColor color 0.2 0.2 0.4 
  ]{
  DirectionalLight { direction         0 -1 -1  }
  Viewpoint { position 0 0 30 }
  DEF BACK Background { skyColor IS color }
  Script {
  directOutput TRUE
  eventIn SFNode effect IS effect
  field SFNode back USE BACK
  url "javascript:
  

effect (callback)


  function effect(node) {
          back.skyColor = new MFColor(
            new SFColor(0.2, node.color, node.color));
          }
          "
  }
  }
  
  DEF STATE state {}
  
  

object


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

bounce


  
  PROTO bounce [
  exposedField SFFloat accel 0.1
  exposedField SFNode object NULL
  exposedField SFNode state NULL
  exposedField SFInt32 id 0
  
  exposedField SFVec3f translation 0 0 0
  exposedField SFVec3f scale 1 1 1
  exposedField SFRotation rotation 0 0 0 0
  ]{
  DEF GROUP Transform {
  translation IS translation
  scale IS scale
  rotation IS rotation
  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 SFNode self USE SCRIPT
  
          field SFFloat accel IS accel
  
          field  SFTime  tick   0
  
          field SFNode group USE GROUP
  
          field SFNode state IS state
          field SFInt32 id IS id
  
          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.02;
                  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);
                  if (state) state.effect = self;
                  }
                  "
          }#<l++>
  

routing


  ROUTE TIME.fraction_changed TO SCRIPT.set_fraction
  }
  
  

element


  
  PROTO element [
  exposedField SFFloat radius 1
  exposedField SFColor color 1 0 0
  exposedField SFFloat speed 0.1
  exposedField SFFloat accel 0.1
  exposedField SFVec3f translation 0 0 0
  exposedField SFVec3f scale 0.2 0.2 0.2
  exposedField SFRotation rotation 0 0 0 0
  ]{
  DEF GROUP Transform {
  translation IS translation
  scale IS scale
  rotation IS rotation
  children [
  Shape {
           geometry Sphere { radius 10 }
           appearance Appearance {
                  material DEF MATERIAL Material {
                          diffuseColor IS color
                          transparency 0.8
                          }
                  }
  }
  Switch {
  whichChoice -1
  choice [
  DEF OBJECT object { }
  ] }
  bounce { translation 0 0 0 scale 0.5 0.5 0.5  accel IS speed object USE OBJECT  }
  ]}
  }
  
  

system


  
  PROTO system [
  exposedField SFVec3f translation 0 0 0
  exposedField SFVec3f scale 1 1 1
  exposedField SFRotation rotation 0 0 0 0
  ]{
  DEF GROUP Transform {
  translation IS translation
  scale IS scale
  rotation IS rotation
  children [
  Switch {
  whichChoice -1
  choice [
  DEF OBJECT1 element { accel 0.1 speed 0.5 }
  DEF OBJECT2 element { accel 0.2 speed 0.6 }
  DEF OBJECT3 element { accel 0.3 speed 0.7 }
  DEF OBJECT4 element { accel 0.4 speed 0.8 }
  DEF OBJECT5 element { accel 0.5 speed 0.9 }
  ] }
  
  bounce {translation -12 0 0 accel 0.1 object USE OBJECT1  }
  bounce {translation 0 0 0 accel 0.2 object USE OBJECT2  }
  bounce {translation 12 0 0 accel 0.3 object USE OBJECT3  }
  bounce {translation -6 0 -10 accel 0.4 object USE OBJECT4  }
  bounce {translation 6 0 -10 accel 0.5 object USE OBJECT5  }
  ]}
  }
  
  

scheme


  
  PROTO scheme [
  exposedField SFFloat radius 1
  exposedField SFColor color 1 0 0
  exposedField SFFloat speed 0.1
  exposedField SFFloat accel 0.1
  exposedField SFVec3f translation 0 0 0
  exposedField SFVec3f scale 0.5 0.5 0.5
  exposedField SFRotation rotation 0 0 0 0
  ]{
  DEF GROUP Transform {
  translation IS translation
  scale IS scale
  rotation IS rotation
  children [
  Shape {
           geometry Sphere { radius 10 }
           appearance Appearance {
                  material DEF MATERIAL Material {
                          diffuseColor IS color
                          transparency 0.9
                          }
                  }
  }
  system { translation -2 0 0 rotation 0 0 1 4.71 scale 0.5 0.5 0.5 }
  system { translation 2 0 0 rotation 0 0 1 1.57 scale 0.5 0.5 0.5 }
  system { translation 0 0 2 rotation 1 0 0 1.57 scale 0.5 0.5 0.5 }
  system { translation 0 0 -2 rotation 1 0 0 4.71 scale 0.5 0.5 0.5 }
  ]}
  }
  
  Switch {
  whichChoice -1
  choice [
  DEF SCHEME scheme { }
  ]}
  
  

instance


  
  bounce {
          state USE STATE 
          scale 2 2 2
          accel 0.1
  	object USE SCHEME  }
  
  


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