Moving Objects

Now that you know how to create objects, we’re going to have some fun and start moving them around the screen. We’ll start off by taking a simple object (a sphere with the brick texture map applied to it) and create controls so that by using our keyboard we can move it around the screen. First, open the file called moving a demo06-01.bb from the CD, or enter the following code. Save the file to a new folder and copy the file called brick.jpg to that folder.

;demo06-01.bb - Moving a sphere
; –––––––––
Graphics3D 640,480
SetBuffer BackBuffer()
;Key constants
Const A_KEY = 30
Const Z_KEY = 44
Const LEFT_KEY = 203
Const RIGHT_KEY = 205
Const UP_KEY = 200
Const DOWN_KEY = 208
Const ESC_KEY = 1

; Create camera
camera=CreateCamera()
; Create a light
light=CreateLight()

;Create sphere
sphere=CreateSphere(16)
PositionEntity sphere,0,0,5

;Load the texture
tex=LoadTexture( "brick.jpg" )
ScaleTexture tex, .5,.5
EntityTexture sphere,tex

; This following code moves our sphere:
While Not KeyDown(ESC_KEY)

    If KeyDown(A_KEY) TranslateEntity sphere,0,-.3,0
    If KeyDown(Z_KEY) TranslateEntity sphere,0,.3,0
    If KeyDown(LEFT_KEY) TranslateEntity sphere, -0.3,0,0
    If KeyDown(RIGHT_KEY) TranslateEntity sphere, 0.3,0,0
    If KeyDown(UP_KEY) TranslateEntity sphere,0,0,0.3
    If KeyDown(DOWN_KEY) TranslateEntity sphere,0,0,-0.3

    RenderWorld
    Flip
Wend
End

Run the program now, and what you see on the screen should look similar to Figure 6.1. You can control the sphere with the A and Z keys and the arrow keys.

Figure 6.1. The demo06-01.bb program.


Now take a closer look at the part of the program that actually controls the movement:

; This following code moves our sphere:
While Not KeyDown(ESC_KEY)

    If KeyDown(A_KEY) TranslateEntity sphere,0,-.3,0
    If KeyDown(Z_KEY) TranslateEntity sphere,0,.3,0
    If KeyDown(LEFT_KEY) TranslateEntity sphere, -0.3,0,0
    If KeyDown(RIGHT_KEY) TranslateEntity sphere, 0.3,0,0
    If KeyDown(UP_KEY) TranslateEntity sphere,0,0,0.3
    If KeyDown(DOWN_KEY) TranslateEntity sphere,0,0,-0.3

    RenderWorld
    Flip
Wend

Let’s examine this section of code piece by piece to see what is going on.

While Not KeyDown(ESC_KEY)—This basically means: as long as the Esc key is not pressed down, continue the program.

If KeyDown(A_KEY) TranslateEntity sphere,0,-.3,0—This line is saying that when the letter A is pressed on the keyboard, move the object down the y axis by 0.3 units. The command that tells Blitz3D to actually move the object is TranslateEntity. The rest of the lines basically say the same thing, but using different keys and designating different movements along the x, y, and z axes.

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

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