Math

Don’t skip this section just because of the title! I know that math can be fairly intimidating for many people, but I promise that this will be easy. Math actually plays a huge role in all types of video games, but luckily for us, most of the calculations are done behind the scenes by the computer. That being said, there will be various instances where you will have to do some calculations in your programs. The truth is that throughout the examples in the book so far, we’ve actually done a bit a math, and you probably didn’t even notice.

Let’s start with a simple example of how you would use math. Earlier in this chapter, we used math when we were determining the score when a sphere bumped into a sphere. Do you remember the code we entered? It was score#=score#+1. See? Just simple addition. The beauty of doing math in Blitz3D is that not only can you work with numbers, but you can also include variables in your equations. The following chart shows you the commands you can use in Blitz3D to write equations:

+Addition
Subtraction
×Multiplication
/Division
=Equals

Let’s put our math skills to use by writing a small program that does some math (by the way, all programs you create use math). We are going to write a program that does a simple conversion for us. I happen to live in Argentina, where everyone uses the metric system, which makes things difficult when driving through the States because our speed limits are in kilometers per hour and the U.S. speed limits are in miles per hour. So, as an example, let’s start by creating a program that converts kilometers to miles. Start by creating a blank program that contains the following code:

; Km conversion
;________________
Graphics3D 640,480
SetBuffer BackBuffer()
; Create camera
camera=CreateCamera()
;Create light
light=CreateLight()
;The following code makes our program run
While Not KeyDown( 1 )
RenderWorld
Flip
Wend
End

In order for this program to work, players will first need to enter the number of kilometers they want converted. To do this, we will add something called a string variable.

;Create light
light=CreateLight()
;Inputting the text
Km = Input
The following code makes our program run
While Not KeyDown( 1 )

Now it’s time for the math. At this point, the player has entered in a number for the number of kilometers he wants converted. This number becomes the variable called Km#. Since there are about 0.62137 miles for every kilometer, we need to do some multiplication to figure out the number of miles. To do this, we are going to have Blitz3D calculate the answer to Km#* 0.62137 and then display it onscreen. Remember that * is multiplication in Blitz3D. Enter the following code:

;Create light
light=CreateLight()
;Inputting the text
Km = Input ("Please enter the number of km you want to convert to miles ")
;The following code makes the program run
While Not KeyDown( 1 )
RenderWorld
Text 100,50, "There are " +km*0.62137 + " miles in "
+ km +
Flip
Wend
End

Run the program now, and you’ll see your math skills in action. You enter a number in kilometers, and you get your answer in miles (see Figure 13.14). Pretty cool, eh? (That’s Canadian talk for “Pretty cool, isn’t it?”)

Figure 13.14. Your math skills have created a conversion calculator.


While this is just one simple example of how math can be used, you can see that we didn’t create any special variables for our equation; we just multiplied our variable (km#) by an amount to get our answer.

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

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