Modifying rotation

Away3D includes over a dozen functions and properties that can be used to rotate a 3D object.

The rotation init object parameters

The initial rotation of a 3D object can quite often be specified using the rotationX, rotationY, and rotationZ init object parameters. These values represent the rotation of a 3D object around the X, Y, and Z parent space axes, and are measured in degrees.

Tip

Away3D functions that accept an angle will usually use degrees. Flash functions like Math.sin, Math.cos, and Math.tan all work with radians. Be mindful of how angles are measured when using different functions.

Note

You can convert radians to degrees using the formula:

degrees = radians * 180 / pi

And degrees into radians using the formula:

radians = degrees / 180 * pi

var sphere:Sphere = new Sphere(
  {
    rotationX: 10,
    rotationY: 20,
    rotationZ: 30
  }
);

The rotation properties

The rotation of a 3D object can also be specified by setting the rotationX, rotationY, and rotationZ properties of the object itself once it is created.

var sphere:Sphere = new Sphere();
sphere.rotationX = 10;
sphere.rotationY = 20;
sphere.rotationZ = 30;

The rotateTo() function

The rotateTo() function can be used to set the rotation around the parent X, Y, and Z axes with one function call. The following example code achieves the same end result as setting the rotationX, rotationY, and rotationZ properties individually as described previously.

var sphere:Sphere = new Sphere();
sphere.rotateTo(10, 20, 30);

The eulers property

The eulers property is much the same as the rotateTo() function, except that it takes a single Vector3D object instead of three individual Numbers.

var sphere:Sphere = new Sphere();
sphere.eulers = new Vector3D(10, 20, 30);

The rotate() function

The preceding rotation functions all work by rotating a 3D object around the parent X, Y, and Z axes. By combining rotations around these fixed axes you can rotate a 3D object to any desired position. However, in some situations it is easier and quicker to rotate a 3D object around a single arbitrary axis. The rotate() function can be used to do just this.

The following code will rotate the sphere 90 degrees around the local space vector (1, 0, 1).

var sphere:Sphere = new Sphere();
sphere.rotate(new Vector3D(1, 0, 1), 90);

The lookAt() function

The lookAt() function can be used to point the local Z axis of a 3D object towards a position in parent space, defined by the first parameter called target, while optionally defining the roll of the 3D object once it has been rotated to face the desired position with the second parameter.

This second parameter is called upAxis. This vector, in parent space, is used in conjunction with the local Z axis of the 3D object once it has been rotated to face the target position to define a plane. The 3D object is then oriented so its local Y axis lies on this plane, while also being at right angles to the local Z axis.

If the upAxis parameter is not supplied, a default value of Vector3D(0, -1, 0) will be used.

The lookAt() function

The following code will rotate the camera so that it is looking at a position (10, 20, 30) units from the origin of the camera's parent container.

camera.lookAt(new Vector3D(10, 20, 30));

The pivotPoint property

By default, the rotation functions and properties detailed so far will rotate a 3D object around the axes of its parent container, or a vector in parent space, as if the 3D object were sitting at the origin of that parent container. The cube in the following image, seen from above, has been rotated 45 degrees around the Y axis.

The pivotPoint property

It is also possible to rotate a 3D object around an external point, known as the pivot point, much like the pendulum in an old grandfather clock. The position of the pivot point can be defined by assigning a Vector3D object to the pivotPoint property. The position of the pivot point is defined in local space. Here we have set the pivot point 200 units to the right of the cube.

  var cube:Cube = new Cube();
  cube.pivotPoint = new Vector3D(200, 0, 0);
  cube.rotationY = 45;
  scene.addChild(cube);

Now instead of rotating "in place" when the cube is rotated 45 degrees around the Y axis, it will rotate around the new pivot point to the right.

The pivotPoint property

The movePivot() function

The movePivot() function can also be used to set the position of the pivot point. The difference between the pivotPoint property and the movePivot() function is that you do not have to create an intermediary Vector3D object when using the movePivot() function.

The following code has the same effect as setting the pivotPoint property at a position of (200, 0, 0).

  var cube:Cube = new Cube();
  cube.movePivot(200, 0, 0);
  cube.rotationY = 45;
  scene.addChild(cube);

The scenePivotPoint property

The scenePivotPoint property provides a way to find the position of the pivot point in world space. The scenePivotPoint property is read-only, so you can not assign a new position for the pivot point through it.

  var cube:Cube = new Cube();
  cube.movePivot(200, 0, 0);
  var scenePivot:Vector3D = cube.scenePivotPoint;

The pitch(), roll(), and yaw() functions

The pitch(), yaw(), and roll() functions rotate the 3D object around the local X, Y, and Z axes respectively. These concepts are easy to visualize if you apply them to a camera. Modifying the pitch will make the camera look left and right. Modifying the yaw will make the camera look up and down. Modifying the roll would be like turning the camera upside down.

The pitch(), roll(), and yaw() functions
var sphere:Sphere = new Sphere();
sphere.pitch(90);
sphere.yaw(90);
sphere.roll(180);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.189.63