Skip to content
Lindsay Kay edited this page Sep 17, 2013 · 5 revisions

The quaternion node is a node subtype that provides a convenient way to define a 3D rotation that can be rotated continually on any axis without gimbal lock or significant numeric instability.

Below is a quaternion that defines a rotation for the nodes in its subgraph. Since these are used for rotation in SceneJS, we'll think of a quaternion in the same terms as the parameters of a rotate node. This example's x, y, z and angle parameters define the base rotation that this quaternion starts off at, in this case none, then the optional rotations array defines a sequence of rotations to apply on top of that. Finally, we apply one more rotation to the Quaternion node instance through its rotate method.

myScene.addNode({
         type: "quaternion",
         id: "myQuaternion",

         // Base rotation
          x : 0.0, y : 0.0, z : 0.0, angle : 0.0,      // No rotation

          // Sequence of rotations to apply on top of the base rotation
          rotations: [
                  { x : 0, y : 0, z : 1, angle : 45 }, // Rotate 45 degrees about Z the axis
                  { x : 1, y : 0, z : 0, angle : 20 }, // Rotate 20 degrees about X the axis
                  { x : 0, y : 1, z : 0, angle : 90 }, // Rotate 90 degrees about Y the axis
               ]
           },

           nodes: [
                // ..
           ]
 });

Then to rotate our quaternion one more time, 15 degrees about the Z axis (assuming our scene has ID "my-scene"):

myScene.getNode("myQuaternion",
        function(q) {
            q.add("rotation", { x : 0, y : 0, z : 1, angle : 15 });
        });
Clone this wiki locally