Graphics Programs Reference
In-Depth Information
57
58
59
60
61
62
63
64
65
66
this.onEnterFrame = function()
{
// check the arrow keys to adjust the camera movement
if (Key.isDown(Key.UP)) {camdz = -camspeed; }
else if (Key.isDown(Key.DOWN)) {camdz = camspeed; }
else if (Key.isDown(Key.RIGHT)) {camdx = -camspeed; }
else if (Key.isDown(Key.LEFT)) {camdx = camspeed; }
else camdz = camdx = 0;
// loop over all the objects
Step 3: Update the objects
We are ready to take the camera's movement into account. To do that, we'll apply what
you know about the theory of relativity. What's that, you say? You know nothing about
the theory of relativity? Betcha do. Einstein used to think up thought experiments to
develop his theory. Suppose you and a friend stand five feet apart in empty space. Your
friend moves one foot closer to you. How far apart are the two of you now? If you say
four feet, you are halfway there.
Now suppose your friend moves back a foot so that you are both again five feet apart.
This time you move one foot closer to your friend. Once more, how far apart are you?
That's right, Albert, the two of you are again four feet apart. So the movement is rela-
tive. It doesn't matter which one you moves, the result is the same. Why do we have
to be in empty space? The answer is that we don't necessarily, but it just makes the
thought experiment sound more impressive.
The point of all this is that we can see the effects of moving the camera by moving
the objects in the opposite direction. Moving the camera to the right is like moving the
objects to the left and so forth. So let's apply this portentous concept to our example
with a few lines of code at the beginning of displayObj() . It's disgustingly simple. It
should be pointed out, however, that what we are doing works best when the back-
ground is fairly nebulous in form.
34
35
36
37
38
39
function displayObj(thisObj)
{
// update the 3D coordinates based on the camera movement
thisObj.x += camdx;
thisObj.z += camdz;
 
Search WWH ::




Custom Search