Have you ever heard the saying “There are many ways to skin a cat”? That saying means that there are different methods to accomplish a specific task, and nowhere is that more true than in creating explosions in Blitz3D. I’m going to show you a simple way to create the illusion of an object blowing up. It will require quite a few lines of code, but I’ll break it down into pieces in order to make it easier to understand.
Imagine a bullet (in this case the bullet will be a simple sphere) hitting an object and causing that object to shatter into a few pieces that fly away. We would accomplish this in a few different steps. The first thing that we would do is have the object (in our example the object will be a cone) disappear when it is hit by the bullet. In the last section, you learned to do this by using the HideEntity command when a collision occurred. The next thing we’ll do is have three tiny spheres appear where the cone once stood and simultaneously have those fly in different directions so that it appears that they are particles from the exploding cone.
Start by opening the file called demo11-07.bb. This is the complete explosion program, with a cone that we will “blow up” and a sphere that will act as our bullet. Collision groups have already been set up for these objects. We will go over the code in this section.
We will start by making the cone disappear as soon as it is hit by the sphere. This was covered in the last section, so try doing it on your own first and then review the code below:
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) HideEntity cone UpdateWorld MoveEntity sphere,x#,y#,z# RenderWorld Flip Wend
The next thing we need to do is create the pieces that will make up the shattered cone. These pieces will be simple spheres. We have two options here: We can create the spheres within the If CountCollision statement, or we can create global variables. Let’s say in our game we want to create many explosions. Rather than having to create particles for our exploded objects every time they are destroyed, we can create a global variable. These global variables can be included in any function. In this case we will create a global variable that creates a sphere. That way, every time an explosion occurs, we won’t have to use the CreateSphere command; we’ll only have to reference our global variable. Sound confusing? It will become quite a bit clearer in a moment when you put it in use. You can see the following three global variables at the beginning of the program just underneath the line SetBuffer BackBuffer().
;demo11-07.bb - Explosssssionnnnnns ;_______________ Graphics3D 640,480 SetBuffer BackBuffer() ;Global particle variables Global particle=CreateSphere() Global particle1=CreateSphere() Global particle2=CreateSphere()
Here we have created four spheres. The beauty of making them global variables is that whenever we need a sphere to act as part of an explosion, we don’t have to create new spheres; we can just call on any of these three. As it stands, these spheres are a little big to be particles in an explosion, so in the next step we’ll reduce their size and color them. Look at the following code:
;Handle particles ScaleEntity particle, 0.2,0.2,0.2 EntityColor particle, 235,125,23 ScaleEntity particle1, 0.2,0.2,0.2 EntityColor particle1, 125,125,125 ScaleEntity particle2, 0.2,0.2,0.2 EntityColor particle2, 235,1,234
So with this code we’ve given each particle in the explosion a different color and we’ve reduced the size of the spheres. Now it’s on to the code that will create the explosion. Basically, when the collision between the sphere and the cone takes place, we want to place the three spheres at the exact location of the cone, and then we will scatter those spheres. Let’s take a look at the main loop:
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) PositionEntity particle, EntityX(cone),EntityY(cone), EntityZ(cone) PositionEntity particle1, EntityX(cone),EntityY(cone), EntityZ(cone) PositionEntity particle2, EntityX(cone),EntityY(cone), EntityZ(cone) HideEntity cone End If UpdateWorld RenderWorld Flip Wend End
By adding this code, we are saying that when a collision with the sphere occurs, put the three spheres at the exact location of the cone. If we looked at the location of the cone in the code (which happens to be at 3,0,5), we could have just entered that location rather than EntityX(cone),EntityY(cone), EntityZ(cone). We entered that code because it will determine a value of the exact location of the cone, even if the cone was moving. In many games, our target will be moving, so we won’t always know its exact location. Entering this code will always tell you the exact location of an object.
We need to enter one more valuable piece of code to this text. In the next section, we will send those three spheres flying. Before we do that, we need to create a variable saying that the collision has happened and that the three particles have been put in place. You can create absolutely any variable you want—it makes no difference as long as we have a variable we can call on in the next section. In this instance, we are going to create the variable called explosion and make its state ready by entering the following code in bold:
If CountCollisions (sphere)
PositionEntity particle, EntityX(cone),EntityY(cone), EntityZ(cone)
PositionEntity particle1, EntityX(cone),EntityY(cone), EntityZ(cone)
PositionEntity particle2, EntityX(cone),EntityY(cone), EntityZ(cone)
explosion=ready
HideEntity cone
End If
Adding explosion=ready may not make much sense right now, but in the next section you’ll see why we had to add it.
Finally, we have to send our spheres flying. We’ll do so by adding the following If statement in bold:
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)
PositionEntity particle, EntityX(cone),EntityY(cone), EntityZ(cone)
PositionEntity particle1, EntityX(cone),EntityY(cone), EntityZ(cone)
PositionEntity particle2, EntityX(cone),EntityY(cone), EntityZ(cone)
HideEntity cone
explosion=ready
End If
If explosion=ready
MoveEntity particle, -.3,.5,0
MoveEntity particle1, +.5,.5,0
MoveEntity particle2, .5,-.5,0
End If
UpdateWorld
RenderWorld
Flip
Wend
End
This If statement is quite straightforward. It says that when the variable explosion=ready is true, then move the spheres in the following directions. Even though this is a separate If statement, it is part of the main loop of the program, so it will continue over and over, meaning those particles will keep moving to infinity and beyond. If we had added it to the same loop that positioned the three spheres, then the three spheres would have only moved once by a small increment and wouldn’t fly off the screen.
Here’s the magic moment. Go ahead and run the program and crash the sphere into the cone. Ta da! You should have your explosion.
As I mentioned in the beginning of this section, there are many ways to skin a cat, and there are a variety of ways we could’ve created this type of explosion. While the code we’ve created here creates an explosion, there are some potential problems that may arise—for example, what about the flying debris? Does it go on forever? What if I have other objects that collide with the bullet? Will they create an explosion? Later in the book, when you learn about functions, you’ll see how you can group code together to combine common tasks, which will certainly help in creating exploding objects. Another way you can solve potential problems with this collision is to use some timing.
18.227.161.173