Game Development Reference
In-Depth Information
Great, everything works as expected. The sun is rotating only around itself. The planet is orbiting
the sun at a distance of 3 units. It is also rotating around itself and is 20 percent as big as the
sun. The moon orbits the planet, but also moves along with it around the sun due to the use of
the matrix stack. It also has local transformations in the form of self-rotation and scaling.
The HierarchicalObject class is generic enough that you can play around with it. Add more
planets and moons, and maybe even moons of moons. Go crazy with the matrix stack until
you get the hang of it. It's again something you can learn only through a lot of practice. You
need to be able to visualize in your brain what's actually going on when combining all the
transformations.
Note Don't go too crazy with the matrix stack. It has a maximum depth, usually between 16 and
32 entries, depending on the GPU/driver. Four hierarchy levels are the most we've ever had to use
in an application.
A Simple Camera System
In the previous example, we saw a hint of how we could implement a camera system in 3D.
We used glTranslatef() to push down the complete world by 2 units on the y axis. Since the
camera is fixed to be at the origin, looking down the negative z axis, this approach gives the
impression that the camera itself was moved up by 2 units. All the objects are still defined with
their y coordinates set to 0.
Instead of actually moving the camera, we need to move the world around. Say we want our
camera to be at position (10,4,2). All we need to do is use glTranslatef() as follows:
gl.glTranslatef(−10,-4,-2);
If we wanted our camera to be rotated around its y axis by 45 degrees, we could do the
following:
gl.glRotatef(−45,0,1,0);
We can also combine these two steps, just as we do for “normal� objects:
gl.glTranslatef(−10,-4,-2);
gl.glRotatef(−45,0,1,0);
The secret is to invert the arguments to the transformation methods. Let's think about it using
the preceding example. We know that our “real� camera is doomed to sit at the origin of the
world, looking down the z axis. By applying inverse camera transformations, we bring the world
into the camera's fixed view. Using a virtual camera rotated around the y axis by 45 degrees is
the same as fixing the camera and rotating the world around the camera by −45 degrees. The
same is true for translation. Our virtual camera could be placed at (10,4,2). However, since our
real camera is fixed at the origin of the world, we just need to translate all objects by the inverse
of that position vector, which is (−10,-4,-2).
 
Search WWH ::




Custom Search