Graphics Programs Reference
In-Depth Information
99
100
101
102
103
104
// check keys to adjust viewer movement
if (Key.isDown(Key.UP)) {viewer.z += viewerSpeed;}
else if (Key.isDown(Key.DOWN)) {viewer.z -= viewerSpeed;}
if (Key.isDown(Key.RIGHT)) {viewer.ry += viewerAngle;}
else if (Key.isDown(Key.LEFT)) {viewer.ry -= viewerAngle;}
For each change in the viewer angle of rotation, we will have to get new values of the
sin and cos of that angle, so we need to add a call to the calcSinCosY() function
before looping over the objects to display them. Insert these lines after checking the
arrow keys.
110
111
112
113
114
105
// get the sin and cos of the rotation angle
calcSinCosY();
// loop over the objects and display them
for (i=1; i<=numberOfObjects; i++)
{
In order to see the effect of the rotation of the viewer on the scene, we have to make
an addition in the displayObj() function. We first calculate the relative translation of
each object and apply the rotation equations to transform into new coordinates. Enter
the lines shown after the calculation of the relative translation and before the calcula-
tion of the perspective ratio.
79
80
81
82
83
84
85
86
// calculate new tx and tz coordinates after rotation --
// find the new tz first but don't use it
// when calculating tx or you get the wrong value
var newtx:Number = tx * viewer.cy - tz *viewer.sy;
var newtz:Number = tz * viewer.cy + tx *viewer.sy;
tx = newtx;
tz = newtz;
Save and test your movie. You should be able to rotate the viewer's position as well as
zoom in and out on the objects as in Figure 7.27. By pressing down on either the Left
Arrow or Right Arrow keys sufficiently long, you can do a complete 360-degree rotation
around the objects.
 
Search WWH ::




Custom Search