3D Game Programming

(C. Jardin) #1
Notice that we didn’t create a new cover for the hand. Instead we reused the
same cover, which we named cover when we used it for the avatar’s body. That
saves us a bit of typing.

Less typing is a good thing since we’re all programmers and programmers
are lazy at heart. That reminds me of some programming wisdom I would like
to share with you:

Good Programmers Are Lazy
I don’t mean that programmers hate doing work. We actually love
our jobs and often spend too much time working because we love
it so much.

No, what I mean by lazy is that we hate doing work that computers
are better at. So instead of creating hands and feet individually, we
would rather write a single hand/foot and then copy it as many
times as necessary.

Being lazy benefits us in two very important ways:



  • We type less. Believe it or not, this is a big win. Not only do we have to
    type less the first time around, but we have to read less when we want to
    update later.

  • If we want to change the way a limb is created, we only have to change
    one thing. That is, if we want to change the cover or even the shape of a
    hand in the future, then we only have to make a change in one place.


So let’s see if we can be even lazier when we create the left hand for our avatar:


varleft_hand =newTHREE.Mesh(hand, cover);
left_hand.position.set(150, 0, 0);
scene.add(left_hand);

Not only did we not make a new cover for the left hand, but we also didn’t
create a new shape! Instead we just used the same shape for the left hand
that we did for the right hand. Now that’s lazy!

With that, our avatar should look something like Figure 2, Avatar with Hands,
on page 28.

OK, I admit that doesn’t look much like a body with hands. Bear with me for
a bit longer, and you’ll see.

report erratum • discuss

Making a Whole from Parts • 27


Prepared exclusively for Michael Powell

Free download pdf