Bumper

tested with ++


This lesson deals about how to add and remove children of nodes. Transform nodes have events addChildren and removeChildren and you can add or remove nodes (surprise!) with them.
We start with a setup of three boxes:

DEF leftBox Transform {

  translation -5 0 0

  children [

    Shape {

      appearance Appearance {

        material Material { diffuseColor 1 0 0 }

      }

      geometry Box {}

    }

  ]

}

DEF rightBox Transform {

  translation 5 0 0

  children [

    Shape {

      appearance Appearance {

        material Material { diffuseColor 0 0 1 }

      }

      geometry Box {}

    }

  ]

}

DEF onoff Transform {

  translation 0 -5 0

  children [

    Shape {

      appearance Appearance {

        material Material { diffuseColor 0 1 0 }

      }

      geometry Box {}

    }

  ]

}
We add a child called 'SphereChild' to the 'leftBox' Transform:

DEF leftBox Transform {

  translation -5 0 0

  children [

    Shape {

      appearance Appearance {

        material Material { diffuseColor 1 0 0 }

      }

      geometry Box {}

    }

    DEF SphereChild Shape {

      appearance Appearance {

        material Material { diffuseColor 1 0 1 }

      }

      geometry Sphere { radius 1.2 }

    }

  ]

}
The child has to be DEFed (DEF SphereChild) in order to use it later in a script. The middle lower box gets a TouchSensor 'TS':

Transform {

  translation 0 -5 0

  children [

    Shape {

      appearance Appearance {

        material Material { diffuseColor 1 0 1 }

      }

      geometry Box {}

    }

    DEF TS TouchSensor {}

  ]

}
So far everything is straightforward and easy. Take a quick look at the bumper.wrl and the bumper source. Next we have to think about the script. To use the eventIns addChildren/removeChildren we need corresponding eventOuts which we will get from a Script node. The triggering eventIn of the Script node is simply the isActive eventOut of the TouchSensor. The Script's eventOut is called 'child' and has to be of type MFNode to be compatible with the eventIn/eventOuts addChildren/removeChildren:

DEF S Script {

  eventIn SFBool isActive

  eventOut MFNode child

  field MFNode testNode USE SphereChild



  url "javascript:

    function isActive(value) {

      if(value) child = testNode;

    }

  "

}
Once the TouchSensor has fired its eventOut 'isActive' to the Script node 'S' it will trigger the eventOut 'child' which the MFNode 'testNode' has been assigned to. This field uses the node 'SphereChild' to pass a reference to the eventOut 'child'. The ROUTEing now needs to go from the eventOut 'child' to the eventIn removeChildren of the Transform 'leftBox' and then to the eventIn addChildren of the Transform 'rightBox'.

ROUTE TS.isActive TO S.isActive

ROUTE S.child TO leftBox.removeChildren

ROUTE S.child TO rightBox.addChildren

When you click on the purple box you will see how the sphere will jump (bump) to the right box. ( bumper.wrl and the bumper source). You might say that you could get the same effect with a translation but when e.g. the 'parents' are moving you need to be able to add/remove children. You can also experiment with commenting out the 'removeChildren' ROUTE or an animated parent object as in bumper.wrl.




Copyright © 1996-98 Markus Roskothen. All rights reserved.