In this program, you will learn how to use the following:

  • The keyboard to move objects around the screen

  • if statements

  • Move commands to move right, left, up, and down

  • AddAngle commands to rotate an object about its x, y, and z axes

  • The CreateSphere command

In the previous programs in Chapter 3, we moved objects around the screen using the Walk command, which was very useful. However, we were restricted to rotating the object and moving it forward/backward. What if we want to make the object move in other directions, like up, down, or sideways ? And what if we want to use other keys instead of the arrow keys to cause such actions to occur? In this project, we will move a sphere around the screen using some more versatile commands. Figure 4.1 shows what the end result should look like.

Figure 4.1: Screenshot of the Sphere1 project.

if Statements

Consider the following Jamagic statements:

 if(Keyboard.IsKeyDown(Keyboard.UP)) {   mysphere.Move(200,2); } 

This looks pretty mysterious , but let s think about it. The statement is saying: if the up arrow key is pressed, then move the object mysphere “200 units back (note the negative sign in front of the 200; this indicates backwards motion ”motion towards the camera); this motion is to take 2 seconds to occur.

Now try to understand what the following statements do:

 if(Keyboard.IsKeyDown(Keyboard.DOWN)) {   mysphere.Move(100,2); } 

Obviously these are very similar to the previous statements, but now if the down arrow key is pressed, the object mysphere is moved ahead 100 units in 2 seconds. The fact that there is no negative (“) sign in front of the number 100 indicates that the number is positive.

The codes for other keys are listed in the Help section of the Jamagic software, but here are a few more that will be used in our game, followed by comment lines that tell you what they are:

 if(Keyboard.IsKeyDown(Keyboard.RIGHT)) // right arrow key if(Keyboard.IsKeyDown(Keyboard.LEFT)) // left arrow key if(Keyboard.IsKeyDown(Keyboard.SHIFT)) //shift keymoves up if(Keyboard.IsKeyDown(Keyboard.CAPSLOCK)) //Caps Lock key//to move down 

The if statement is an extremely powerful and useful programming tool, and it exists in just about every programming language.

Here is another example of an if statement ( please note that we will not use this example in our program ”it s presented only for illustration purposes):

 if (speed = = 3) { mysphere.move(10) } 

These lines mean that if speed is equal to 3 then mysphere will be moved 10 units forward. You get the basic idea: everything inside the brackets is done when the condition specified in the if statement is met.

From this, you might have been able to figure out that the if statement has three parts :

  1. The if statement itself, which tells which condition to test for, with the condition to be tested in parentheses ( if (speed = = 3) )

  2. The brackets, which indicate the end and beginning of the action to be taken

  3. The action to be taken (in our example, the action is to move mysphere )

Now that you know the components of the if statement, let s look at the one we have been discussing a little more closely. For instance, you may be wondering why there are two equal signs in succession in the statement if (speed = = 3) . The reason is that the single equal sign (=) is used to set a value. For example, the statement speed = 3 can be used in your code to set speed to a value of 3 . If you tell Jamagic if (speed = 3) you are setting speed equal to 3, then the if statement is automatically satisfied! By using two equal signs, you are telling Jamagic that you are just seeing whether the value of speed is the same as 3 .

The Move Command

The Move command needs to be explained in more detail also. The entire syntax for the command is as follows :

 myobject.Move(distance, time, animation, animation speed); 

myobject refers to the 3D object you want moved. Move moves it in the direction that the object is facing , and distance is the number of units you want the object moved. time refers to the number of seconds you want the move to take ”if you want the object to cover the specified distance quickly, use a lower number of seconds. Incidentally, if you use any number lower than 1, such as 0.5, make sure that you do not omit the zero. If you do, you will get an error message. This is true in all cases. If you leave out the time parameter, then the object will cover the specified distance immediately.

An important note about leaving out the time parameter (for example, myobject.move(100); ): this can be very useful if you want the object to keep moving as you hold down the key. Normally, if you specify the time and duration, the object moves the distance specified in the time specified, but you must press the key again if you want the object to continue moving beyond the specified distance. By leaving out the time parameter, the object will keep moving as long as you hold down the key. The downside of this is that the speed at which the object will move depends on the computer. This means that your game might seem much too slow on some machines or much too fast on others.

The animation parameter is the animation to be played , if any, while the object moves, and animation speed is the speed at which the animation is to be played. We will not use these features in this text. You are encouraged to explore the Help section of the Jamagic software, or wait for the advanced version of this book.

Here are some other Move commands:

 mysphere.MoveRight(); mysphere.MoveLeft(); mysphere.MoveDown(); mysphere.MoveUp(); 

These are pretty much self-explanatory. For example, mysphere.MoveRight() moves the object mysphere to the right, based on the direction the object is facing. The same parameters can be used inside these parentheses as were previously discussed for the Move command.

There is one last statement we will learn in this section:

 mysphere = oworld.CreateSphere(100,100,100,10,10); 

This statement creates a 3D sphere for us in our world. The sphere s name is mysphere and our world s name is oworld . The first three numbers inside the parentheses indicate the size of our sphere, which, in this case, is 100 units in the x direction, by 100 in the y and z directions.

If these numbers were not equal to each other, say (100, 50, 60), then the sphere would not be a sphere, instead, it would be elliptically shaped in the 3D world ”like some form of egg.

The last two parameters in the parentheses indicate the number of triangles you want the sphere to be made of in the x and y directions. Remember how every 3D object is made of triangles? The more triangles you specify, the smoother looking your sphere will be.

You may be wondering why you shouldn t just use a huge number of triangles for better appearance. The answer is that there is a limit, based on your computer s speed and memory, of the number of triangles or polygons that can be tracked by your computer without slowing down your game. That is the reason why commercially available games do not show anywhere near the detail of a real-world scene: computers do not (yet) have the capacity to keep track of so many polygons. You will do well to remember this when making your games ”if you end up with a very slow game you will know what to check!

OK, you should now be ready to type in the complete code. To do so, make a new program called Sphere1 in your Projects subdirectory and then type in the following code. Please note that the if statements go inside the game loop. This makes sense, since the computer should be constantly checking for user input. Recall that items in the game loop keep repeating, while the initial code that lies outside of the game loop is looked at only once at the beginning.

Sphere1 Code

 // // Sphere1: First Spherelander game. Will create sphere and // move it around // with the arrow keys plus cap and shift keys. // Covers: if statements, using keyboard to move objects, //        move commands, createsphere command // Create a new world with a new camera. oworld = New World(); ocamera = New Camera(oworld); // Create a sphere. mysphere = oworld.CreateSphere(100,100,100,10,10); mysphere.SetPosition(0,0,0); // Camera ocamera.SetPosition(0,0,500); //Game loop While(1) // The first bracket indicates start of game loop. { if(Keyboard.IsKeyDown(Keyboard.UP))  {    mysphere.Move(200,2); } if(Keyboard.IsKeyDown(Keyboard.DOWN))  {    mysphere.Move(200,2); } if(Keyboard.IsKeyDown(Keyboard.RIGHT))  {    mysphere.MoveRight(200,2); } if(Keyboard.IsKeyDown(Keyboard.LEFT))  { mysphere.MoveLeft(200,2); } if(Keyboard.IsKeyDown(Keyboard.SHIFT))  { mysphere.MoveDown(200,2); } if(Keyboard.IsKeyDown(Keyboard.CAPSLOCK))  {    mysphere.MoveUp(200,2); } // The last bracket shows the end of the game loop. } 

You may be wondering why we put in negative numbers in the MoveRight and MoveLeft statements. The problem is that the CreateSphere command makes a sphere that is facing you, which means that the front of the sphere has right and left directions reversed relative to you (this is like what happens if a person is standing in front of you and pointing to his right; he is pointing to your left). Because we want the sphere to move to our right in the MoveRight command, we have to tell the sphere to move to its left, or negative right. Of course, it may have been a lot simpler to use the statement MoveLeft(10) when the right arrow key is pressed!

After you have finished typing in the code, please run it. You should get a big blue sphere that is controllable via the keyboard, as shown by Figure 4.1. The up and down keys should make it go forward/backward, the right/left keys should move the sphere to the sides, and the Caps Lock and Shift keys should move the sphere up and down. Already this supplies much more flexibility than the Walk command used in Chapter 3.

Want even more control of the sphere? Try adding the following statement so that you can rotate the sphere: (of course, you would have to replace myobject with mysphere below):


This command rotates an object called myobject . The parameters in the parentheses specify how much to rotate it: the first parameter denotes how far to rotate it in radians about the x axis, the second about the y axis, and the third about the z axis. Please consult Figure 4.2, which shows this rotation about the axes.

click to expand
Figure 4.2: Rotation about the three axes.

You may recall that radians are similar to degrees: there are about 57 degrees in one radian and 2 pi radians per revolution. Hence, the statement


describes a rotation of pi radians about the x axis (180 degrees), pi/2 radians about the y axis (90 degrees), and pi/4 radians about the z axis(45 degrees). The number 5 at the end specifies that the rotations are to take 5 seconds to execute. Of course, specifying less time will cause the rotation to occur more quickly.

Try adding the following code to your Sphere1 program, below the other if statements, but inside the game loop. The code will cause 180 degrees of rotation about the x, y, and z axes when the F1, F2, and F3 keys are pressed. The rotation will take four seconds to finish:

 if(Keyboard.IsKeyDown(Keyboard.F1)) {   mysphere.AddAngle(Pi/2,0,0,4); } if(Keyboard.IsKeyDown(Keyboard.F2)) {   mysphere.AddAngle(0,Pi/2,0,4); } if(Keyboard.IsKeyDown(Keyboard.F3)) {   mysphere.AddAngle(0,0, Pi/2,4); } 

When you run this code, you will see the sphere rotating about the different axes. Please do this before going on to the next project.

Elementary Game Programming and Simulators Using Jamagic
Elementary Game Programming & Simulations Using Jamagic (Charles River Media Game Development)
ISBN: 1584502614
EAN: 2147483647
Year: 2002
Pages: 105
Authors: Sergio Perez

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net