While switching cameras is one way of changing the viewpoint, another easier way to change the viewpoint is to simply move or reposition a camera when an event occurs. In the following example, we’ll change the position of the camera when certain keys are hit. Enter the following code to create a simple cylinder:
; Lighting and Cameras ; ––––––––––––––––—— Graphics3D 640,480 SetBuffer BackBuffer() ;Create camera camera=CreateCamera() ; Creating a light light=CreateLight() ; This is the code for creating the cylinder cylinder=CreateCylinder() PositionEntity cylinder,0,0,5 ; This following code makes our program run While Not KeyDown( 1 ) RenderWorld Flip Wend End
If you run the program now, you should just see a cylinder as in Figure 7.6.
The following code will move the camera when certain keys are pressed:
While Not KeyDown(ESC_KEY) If KeyHit(ONE_KEY) Then PositionEntity camera, 0,-1,0 If KeyHit(TWO_KEY) Then PositionEntity camera, 0,1,0 If KeyDown(THREE_KEY) Then PositionEntity camera, 1,0,0 If KeyDown(FOUR_KEY) Then PositionEntity camera, 0,0,0 RenderWorld Flip Wend
When you run the program, pressing the keys 1 through 4 will change the viewpoint (see Figures 7.7–7.10).
3.17.78.47