# # #return : [Script] #
 ex-script-balls.wrl 

# Written By Alberto B. Raposo and Leo Pini Magalhaes
# Computer Graphics Lab - CS Univ. of Waterloo
# Last Modified 11-Oct.-96

# This is an animation in which the trajectory of a sphere is 
# determined by pointing the mouse into a sensor-cube. 
# If we point the red one, the sphere will pass at left; if we 
# point the green, the sphere will pass at right.
# The trajectories are defined by interpolation and the "behavior" 
# (i.e.,  which trajectory to follow) is defined by a java script.

# the java routine is Java Source

#VRML V2.0 utf8 
# Camera
Viewpoint {
   position    0 5 18
   orientation 1 0 0 -0.24
   fieldOfView 0.785398
}

# Illumination
SpotLight {
	   location 0 10 0
	   direction 0 -1 0
           radius 100		 
	   }

# The moving object (yellow sphere)
DEF MOV_OBJ Transform {
	translation -8 0 0
	children [
		Shape {
			geometry Sphere { radius 0.7 }
			appearance Appearance {
				   material Material {
				      	diffuseColor 1 1 0
				        	      }
				               }
		      }	
		]
			}

# The sensors
Transform {
	translation 6.25 0 0
	children [
		DEF TOUCH1 TouchSensor {}
		Shape {
		    geometry Box { size .5 1 1 }
		    appearance Appearance {
			material Material {
		         	   diffuseColor 1 0 0
		         	           }
		        	           }
	                          }
		 ]
	}
Transform {
	translation 6.75 0 0
	children [
		DEF TOUCH2 TouchSensor {}
		Shape {
		    geometry Box { size .5 1 1 }
		    appearance Appearance {
			material Material {
		        	   diffuseColor 0 1 0
		                 	   }
		                	  }
	        	}
		 ]
	}

# Cubes that don't move 
Transform {
	translation -5 0 0 
	children [
		DEF CUBE_CHILDREN Shape {
			geometry DEF CUBE_LARGE Box { size 1 1 1}
			appearance Appearance {
				    material Material {
				         diffuseColor 1 0 1
				        	       }
				              }
		      }	
		]	
	  }
Transform {
	translation 0 5 0 
	children [ USE CUBE_CHILDREN ]
	}	
Transform {
	translation 0 -4.5 0 
	children [ USE CUBE_CHILDREN ]
	}	  	

# Timer
DEF TIMER TimeSensor {
	loop TRUE
	cycleInterval 7
	     	    }

# Trajectories for the sphere
DEF INTERP1 PositionInterpolator {
	key [0, 0.0833, 0.167, 0.25, 0.333, 0.417, 0.5, 0.583, 
             0.667, 0.75, 0.833, 0.917, 1]
	keyValue [-8 0 0, -7 -4 0, -4 -6 0, 
	   	   0 -7 0, 2.75 -5 0, 4 -3 0, 
	  	   5 0 0, 4 3 0, 2.75 5 0,
	   	   0 7 0,  -4 6 0, -7 4 0, -8 0 0]
			  }

DEF INTERP2 PositionInterpolator {
	key [0, 0.0833, 0.167, 0.25, 0.333, 0.417, 0.5, 0.583, 
             0.667, 0.75, 0.833, 0.917, 1]
	keyValue [-8 0 0, -7 -4 0, -4 -6 0, 
	   	   0 -7 0, 4 -6 0, 7 -4 0, 
	   	   8 0 0, 7 4 0, 4 6 0,
	   	   0 7 0,  -4 6 0, -7 4 0, -8 0 0]
			  	}

# The script, responsible for the "behavior" of the sphere
DEF S Script {	
		eventIn SFFloat start 
		field SFNode inside USE TOUCH1
		field SFNode outside USE TOUCH2
		eventOut SFVec3f newPosition
		field SFNode PosInterp1 USE INTERP1
		field SFNode PosInterp2 USE INTERP2

#url "file:/vfs/lab1/i/users/lpini/vrml/book/run-examples/exbehav.class"
#url "http://www.cgl.uwaterloo.ca/~lpini/leovrml/run-examples/exbehav.class"
url "exballs.class"     }


# Routing events
ROUTE TIMER.fraction_changed TO INTERP1.set_fraction
ROUTE TIMER.fraction_changed TO INTERP2.set_fraction

ROUTE TIMER.fraction_changed TO S.start

ROUTE S.newPosition TO MOV_OBJ.translation

#