Datasheet

Chapter 1: Understanding Flash3D
21
But in CS4 there ’ s a problem with this. When using the sorting algorithm (above) you sort the elements
on the z - axis internal to the container. But when the container is rotated the objects rotate with it and are
no longer sorted correctly. Essentially, there are two z values: z inside the container and z outside the
container. To sort these correctly, you must sort with respect to the z axis outside your container. You
must transform the local coordinates inside your container to the stage coordinates (or z outside your
container).
Keith Peters in his advanced book on AS3 Animation proposed the following solution for the
container - sorting problem.
//Sorting
function sortParticles():void
{
particles_ary.sort(particleZSort);
for(var i:int = 0; i < particles_ary.length; i++)
{
myDisplayObject.addChildAt(particles_ary[i] as Sprite, i);
}
}
//
function particleZSort(particle1:DisplayObject,particle2:DisplayObject):int
{
var zpos1:Vector3D = particle1.transform.matrix3D.position;
zpos1 = myDisplayObject.transform.matrix3D.deltaTransformVector(zpos1);
var zpos2:Vector3D = particle2.transform.matrix3D.position;
zpos2 = myDisplayObject.transform.matrix3D.deltaTransformVector(zpos2);
return zpos2.z - zpos1.z;
}
The particles_ary.sort method passes in the particleZSort function as a parameter. The
particleZSort function is called multiple times with pairs of particles during a sort, returning a
negative number if the first particle should be placed in front of the second. The
Matrix3D and
Vector3D methods are used to keep track of your objects ’ rotated positions, which we discuss in greater
detail later in Chapter 3.
This method is computationally intensive, but works surprisingly well (as you can see from the Carousel
and Image Ball examples next).
Rotation
You ’ ve already met basic sinusoidal functions, such as sine and cosine, in this chapter: it ’ s these
functions that give you the ability to rotate elements in space. Even though rotation is inherent in
Papervision3D (and Flash 10), you still use these functions occasionally when manipulating elements
in 3D space. For example, in the gaming section of this book, you use these equations to rotate a
Rubik ’ s cube.
c01.indd 21c01.indd 21 12/14/09 3:03:30 PM12/14/09 3:03:30 PM