Switching Cameras Virtually

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.

Figure 7.6. The code you created should produce this cylinder.


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.77.10).

Figure 7.7. View 1 of demo07-03.bb.


Figure 7.8. View 2 of demo07-03.bb.


Figure 7.9. View 3 of demo07-03.bb.


Figure 7.10. View 4 of demo07-03.bb.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.78.47