TouchSensor Node

The TouchSensor node is a way of providing interactivity with the user. This sensor is usually defined in a group and affects all shapes within that group. The sensor reacts when the user has the mouse over and when the user clicks a shape contained in the group.

This node has a single field which specifies if the sensor is enabled or not.

The isOver event is generated with the value TRUE by this node when the field is enabled and the mouse moves from a position where it is not over a shape contained within the group to a position where it is over a shape. A value FALSE is provided by this event when the sensor is enabled and the mouse stops being over a shape within the group.

If the shapes are not visible, i.e. they are hidden by other surfaces, then the touch sensor should not generate the isOver event.

When the mouse is over a shape within the same group as a TouchSensor, then the following events are generated when the mouse moves:
  • hitPoint_changed: Provides the 3D position on the surface of the shape in the TouchSensor's group coordinate system.
  • hitNormal_changed and hitTextCoord_changed: provide respectively the surface normal vector and the texture coordinates of the surface at the hitPoint.


  • When the user presses the mouse button the TouchSensor will also generate the event isActive with the value TRUE when the user clicks the mouse, if over a shape and enabled. When the user releases the mouse button an isActive event is generated with the value FALSE plus the event touchTime with the current time.

    Syntax:
    TouchSensor { 
      enabled TRUE 
    }
    When defining several TouchSensor inside nested groups, only the lowest sensor will generate events. For instance consider the following excerpt of code: 
    DEF ga Group {
    }


    When the user clicks over Shape sa, then TouchSensor ta will generate events, when the user clicks on Shape sb only the TouchSensor tb will generate events.




    Example of a TouchSensor to play a sound:

    The following source code describes a group with a Shape, a TouchSensor, and a Sound.
    #VRML V2.0 utf8 
    Group { 
    }


    To play the sound when the user clicks the shape the following route should be used:
    ROUTE ts.touchTime TO ac.set_startTime


    The sound will play once because the loop field of the AudioClip is set to FALSE.

    Another possibility is to have the sound playing whenever the mouse is over the shape. This can be achieved by the following route:
    ROUTE ts.isOver TO ac.set_loop


    This will cause loop to become TRUE when the TouchSensor output an isOver event with the value TRUE. When the user is no longer over the shape the isOver event from the TouchSensor will output FALSE therefore stopping the sound as soon as the sound's duration/pitch or stopTime are achieved.