Camera Movement

In Simple3DTest1, you applied a transform to the tree polygons to make the tree move toward and away from the camera. Actually, could you really tell what was moving? Was it the tree or was it the camera? It doesn't matter which was moving because, from the viewer's point of view, you perceive only the relative distance to the tree. Instead of changing your 3D coding techniques to allow a camera anywhere in the world, use a Transform3D that represents the camera location, and apply the inverse of this transform (subtract) to every polygon in the scene. So basically, you move the world around, rather than moving the camera around. This way, you can keep your camera at (0,0,0) to simplify the math, but it will appear as if the camera is roaming wherever it wants. For a camera transform example, let's say the virtual camera is at (10,50,500) and is looking to the left 5°. To apply the camera transform, displace every polygon by (–10,–50,–500) and rotate to the right 5°. In other words, just call the subtract(Transform3D) method for each polygon. Rotation around each axis provides the following effects in your 3D coordinate system:

  • Rotating around the x-axis is like looking up or down.
  • Rotating around the y-axis is like turning your head left or right.
  • Rotating around the z-axis is like tilting your head to the side.

In your camera code, you'll actually limit how much you can look up and look down. Also, you'll keep the camera tilting for demonstration purposes; allowing users to tilt their head in a normal 3D game usually isn't necessary.