3D Game Programming

(C. Jardin) #1
Also new to us is converting from local coordinates to world coordinates with
the localToWorld() method. For most of this book we have found it very useful
to work in a frame of reference—it’s an invaluable trick for 3D programmers.
Every now and then we have to put things back in the regular scene coordi-
nates. This is one of those times. The localToWorld() method gives us the scene
coordinates for the random river points so that when the fruit is added to the
scene, it looks as though it was added to the river.

Before a localToWorld() call, it’s a good idea to call updateMatrixWorld() on the thing
whose world coordinates we need. A matrix is a mathematical way to describe
position, direction, and other values in 3D. The updateMatrixWorld() call ensures
that these values are all up-to-date and accurate.

With that, we have two pieces of fruit that can help you score points like crazy
while playing the game. You might even be able to beat my high score:

20.6 The Code So Far


If you would like to double-check the code in this chapter, turn to Section
A1.20, Code: River Rafting, on page 265.

21.4 What’s Next


That was a lot of code. But it was worth it. We put many of our skills to use
with physics, lights, and materials. We also saw glimpses of what else is
possible in 3D programming by pulling vertices of shapes and converting local
coordinates to “world” coordinates.

Like our other games, do not consider this one final. There’s still plenty that
you can add. Maybe you can incorporate obstacles that take away points?
Add some jumps? Make this a multilevel game as we did in Chapter 19, Project:
Multilevel Game, on page 177? Make the course longer? You might try adding
camera controls so you can see from the viewpoint of a raft passenger instead
of viewing everything from above. Or maybe you already have ideas for how
to improve this game that I can’t even begin to think of.

In our final chapter we’ll switch gears to putting our projects out on the Web.


report erratum • discuss

The Code So Far • 205


Prepared exclusively for Michael Powell

Free download pdf