Poof! Now You See It, Now You Don’t

One type of collision you’ll want to create often is having one object disappear when it hits another. This is perfect when you create things like bullets that hit enemies, since you’ll want the enemies to disappear. We’ll keep things simple in this example. We’ll have one object (a cone) disappear when it is hit by a sphere. Demo11-04.bb shows how to do this. This program controls a sphere that can collide with a cone. When you run the program, you’ll see that you can control the sphere, but it can’t go through the cone since a collision has been created. The following code makes the disappearing occur!

; 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#
If CountCollisions (sphere)=True Then HideEntity cone
UpdateWorld
RenderWorld
Flip
Wend
End

Run the program now and see what happens. When the sphere hits the cone, it disappears, as seen in Figure 11.8. Let’s take a close look at the one line of code we added to create this effect. The first part uses the CountCollision command. As mentioned earlier, Blitz3D counts how many times one object has collided with another. The CountCollision command will tell you how many collisions have occurred. When we add the code If CountCollisions (sphere)=True we are basically saying that if there are any collisions of the sphere at all, then do the following, HideEntity cone which will hide the cone.

Figure 11.8. Poof! The cone disappears when it is hit by the sphere in demo11-04.bb.


Start by opening the file demo11-06.bb, and save it with a new name. This file has a sphere that you can control and two cones. Collisions are currently set up so that the sphere can’t go through the cones. Let’s look at the following code that will make any object that the sphere collides with disappear.

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

      If CountCollisions (sphere)
      crash=CollisionEntity (sphere,1)
      HideEntity crash
      EndIf

      MoveEntity sphere,x#,y#,z#

      UpdateWorld
      RenderWorld

      Flip
Wend

Run the program now and you’ll see that any object the sphere encounters will disappear. Now let’s take a close look at the code we used to get this effect.

If CountCollisions (sphere)—This part of the code says that if the sphere has registered any collision at all, then do the following.

crash=CollisionEntity (sphere,1)—Here we create an entity called crash (which we could have called any name we chose) and we assign the Collision Entity command to it. The collision entity has two parameters in parentheses: the first is sphere and the second is 1. This basically tells Blitz3D to remember what the sphere has crashed into and call it crash.

HideEntity crash—This will hide the entity called crash, which is whatever the sphere has crashed into.

EndIf—This ends the If statement.

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

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