Velocity

Creating the illusion of velocity can make your game feel more realistic. In real life, most objects don’t just start and stop. Think of racers who sprint in the Olympics. When the gun sounds they aren’t going their top speed instantly; it takes several seconds for them to reach their maximum stride. The same holds true when they cross the finish line. They don’t stop instantly; it takes them quite a few steps to apply the brakes and slow down. By controlling an object’s velocity in your game, you can make its movement seem more realistic. In an earlier chapter, you learned how to have objects move on their own. Here you will learn to increase and decrease their speed or stop on a dime. These types of controls are great for games that contain any type of moving vehicle that the player controls.

Start by opening the program called demo13-03.bb. This program contains a textured sphere that is just sitting there. We are going to add controls that will act like a gas pedal, a brake, and an emergency brake to move and stop the sphere. To keep things simple, we will only add controls for the z axis.

; The following code makes the program run
While Not KeyDown(ESC_KEY)
If KeyDown(DOWN_KEY)=True Then velocity#=velocity#-0.001
If KeyDown(UP_KEY)=True Then velocity#=velocity#+0.001
If KeyDown(S_KEY) Then velocity#=0
MoveEntity sphere,0,0,velocity#
TranslateEntity sphere, 0,-0.03,0
Collisions type_player,type_ground,2,2
Collisions type_ground,type_player,2,2
UpdateWorld
RenderWorld
Flip
Wend
End

With these four lines of code, we’ve created a gas pedal, a brake (or reverse), and an emergency brake. Let’s look at the breakdown of the code we added:

If KeyDown(UP_KEY)=True Then velocity#=velocity#-0.001—This code says that when the up arrow is pressed (key 208), then increase the current velocity by 0.001 units. In other words, every time you press the up arrow, the velocity will become greater and greater. By adjusting the velocity number (0.001), you can increase or decrease the speed of the acceleration. By the way, we created a variable here called velocity, but we could have called it anything we wanted—it’s just a name we are assigning to this variable.

If KeyDown(DOWN_KEY)=True Then velocity#=velocity#+0.001—This code is the opposite of the code above. It says that when the down arrow is pressed, decrease the current velocity by 0.001 units.

If KeyDown (S_KEY) Then velocity#=0—This is our emergency brake. The code says that if you press the S key on your keyboard (key 31), then the velocity will become 0.

MoveEntity sphere,0,0,velocity#—This is the code that sets everything in motion. It says that while the program is running, don’t move the sphere at all along the x or y axes, but move it along the z axis by the velocity number. The velocity number will change depending on whether the up arrow, down arrow, or S key is being pressed.

Now go ahead and run the program. Pressing the up arrow will increase your velocity, while pressing the down arrow will slow you down (see Figure 13.4). If you keep pressing the down arrow, you will eventually start accelerating in reverse. You can press the S key at any time to stop the acceleration.

Figure 13.4. You can use the arrow keys to zoom the ball around the playing field.


Note: Now You Try

Right now we can only accelerate and decelerate along the z axis. Add the necessary code to this program to allow for acceleration and deceleration along the x axis using the left and right arrow keys. Once you’re done, compare your code to the code added below in bold.


; The following code makes the program run
While Not KeyDown(ESC_KEY)
If KeyDown(DOWN_KEY)=True Then velocity#=velocity#-0.001
If KeyDown(UP_KEY)=True Then velocity#=velocity#+0.001
If KeyDown (S_KEY) Then velocity#=0
If KeyDown (S_KEY) Then xvelocity#=0
						If KeyDown(LEFT_KEY)=True Then xvelocity#=xvelocity#-0.001
						If KeyDown( RIGHT_KEY)=True Then xvelocity#=xvelocity#+0.001
MoveEntity sphere,xvelocity#,0,velocity#
TranslateEntity sphere, 0,-0.03,0
Collisions type_player,type_ground,2,2
Collisions type_ground,type_player,2,2
UpdateWorld
RenderWorld
Flip
Wend
End

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

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