Collision in VRML

Introduction

There is a need in some VRML worlds to allow the user to move through objects. For example allow movement through water instead on walking on the surface. For this we use the Collision node.

Examples and code

Example 1

Collision {
 collide FALSE
 # Collide with this object TRUE / FALSE
 children [
  # any number of Group or Transform or Shape nodes
  # which you wish to effect
  Shape {
   appearance Appearance {
    material Material {
     diffuseColor 1 0 0
    }
   }
   geometry Sphere {
    radius 2
   }
  }
 ]
}

The code is fully documented so notes should not be needed
Collision example 1

Example 2

This example starts an animation when the object is collided with. This could be simply modified to trigger a sound effect instead.

# detect collision with an object
DEF Detect Collision {
 collide TRUE
 children [
  Shape {
   appearance Appearance {
    material DEF Mat Material {
     diffuseColor 0.6 0.6 1
     transparency 0
    }
   }
   # Just your basic 1x1x1 meter box but using
   # an IndexedFaceSet and double sided
   geometry IndexedFaceSet {
    solid FALSE
    coord Coordinate {
     point [
      1 1 1, 1 1 -1, 1 -1 1, 1 -1 -1,
      -1 1 1, -1 1 -1, -1 -1 1, -1 -1 -1
     ]
    }
    coordIndex [
     4 0 1 5 -1
     7 3 2 6 -1
     6 2 0 4 -1
     2 3 1 0 -1
     3 7 5 1 -1
     7 6 4 5 -1
    ]
   }
  }
 ]
}
DEF Timer TimeSensor {
 cycleInterval 10
 startTime -1
}
ROUTE Detect.collideTime TO Timer.startTime
ROUTE Timer.fraction_changed TO Mat.transparency

The code is fully documented so notes should not be needed
Collision example 2