Revolver

tested with ++


The idea for this tutorial came from a feature of Paragraph's 3D space builder: when you leave the mouse pointer over one of the primitives it will start turning. That's all we'll accomplish today.
I start with a standard cube with a TouchSensor and a little script:

DEF revolver Script {

  eventIn SFBool startRevolving

  eventOut SFRotation revolve

  field SFFloat angle 0.5



  url "javascript:

    function startRevolving() {

      revolve[0] = 1;

      revolve[1] = 1;

      revolve[2] = 1;

      revolve[3] = angle;

      angle += 0.1;

    }

  "

}

ROUTE TouchS.isOver TO revolver.startRevolving

ROUTE revolver.revolve TO cube.set_rotation
Here I take the TouchSensor's eventOut 'isOver' and ROUTE it to the Script's eventIn 'startRevolving'. The function 'startRevolving' assigns to the array 'revolve' values and increments the value of the angle. 'Angle' is a field of type SFFloat since the rotation field of the Transform 'cube' needs float values. The resulting SFFloat array 'revolve' is now ROUTEd to the rotation field of 'cube'.
Test the first version of revolver.wrl and you will see that the cube will turn as long as you keep moving the mouse pointer (revolver source). This is not what I wanted and a brief look at the VRML2.0 specification reveals that the isOver event is only generated when "the pointing device has changed ...". That means that there is only a TRUE generated when you move the mouse pointer over the TouchSensor and a FALSE when you leave the TouchSensor. Ok, back to the file.
One possibility to drive the animation of the cube is to utilize a TimeSensor. A TimeSensor generates an eventOut 'cycleTime'. Whenever the cycleInterval is elapsed this eventOut is sent and can be used to trigger other events.

DEF ticker TimeSensor {

  cleInterval 0.1

  loop TRUE

  enabled FALSE

}
The TimeSensor is initially disabled since we use it as the 'engine' of our animation and we need it running only when the mouse pointer is over the cube. Here every 0.1 seconds the TimeSensor will send the current time to the Script function 'startRevolving':


ROUTE TouchS.isOver TO ticker.set_enabled

ROUTE ticker.cycleTime TO revolver.startRevolving

ROUTE revolver.revolve TO cube.set_rotation
The TouchSensor enables the TimeSensor which in turn starts ticking. Every time the function 'startRevolving' is called the eventOut 'revolve' is produced which is ROUTEd to the cube's rotation field.
A brief look at revolver.wrl will show you that the cube is now constantly spinning after you have moved the mouse pointer over it (revolver source).





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