3D Game Programming

(C. Jardin) #1

❶We’ll use this 3D clock as a timer for our animation.


❷In addition to performing acrobatics, now we’ll also walk.


❸This is the function that moves the hands and feet.


As you might guess from the name, Math.sin() has something to do with math.
In fact, it’s a pretty amazing mathematical something called a sine that has
all sorts of uses. Here we’re making use of the fact that Math.sin() will generate
a number between –1 and 1 as time passes.

Multiplying 100 times Math.sin() means that position will be a number between –100
and 100. In JavaScript, the asterisk character (*) is used for multiplication.

We’ll talk more about how we work with math in A Closer Look at JavaScript
Fundamentals.

If you’ve typed in everything correctly, you should see the right hand of the
avatar jiggling back:

and forth:


And it should be moving pretty quickly.


Try This Yourself
Experiment with the number inside animate(). If you change the 10
to 100, what happens? If you change the 100 to 1000, what hap-
pens? Try doing a position.x or position.y instead of position.z. Try doing
position.y and position.z at the same time.

Once you have a feel for those numbers, try doing the other hand
and the feet.

Chapter 6. Project: Moving Hands and Feet • 62


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf