Creating Collisions

Collisions are of utmost importance to our game for several reasons. Using collisions, we’ll have the bullets destroy the targets, we’ll prevent the player from falling through the ground, and we’ll randomly reposition the targets when they hit the ground. We need to identify our collisions, assign a collision type to each object, and then define the collisions. We’ll start with the easy part, identifying the collision types. There are four different groups of collision objects that we will create for this game: the scenery, the bullets, the player, and the gallery items. To define these groups, add the following code in bold:

Graphics3D 1024,768
SetBuffer BackBuffer()

;Setting the type values
type_player=1
type_scenery=2
type_gallery=3
type_bullet=4

Now that we have the types identified, we need to assign each object in our game to one of these types by using the EntityType command. Since many of the objects, such as the targets and the bullets, are part of an array, we need to enter this code within the actual array scattered throughout the program. There are actually eleven different lines of code you need to add in different locations. Here is a list that you can use as a checklist as you go through your program:

EntityType camera, type_player
EntityType water, type_scenery
EntityType terrain, type_scenery
EntityType ducks(x), type_gallery
EntityType seahorse(x), type_gallery
EntityType donkey(x), type_gallery
EntityType flamingo(y), type_gallery
EntityType dolphin(x), type_gallery
EntityType snake(x), type_gallery
EntityType bullet(i), type_bullet

While we’re at it, we’re also going to set the radius size for each of the objects using the EntityRadius command. Here are the EntityType commands in bold, within the actual text of the program along with the corresponding EntityRadius commands.

;Shooting Gallery
;________________
;Key constants
Const ENTER_KEY = 28
Const R_KEY = 19
Const V_KEY = 47
Const P_KEY = 25
Const LEFT_KEY = 203
Const RIGHT_KEY = 205
Const UP_KEY = 200
Const DOWN_KEY = 208
Const SPACE_BAR = 57
Const ESC_KEY = 1

welcome()
Function welcome()
      Graphics 1024,768
      screen=LoadImage ("welcome.bmp")
      DrawImage screen,0,0
      While Not KeyDown(ESC_KEY)
      If KeyDown(ENTER_KEY) Then
         Return
      EndIf
      Wend
End Function

;Setting the graphics for the program
      Graphics3D 1024,768
      SetBuffer BackBuffer()

;Setting the type values
      type_player=1
      type_scenery=2
      type_gallery=3
      type_bullet=4; Creating the camera
      camera=CreateCamera()
      CameraClsColor camera,0, 125, 255
      EntityType camera, type_player
      EntityRadius camera, 5

; Creating a light
      light=CreateLight()

; Creating the water plane
water=CreatePlane()
PositionEntity water, 0,-15,0
watertexture=LoadTexture ("water.jpg")
EntityTexture water, watertexture
ScaleTexture watertexture,15,15
EntityType water, type_scenery; Loading the heightmap
terrain=LoadTerrain ( "ground.jpg" )
ScaleEntity terrain,5,50,5
PositionEntity terrain,-500,-20,-500
tex=LoadTexture( "greenery.jpg" )
ScaleTexture tex, 50,50
EntityTexture terrain,tex
EntityType terrain, type_scenery
;Creating the gallery items
      Dim ducks(5)
      For x = 1 To 5
         ducks(x)= LoadMesh ("duck.3ds")
         PositionEntity ducks(x), x*15,0,90
         EntityColor ducks(x), 255,255,0
         ScaleEntity ducks(x), 0.2,0.2,0.2
         EntityType ducks(x), type_gallery
         EntityRadius ducks(x), 3
      Next

Dim seahorse(5)
      For x = 1 To 5
      seahorse(x)= LoadMesh ("seahorse.3ds")
      PositionEntity seahorse(x), x*6,10,45
      EntityColor seahorse(x), 255,102,0
      ScaleEntity seahorse(x), 0.03,0.03,0.03
      EntityType seahorse(x), type_gallery
      EntityRadius seahorse(x), 3
      Next

Dim donkey(5)
      For x=1 To 5 donkey(x)= LoadMesh ("donkey.3ds")
      PositionEntity donkey(x), x*5,35,85
      EntityColor donkey(x), 204,204,204
      ScaleEntity donkey(x), 0.3,0.3,0.3
      EntityType donkey(x), type_gallery
      EntityRadius donkey(x), 3
      Next

Dim flamingo(5)
      For y=1 To 5
      flamingo(y)= LoadMesh ("flamingo.3ds")
      PositionEntity flamingo(y), y*6,8,25
      EntityColor flamingo(y), 102,51,51
      ScaleEntity flamingo(y), 0.003,0.003,0.003
      EntityType flamingo(y), type_gallery
      EntityRadius flamingo(y), 3
      Next

Dim dolphin(5)
      For x=1 To 5
      dolphin(x)= LoadMesh ("dolphin.3ds")
      PositionEntity dolphin(x), x*6,45,0
      EntityColor dolphin(x), 102,153,255
      ScaleEntity dolphin(x), 0.3,0.3,0.3
      EntityType dolphin(x), type_gallery
      EntityRadius dolphin(x), 3
      Next

Dim snake(5)
      For x=1 To 5
      snake(x)= LoadMesh ("snake.3ds")
      PositionEntity snake(x), x*16,10,10
      EntityColor snake(x), 153,255,153
      ScaleEntity snake(x), 0.06,0.06,0.06
      EntityType snake(x), type_gallery
      EntityRadius snake(x), 5
      Next

;Creating the gun
gun=CreateCylinder(12)
ScaleEntity gun,0.2,0.6,0.4
RotateEntity gun,45,0,0
PositionEntity gun, EntityX(camera),EntityY(camera)-2, EntityZ(camera)+3
EntityOrder gun, -1
EntityParent gun,camera
EntityColor gun, 100,100,100

;Creating the bullets
maxbull = 100
Dim bullet(maxbull)
      For i=0 To maxbull
      bullet(i)=LoadMesh ("bullet.3ds")
      EntityColor bullet(i), 100,100,100
      EntityType bullet(i), type_bullet
      Next

Wow, that was a lot of fishing through our code, but we got it done! Now that we have our objects assigned to different collision types, we must create the collisions themselves. We want to set the collisions for the gallery items and the scenery, the bullets and the gallery items, and the player and the scenery. All will be set to slide (a collision value of 2) except for the collision between the player and the scenery, in which case we want the collision to stop, so we assign it a collision value of 1. Enter the following code in bold:

bullet(i)=LoadMesh ("bullet.3ds")
      EntityColor bullet(i), 100,100,100
      EntityType bullet(i), type_bullet
Next
; Defining the Collisions
Collisions type_gallery,type_scenery,2,2
Collisions type_bullet,type_gallery,2,1
Collisions type_player,type_scenery,2,2
Collisions type_scenery, type_gallery, 2,2

Before we can run our program and test to see if our collisions work, there is one important line of code we need to enter: UpdateWorld. UpdateWorld checks for collisions and takes the appropriate actions, and we need to enter it before RenderWorld near the end of the program:

Updateworld
      RenderWorld
      Flip
Wend
End

Now run the program, and, with any luck, what you see on your screen should look similar to Figure 14.14. Notice how your player no longer falls through the ground, because of the collisions we’ve created.

Figure 14.14. After creating collisions, the game should start to come together.


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

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