Your First Program: Moving Camera and Triangle

Here is how we will proceed to explain what you need to do to write your first program. Each line of code will be listed below, followed by an explanation of what it means. Please read carefully through the explanations, but don t begin typing in the code yet! You will be asked to do this very soon, but only after you have had a chance to understand what everything means. At the end of this section, after all of the explanations , the entire code will be given so that you can easily type it in. This is the format we will be using for the rest of the book. Here we go!

 oworld = New World(); 

This line tells the computer to create a new 3D world in which your game will take place. The world s name is oworld , but you can give it almost any name you feel like (for example, myworld , joesworld , joesgrill , and so on).

The New World part of this line must be typed in exactly as shown in order for Jamagic to understand what you are doing. So, the left part of the line above can be almost any name, and the right part must be something Jamagic already understands.

Notice that there is nothing inside the parentheses () that follow New World . We could put information inside the parentheses that described the 3D world we are creating in more detail, but there is no need to do this at this time. By leaving the parentheses empty, we tell Jamagic to use the default values for the New World statement.

Also notice that at the end of the line there is a semicolon ( ; ). Almost every line in Jamagic ends with a semicolon (this is also true in both the C and C++ languages).

 ocamera = New Camera(oworld); 

This line tells Jamagic to create a camera in our 3D world ( oworld ) and to call it ocamera . Again, the name given to the camera could have been virtually anything: nicecamera , bigcamera , candycamera , and so on. This camera is essential because you will see all of the game action through the camera s lens. As mentioned above, statements to the left of the equal sign can have just about any name, but those to the right must be recognizable to the computer. The oworld part of this line in the parentheses following New Camera is essential because it tells Jamagic in what world to put the camera. Notice that there is also a semicolon at the end of this line.

 opolygon = New Object(oworld); 

With this line, we tell Jamagic that we will be having a 3D object in our world and that the object will be called opolygon . Yes, you guessed it: we could have given almost any other name to our new object, but because we are totally unimaginative people we have chosen a boring name like opolygon . For this example, the object we will be using is a triangle. If we wanted to include other objects in the world, we would have to include lines of code like the one above to tell the computer about it.

 opoint1 = New Point3D(opolygon,300,300,0); 

Wow! That s a nasty looking piece of code, but you ll see that it s really not bad at all.

We are going to mix things up a bit by explaining the last part of this line first. Remember from the previous chapter how objects in a 3D world can be described by giving the x, y, and z coordinates of different points on the object? In this line we are describing the location of one of the points on our object ( opolygon ).

By now, you may have figured out that the name of that point is opoint1 , and yes, we could have given it any other name like getthepoint or pointless (we seem to be developing more imagination !).

The part that says New Point3D tells Jamagic that a 3D point is being made, and opolygon in the parentheses tells Jamagic that the point will be a part of the opolygon object defined in the previous line.

And now we ve arrived back at the end of the line! You have probably figured out that “300, “300,0 are the x, y, and z coordinates of the point, and you are probably feeling happy that you took the time to read and understand Chapter 2, right? If you didn t, now may be a good time to go back!

 opoint2 = New Point3D(opolygon, 300,300,0); 

This line tells Jamagic to create a second point, opoint2 , and that it will be part of opolygon . It then lists the x, y, and z coordinates of the point.

 opoint3 = New Point3D(opolygon, 0,1000,0); 

This line creates another point in the polygon.

 opolygon.AddPolygon (opoint2,opoint3,opoint1); 

Here we put all of the points together to form our triangle called opolygon . The AddPolygon portion of this line tells Jamagic to add points opoint2 , opoint3 , and opoint1 to opolygon .

Figure 3.5 shows what we now have.

click to expand
Figure 3.5: The triangle we are constructing with three posts in a 3D world.

Notice the winding order specifying which is the front face of the triangle and which is the back face. The order is counter-clockwise, so a screw turning in the same direction would come towards us. Thus the triangle s front face would then be facing us (please see Chapter 2 if you have forgotten this).


Here we put the triangle exactly where we want it in the 3D world. The center of the triangle will be at the origin (0,0,0) . If we had left this statement out, the triangle would have been drawn at the location indicated by the three points in the AddPolygon statement.


This line sets the position of our camera at (0,100, “9000) . You have to be careful where you place the camera or your object may not be visible. By default, the camera faces towards the positive side of the z-axis, as shown in Figure 3.6.

click to expand
Figure 3.6: Jamagic default camera location ”toward the positive z-axis.

This explains why we gave the camera a z coordinate of “9000. Because the camera is facing towards the positive z-axis, the camera must be placed in the negative z region to ensure that our object at z = 0 will be visible.

If we had placed the camera at (0,100,0), opolygon would not have been visible, resulting in a blank screen when you run the program.


This line enables us to move the camera around the 3D world. The Walk command makes the camera respond to the arrow keys on your keyboard, as described below:

Right and left arrows: The camera moves right or left.

Up and down arrows: The camera moves forward (toward positive z values) closer to your object, or backward (toward negative z values) further away from your object.

The motion right or left with the right and left arrows bears explaining. If you press the right arrow key, the camera will rotate to the right, much as if you were to start spinning while sitting on a chair that rotates. Eventually, your object becomes invisible when it spins out of the camera s field of view. If you were to continue holding down the right arrow key, your object would eventually reappear as the camera revolved through 360 degrees, and you ended up back where you started.

Notice the empty parentheses after the Walk command. You can adjust the speed at which the camera moves by putting in values here, but by leaving the parentheses empty, we tell Jamagic to use the default values. We will cover this more later.

 While(1) { } 

This is a very important part of the program: the game loop. Because programs do exactly what you tell them, if these lines above were not present, the following would happen:

  1. A new world would be made.

  2. A camera would be added.

  3. Three points would be made.

  4. The points would be put together to make a triangle.

  5. The triangle would be moved to (0,0,0).

  6. The camera would be moved to (0,100, “9000).

  7. The arrow keys would be given the capability to move the camera.

  8. The program would shut down.

Given the speed of today s computers, which can do millions of operations per second (if you have a 1-GHz processor, your computer can do 1,000 million operations per second), this will take a very, very short time. This makes for a pretty bad game since the game is over virtually at the same time that it starts!

We need a way to keep the game going so that the game can wait for the user to have input; in this case, this means that the user uses the arrow keys to move the camera around. The game loop keeps the program going (looping) until the user interrupts the game and tells it to stop.

The statement While(1) tells the computer to keep looping as long as (while) the user does not interrupt the program. In this case, the interruption would be caused by clicking the X icon, which will automatically appear on the upper-right side of the program screen when the program runs. Bottom line ”the program runs until the user clicks the X.

Why the two curly brackets with nothing in them? The program will keep looping through the portions between the brackets. In this case, nothing is there, but when we do more complex programs, we will want the looping to include certain commands. You will learn more about this later!

So why not include the entire program in the brackets? That is a good question. The reason is obvious if you think about it. If you looped statements like the ones that set the position of the camera or the polygon, they would be being constantly reset to those positions despite any input from the user. For example, the user would tell the camera to move back by pressing the down arrow key, but the ocamera.Set Position statement would then reset it back to (0,100, “9000), so the user s request would be completed and then immediately overridden. In addition, it may be a waste of time to repeat some statements when having the computer read them once is enough. As you will find, we need to be very concerned with not making the computer work any harder than it must in order to make the program respond as quickly as possible.

Do you notice how the brackets in the last code segment are indented a few spaces? You can start a command anywhere on a line, and many times it s useful to indent lines so that you can more easily follow your program s logic. In this case, indenting really did not help any because this loop is so simple, but you will see that this really does help keep things clear when the programs get more complicated.

The program is done! Now take a look at the entire code. Type it in very carefully, making sure you put in the semicolons at the end of the lines that require them! If you need help getting back into the source editor, please see the previous section s discussion on getting back into your code. Also, make sure you type the portions of the lines that appear to the right of the equal sign very carefully ”pay particular attention to making sure that you leave the correct number of spaces after words like New .


For your information, spaces left blank in-between commas are not important. For example 300,200 is the same as 300, 200.

 //Project1 by  your name here  // This is my first project. // We will begin very simply, and then add more and more. // 7 June 2002 oworld = New World(); ocamera = New Camera(oworld); opolygon = New Object(oworld); // Make the points. opoint1 = New Point3D(opolygon,300,300,0); opoint2 = New Point3D(opolygon,300,300,0); opoint3 = New Point3D(opolygon,0,1000,0); // Add them to the polygon. opolygon.AddPolygon(opoint1,opoint2,opoint3); //Set positions. opolygon.SetPosition(0,0,0); ocamera.SetPosition(0,100,9000); //Move the camera. ocamera.Walk(); //Loop until the user interrupts. While(1) { } 

Before you try to run your program, look it over. Chances are that you have made some form of mistake! Is there a red rectangle to the left of any line? If there is, this means that there is some kind of error in that line. Compare the code you have typed in with the one listed here. Did you forget to put in the semicolons where required?

Bugs , or errors, are a part of programming, and if you were to never have any, you would be the first in the history of programming. Expect errors and look your code over very carefully!

If you try to run your program with any errors in it, the program just won t run, and it will indicate to you where the first error is located in your code. Don t worry, you cannot hurt the computer by making typing errors in your code, the program just won t run until they are fixed.

Now that you have checked for errors, you are ready to run the program. To do so, look in the upper-left corner of your screen. You will see what looks like a red figure running. To run you program, double-click this Run icon, wait a second, and your game should start.

When the program begins, you should see a blue triangle in front of you (blue is the default color, and it was used because we did not specify which color to use). It s the triangle we built with the three points. Now experiment a bit with the arrow keys. Note how the arrow keys move you in or out, left or right. Try holding down the right arrow key until the object disappears and then reappears on the left side of your screen. Not too bad considering that we only had a few lines of code. If you were programming in C++ and were accessing DirectX directly, you would have had many, many more lines of code, and learning how to do a program like this would have taken months of study. This is the beauty of Jamagic .

When you were typing in your code, you may have noticed a couple of things. When you first type in a line, a red rectangle appears to the left of the line until the line is correct (in a format understood by Jamagic ). As mentioned earlier, you can use this visual clue to determine whether you have some bug (error) in your code.

You also may have noticed that the text is black, but that sometimes the black turns blue. This happens when Jamagic recognizes commands like AddAngle , New or World . In this way Jamagic lets you know that you are typing in commands properly.

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 © 2008-2017.
If you may any questions please contact us: