Now that we know what is required to define a collision, let’s create one in a simple program. The following program contains two simple objects: a sphere that will be our player that we move and a cone that will be the object we smash into. Open the file demo11-01.bb or enter the following code:
;demo11-01.bb - Collisions ; ———————————————— Graphics3D 640,480 SetBuffer BackBuffer() Const ESC_KEY = 1 Const LEFT_KEY = 203 Const RIGHT_KEY = 205 Const UP_KEY = 200 Const DOWN_KEY = 208 camera=CreateCamera() ; Creating a light light=CreateLight() ; Creating a sphere sphere=CreateSphere() ScaleEntity sphere, 0.5,0.5,0.5 PositionEntity sphere, -3,0,5 ; Creating a cone cone=CreateCone() PositionEntity cone, 1,0,5 ScaleEntity cone, 1.5,1.5,1.5 ; This following code makes our program run While Not KeyDown(ESC_KEY) x#=0 y#=0 z#=0 If KeyDown(LEFT_KEY)=True Then x#=-0.1 If KeyDown(RIGHT_KEY)=True Then x#=0.1 If KeyDown(DOWN_KEY)=True Then y#=-0.1 If KeyDown(UP_KEY)=True Then y#=0.1 MoveEntity sphere,x#,y#,z# RenderWorld Flip Wend End
Go ahead and run the program and use the arrow keys to move the sphere around. You can see that when the sphere gets to the cone, it goes right through. We’ll now create a collision so that the sphere won’t be able to go through the cone. The first thing we’ll do is create the types. In this instance we’ll create two types—one for the sphere, which we’ll call player, and the other, which we’ll call “obstacle.” Enter the following code under the “SetBuffer” section to create the types. If you ran the program now (see Figure 11.4), the sphere could go through the cone.
; Creating the types type_player=1 type_obstacle=2
The number that we chose for the two different types (1 and 2) don’t have any importance. We could have selected any numbers; it’s just important that they have a number assigned to them.
Next we have to assign the objects that we created to our types. Add the following code (in bold) to assign the two objects to the different teams (these code changes are in demo11-02.bb):
; Creating a sphere sphere=CreateSphere() ScaleEntity sphere, 0.5,0.5,0.5 PositionEntity sphere, -3,0,5 EntityType sphere,type_player ; Creating a cone cone=CreateCone() PositionEntity cone, 1,0,5 ScaleEntity cone, 1.5,1.5,1.5 EntityType cone,type_obstacle
Now we will create the collision itself. Add the code in bold below in the specified location:
Collisions type_player,type_obstacle,2,2
; This following code makes our program run
While Not KeyDown( 1 )
x#=0
y#=0
z#=0
If KeyDown( 203 )=True Then x#=-0.1
If KeyDown( 205 )=True Then x#=0.1
If KeyDown( 208 )=True Then y#=-0.1
If KeyDown( 200 )=True Then y#=0.1
MoveEntity sphere,x#,y#,z#
UpdateWorld
RenderWorld
Flip
Wend
End
Let’s take a close look at the code to see what we have just done.
Collisions type_player,type_obstacle,2,2—This is the code that creates the collision and defines the methods and response for the collision.
UpdateWorld—This line is very important because it checks for collisions.
Now go ahead and run the program and you should see that your objects can’t get close, as in Figure 11.5.
3.137.199.214