Learn how to track the camera over the surface of a terrain mesh.
In the last tutorial we looked at the Elevation extrusion class, which can be used to make a terrain mesh from a height map. In this tutorial we will use the ElevationReader to create a camera that moves across the terrains surface.
MoveableCamera.as
The MoveableCamera class responds to keyboard input to move the camera around, and then uses the ElevationReader to keep the camera over the surface of the terrain. The effect of this is that the camera appears to walk over the surface of the terrain.
First up we need to add event listeners to the keyboard events KEY_UP and KEY_DOWN. The event listener functions are attached to the stages keyboard events. [code]
We then create a new ElevationReader object, and call the traceLevels function supplying the same parameters that were supplied to the Elevations generate function. [code]
The keyDown and keyUp functions watch for keyboard key presses and releases, and set the variables forward, backward, turnLeft and turnRight to true when their corresponding keys are pressed, and false when they are released. If you need to find what keyCode corresponds to which key, you can use this utility here. [code]
In the enterFrame function the camera is moved and rotated according to which of the forward, backward, turnLeft and turnRight variables are true. [code]
We then call the ElevationReader getLevel function to find the height of the terrain at the cameras current position. The position on the z axis is negative because of the way that the terrain was flipped in the ApplicationManager. [code]
ApplicationManager.as
The ApplicationManager is the same as the last tutorial, except that the variables that are passed to the generate function are now static variables (so they can be more easily shared with the MoveableCamera class). [code]
We also create a new instance of the MoveableCamera class. [code]
Using the ElevationReader we can easily create a camera that follows the height of terrain created with the Elevation class. This makes interacting with the terrain quite easy.
Go back to Away3D Tutorials