Modifying position

We have already seen how to set the position of a 3D object via the x, y, and z properties. Away3D includes a number of additional properties and functions that can also be used to move a 3D object within the scene.

The x, y, and z properties

The initial position of a 3D object can quite often be specified with the x, y, and z properties of the init object. This method has already been used in previous examples presented in the book. This method sets the position properties once when 3D object is created, rather than letting them be set to a default value and then later modifying them, making this the most efficient, and therefore preferred, way to define the initial position of a 3D object.

var sphere:Sphere = new Sphere(
  {
    x: 10,
    y: 20,
    z: 30
  }
);

Tip

While a number of Away3D classes accept an init object, as we saw with the Trident class in Chapter 2, Creating and Displaying Primitives, this is not always the case.

The position of a 3D object can also be specified by setting x, y, and z properties of the object itself once it is created.

var sphere:Sphere = new Sphere();
sphere.x = 10;
sphere.y = 20;
sphere.z = 30;

The position of a 3D object is always set relative to the position of its parent. This is true for the x, y and z properties, and the position property. It is possible to find the position of a 3D object within the scene (or the world position) by accessing the scenePosition property, however this is a read-only property.

The position property

The position property can be set with a Vector3D object, which specifies the position of the 3D object relative to its parent on the X, Y, and Z axes all at once.

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

The move functions

The moveForward(), moveBackward(), moveLeft(), moveRight(), moveUp(), and moveDown() functions can all be used to move a 3D object by a specified distance, relative to the 3D object's local axes. The following image shows how these directions relate to the local X, Y, and Z axes.

The move functions

The following code moves a primitive cube 3D object 10 units forward, 20 units to the right, and then 30 units down:

var cube:Cube = new Cube();
cube.moveForward(10);
cube.moveRight(20);
cube.moveDown(30);

The individual movements are shown in the following image:

The move functions

The moveTo() function

The moveTo() function works in much the same way as the position property in that it can be used to set the position of a 3D object relative to its parent along all three axes at once.

The call to the moveTo() function in the following example achieves the same result as assigning a Vector3D object with the values (20, -30, 10) to the position property.

var cube:Cube = new Cube();
cube.moveTo(20, -30, 10);

The translate() function

The translate() function is used to move a 3D object along an arbitrary vector in local space.

The first parameter, axis, defines the direction to move. The length of the vector is not used when calculating the distance to move (it is normalized, or modified so it has a length of one unit, by the translate() function). It is the second parameter, distance, which defines how far along the vector the 3D object will move.

The following example would move the cube to the same position as the other move functions shown previously. We know that the axis vector will result in the cube moving the same direction as it was moved in the other examples because we have constructed it using the same values for the X, Y, and Z axes (that is, 20, -30, and 10). We constructed it using these values for convenience; remember that, because it is normalized, it is only the direction that these vector points count. If we had specified a vector of (2, -3, 1) or (2/3, -1, 1/3), the end result would be exactly the same.

So we know that the axis vector only defines the direction in which to move, and not how far to move. Therefore, to move the cube to the same final position as the other move examples, we need to know the length of the vector (20, -30, 10). We can calculate this using Pythagoras' theorem, which states that the length of a vector can be calculated as the square root of the sum of the squared lengths of its component axes. From this we can calculate the length using the code Math.sqrt(20*20 + -30*-30 + 10*10) (or simply Math.sqrt(1400)).

var cube:Cube = new Cube ();
cube.translate(new Vector3D(20, -30, 10), Math.sqrt(1400));
..................Content has been hidden....................

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