Sphere9


In this game, we will continue to increase the quality, which will prepare us for our first full-blown flight simulator in Chapter 5.

First we will explore some of the extremely nice 3D objects that come with the Jamagic CD-ROM. These objects include ships, planes of all types, trees, houses , spaceships, and cars , just to name a few. They are three-dimensional, and you can view them from all angles as you would any object in the real world.

We will also add some nice effects to our game: a realistic looking water surface that moves slightly, an initial sound when the game is first turned on, better music, a jet sound when the thruster is turned on, and a voice that warns the game user when he s letting the altitude get too low. The sounds will be edited using the Jamagic sound editor. Figure 4.21 shows a screenshot of what you should expect when we are finished.

click to expand
Figure 4.21: Screenshot from Sphere9. The sphere is replaced with a spaceship imported from the CD-ROM.

The Jamagic topics you will learn in this section include the following:

  • Importing 3D objects, sounds, and music from the Jamagic CD-ROM

  • Using the Wait command

  • Tiling

  • Learning about UV Animation

  • Using the Jamagic sound editor

  • Comparing textures and materials

Importing a Spaceship into Our Game from the CD-ROM

The sphere has served us well, but it has to go! We need something that is visually more interesting. Sure, by now you know how to take an image and texture it onto the sphere ”maybe a picture of yourself or a family member ”but we want something with a more professional look.

As mentioned in the introduction to this section, the Jamagic CD-ROM comes with many beautiful 3D objects that have already been made for you. You could make something like them using MilkShape 3D, but it would take a considerable amount of work and skill. And, in addition to these objects, the Jamagic CD-ROM also has tons of interesting sounds (engines, people speaking, springs, horns, and so on) as well as some terrific music including classical, modern, jazz, and country. For the price, Jamagic is really tough to beat.

Note  

The CD-ROM that came with this book will not have all of the 3D images, sounds, and music available with the full-capability Jamagic version.

 On the CD    We will now import a 3D spaceship image ( fighter ) that comes with the book s CD-ROM and use it instead of the sphere. In the following code, you will notice that we call the new spaceship mysphere , the same name we gave the sphere in previous programs. We do this in order to minimize the changes to our previous code. The fighter will move under the same commands as the sphere, so it makes sense not to change the name.

 // Import jet oworld.Load("fighter"); mysphere= oworld.GetObject(oworld.GetNObjects()1); mysphere.Scale(0.01); mysphere.SetPosition(500,150,2000); 

As you can see, we had to scale the fighter to a smaller size because the object was originally quite large.

We now need to bring the fighter image into the program. If you have the full-capability version of Jamagic (not the limited version that came with this book), you must import the image from the CD-ROM by doing the following:

  1. From the Jamagic CD-ROM, select Jamagic11, and then go to Library. You will see several categories there: 3D, Animations, Musics, Objects, and Pictures.

  2. Select 3D. By doing so, you will have access to the numerous 3D images that come with Jamagic .

  3. Select the Space category, and then select our image fighter , followed by fighter.3ds .

  4. You will now be prompted to choose a texture directory, and fighter will be automatically selected for you from the proper directory. Choose OK and you re ready to go.

 On the CD    If you have the version of Jamagic that came with this book, you should do the following in order to load the fighter image:

  1. On the Jamagic project screen, several lines beneath Sources on the left side of your screen, you will see 3D. Right-click 3D, and then click Import.

  2. Select C:/Program Files/Jamagic/Library/3D/Space, and then select fighter .

  3. You will be prompted to choose a texture directory, and fighter will be automatically selected for you from the proper directory. Choose OK and you re ready to go.

When you return to the Jamagic screen and click 3D, you will see fighter listed. In addition, if you click Pictures, you will see that some entries have been automatically added; these are the textures that are automatically placed on your new image. You can see them by double-clicking the added pictures. Please see Figure 4.22.

click to expand
Figure 4.22: Texture pictures loaded with fighter.

For this program we will also need the mtnlow and plains 3D images that we made with MilkShape . You can import these from the previous program.

Adding More Sounds and Music

We would like to add three new sounds to the game: a voice that says Watch out! when the altitude gets too low, an introductory sound when the game first comes on, and a jet sound when the thrusters are on. In addition, we d like to add some nice music in the background.

The voice file name is called, oddly enough, watchout , and we can import it from the CD-ROM s Human Voices section of the full-capability version of Jamagic . We have already covered how to do this in a previous section, but here are the directions again briefly : right-click Sounds and select Import, then choose the Jamagic CD-ROM as your source. In the CD-ROM, go to Library, then Sounds, then Human, and select watchout .

 On the CD    If you have the Jamagic version that came with this book, then right-click Sounds and select Import. Select C:/Program Files/Jamagic/Library. You should then select Sounds, followed by Human, after which you will see the sound watchout .

The thruster sound is called aireng01 and can be found in the Sounds/Vehicles directory on the CD-ROM if you have the full-capability version; the introductory sound is called crevasse and it can be found in the Sounds/Music directory. The background music is called Lev1and3 and it can be found in the Library/Musics/Zeb directory. Make sure that you import the sounds into Sounds (a few lines below Sources) and the music into Musics.

 On the CD    If you have the Jamagic version that came with this book, you may import the crevasse sound by clicking on Sounds, then Import, and then selecting C:/Program Files/Jamagic/Library/Sounds/Music. The music Lev1and3 can be found by clicking on Musics, followed by Import, and then by choosing Program Files/Jamagic/Library/Musics/Zeb and then selecting Zeb1and2 .

Here s the code that shows the sounds and music being loaded:

 osound = New Sound(crevasse); osound.SetLoop(ON,1); osound.Play(); Wait(3000); warning = New Sound("watchout"); thruster = New Sound("aireng01"); thruster.SetLoop(ON,30); omusic = New Music("Lev1and3"); omusic.SetLoop(ON,5); omusic.Play(); 

The Wait Command

The Wait(3000) command in the preceding code tells the computer to pause for 3000 milliseconds (3 seconds). This allows the introductory music to play un- interrupted before the background music comes on. If this wasn t present the two would play at the same time. You can put a Wait() command just about anywhere in your program in order to delay the continuation of the program temporarily.

In this program, another technique is use on the thruster sound. It has to be looped 30 times to ensure that it stays on for long enough. In contrast, the warning sound is only played once.

The Jamagic Sound Editor

The background music omusic is louder than the thruster sound, so we need to raise the thruster s volume to make it audible. Jamagic comes with a sound editor that allows you to modify sounds (but not music). You can remove parts of a sound, amplify all or some of it, reverse it, and set echo effects.

Working with sounds is very easy. Just right-click aireng01 while you are in the Jamagic main screen and select Edit. You will see a graphical representation of the sound, as shown in Figure 4.23.

click to expand
Figure 4.23: Jamagic sound editor.

On the y axis you can see time, and the louder the sound is at a certain time, the further it is from the center line. The height of the sound above the center line is known as amplitude .

We will increase the amplitude (that is, raise the volume ”sometimes it s hard to resist sounding technical) using the editor. To do this, simply click Sound on the upper toolbar and select Amplify. You have two choices: + 25 % or “25 %. Since we want to raise the volume, you should select + 25 %. When you do, you will see the amplitude magically increase before your eyes. The sound clip will now be louder. To listen to the change, you can play the sound using the red arrow on the toolbar.

The sound editor also allows you to select parts of the sound clip to work on, thus allowing you to change only that part of the sound. To select parts of the sound, simply click and drag your mouse over the desired portion. The selected portion will turn white. You can now amplify or set an echo effect. You can also delete the selected portion of the sound by right-clicking it and selecting Delete.

If a sound is going to be looped, it may be better to cut its length in the editor if there will be no discernible difference at runtime. This is possible with consistent sounds like the one we require for the thruster. The reason for reducing the sound length is that sound and music files take up a lot of the computer s memory, and by now, you realize that this could result in a slow-running program. For example, the aireng01 file comes from the CD-ROM at over 700 KB and we cut it down to 200 KB in the editor with no noticeable difference when the game is run.

For fine-tuning of sounds, you can zoom-in on particular parts of the sound using the Zoom command. You will notice an icon resembling a magnifying glass on the left portion of the toolbar. If you click on the sound, the portion you clicked will be magnified, allowing you to work in detail on that section. Clicking again will magnify it even further, and right-clicking brings the magnification back down. Please see Figure 4.24.

click to expand
Figure 4.24: Zoom Feature of Jamagic sound editor.

To leave the magnification mode, just click the small white icon to the left of the Zoom icon. Selecting this icon puts you back in the Select mode, allowing you to select portions of the sound again. You can leave the editor by selecting any other choice on your screen, for example Sources.

Adding a Realistic Water Surface ”Tiling and UV Animation

The water surface from the previous game looked completely unrealistic . In this game, we are going to import a very nice looking water surface. We could import one large image to cover the entire water surface, but this would use up lots of memory ” generally the bigger the image, the greater the load on your program. Instead, we will use a small image and apply tiling to it.

Think of floor tiles and how the same design is repeated over and over. Tiling of images is very similar in that the same image is repeated many times on your screen. Repeating an image is much less burdensome to the computer, allowing for faster running programs. In fact, if you have an older computer with a clock speed less than about 200 MHz, you will probably begin to notice some speed problems now!

Let s begin. We first import the image called Water into our program. To do this, right-click Pictures and select Import. You will find the image in C:/ProgramFiles/Jamagic/Help/Tutorials/Simple/FinalProject/Pictures. Once in Pictures, select the image water .

To tile the image, we first make a texture from the image called otexture , and then apply it to the plane that makes up our water surface, as shown here:

 // Add a water texture otexture = New Texture(oworld,"water"); matplane = New Material(oworld,otexture); matplane.SetMapped(ON); myplane.SetGouraud(ON); myplane.ReplaceMaterial(matplane); otexture.SetTiling(15,15); 

The last line of this code tells Jamagic to tile otexture 15 times in the x direction and 15 times in the y direction. In other words, Jamagic will make sure that the image water is sized exactly right so that 15 water images fit in each direction. How did we know to tile it 15 times? This was a trial and error process. This result gave the best visual impression when the program was run. You are encouraged to experiment with this on your own!

We can add some very interesting effects to the texture by making it look like it is moving. We do this using UV Animation with the AnimUV command; this is a great effect for water surfaces or for any other illusion of motion. This command moves the entire texture at a rate that you can specify. Here is an example:

 matplane.AnimUV(0.0001,0.00005); 

This line tells Jamagic to move the material applied to the plane 0.0001 units to the right every 0.02 seconds and towards the viewer 0.00005 units every 0.02 seconds (the 0.02 seconds is the frequency at which the motion automatically takes place). The same sign conventions are used here as are used with the remainder of the program: to the right and towards the screen are positive directions.

In this program, we have used a fairly slow speed of motion for the water surface. You notice it only when the aircraft is not moving since the water appears to move when the aircraft moves. A faster speed could be used, but a trial and error process again guided us to select the current settings. You are encouraged to experiment by increasing this speed. You will be surprised by the intensity of the feeling of motion resulting from this feature!

You may be a little confused about when to use a texture with a material. In the preceding programs, we did not use the Texture command, although we did in the very first programs. In general, textures give you much more flexibility, and allow you to use such features as tiling and UV Animation.

We are now ready to type in our code. When you run it, you will see a very nice-looking aircraft that is followed by the camera. Our object is to land the aircraft on the landing pad, as indicated by the x and z distances on your instrument panel. The trick is not to let yourself hit the water or the islands. The game is easy once you see what s going on, and it is still a bit primitive, but nevertheless fun to use (we will go for realism in the next project!). You can make it more difficult by reducing the amount of available fuel, thus making it very important to not over-use the upwards thrusters and to steer efficiently .

Sphere 9 Code

 // // Sphere9: 9th Spherelander game. This program will //(a) Add new sound at the beginning from the Jamagic     //CD-ROM. //(b) Add voice if altitude gets too low. //(c) Make use of the wait command. //(d) Add a spaceship from the Jamagic CD-ROM. //(e) Use tiling for water and islands. //(f) Use UV Animation. //(g) Add a thruster sound. //(h) Use the sound editor. // Sounds osound = New Sound("crevasse"); osound.SetLoop(ON,1); osound.Play(); Wait(3000); omusic = New Music("Lev1and3"); omusic.SetLoop(ON,5); omusic.Play(); warning = New Sound("watchout"); thruster = New Sound("aireng01"); thruster.SetLoop(ON,30); // New Main Window owindow = New Window     ("Spherelander",640,480,Window.STANDARD);//one line owindow.SetAutoRefresh(OFF); // New World oworld = New World(0); // Import and massage the 1st 3D object made with     // MilkShape. // oworld.Load("mtnlow"); oflatland = oworld.GetObject(oworld.GetNObjects()1); oflatland.Scale(10); oflatland.SetPosition(0,0,3000); matflatland =New Material(oworld,GetRGB(100,200,0)); matflatland.SetMapped(ON); oflatland.SetGouraud(ON); oflatland.ReplaceMaterial(matflatland); oflatland.SetStatic(); // 2nd 3D object oworld.Load("plains"); mountain = oworld.GetObject(oworld.GetNObjects()1); //mountain = oworld.GetObject(0); mountain.Scale(30); mountain.SetPosition(400,0,0); matmount =New Material(oworld,GetRGB(100,200,0)); matmount.SetFlat(ON); mountain.SetGouraud(ON); mountain.ReplaceMaterial(matflatland); mountain.SetStatic(); // Import jet oworld.Load("fighter"); mysphere= oworld.GetObject(oworld.GetNObjects()1); mysphere.Scale(0.01); mysphere.SetPosition(500,150,2000); //Initialize variables. fuel = 200.; fuelleft = 200.; ftime = 0.0; fuelflag = 0; block = FALSE; upflag = FALSE; clickcheck = TRUE; Soundflag = FALSE; // New Cameras ocamera1 = New Camera(oworld,owindow); ocamera1.SetPosition(500,500,5000); // Instruments mytext6 = New StaticText     (owindow,"INSTRUMENTS",130,0,170,20);//one line mytext6.SetBackColor(GetRGB(0,0,0)); mytext6.SetColor(GetRGB(0,255,255)); fnt = New Font("ARIAL",20,Font.BOLD); mytext6.SetFont(fnt); mytext7 = New StaticText(owindow,"Remaining Fuel:",0,150,170,20); // one line mytext7.SetBackColor(GetRGB(0,0,0)); mytext7.SetColor(GetRGB(255,0,0)); mytext = New StaticText(owindow," ",0,30,110,20); mytext.SetBackColor(GetRGB(0,0,0)); mytext.SetColor(GetRGB(255,255,255)); mytext2 = New StaticText(owindow," ",0,60,110,20); mytext2.SetBackColor(GetRGB(0,0,0)); mytext2.SetColor(GetRGB(255,255,255)); mytext3 = New StaticText(owindow," ",0,90,110,20); mytext3.SetBackColor(GetRGB(0,0,0)); mytext3.SetColor(GetRGB(255,255,255)); mytext4 = New StaticText(owindow," ",0,120,140,20); mytext4.SetBackColor(GetRGB(0,0,0)); mytext4.SetColor(GetRGB(255,255,255)); mytext5 = New StaticText(owindow," ",0,170,220,20); mytext5.SetBackColor(GetRGB(0,0,0)); mytext5.SetColor(GetRGB(0,0,255)); //Buttons myButton=New Button(owindow,"Ahead",220,30,65,20); myButton.OnClick = Doahead; myButton2=New Button(owindow,"Off",220,60,65,20); myButton2.OnClick = Dostop; myButton3=New Button(owindow,"Back",220,90,65,20); myButton3.OnClick = Doback; myButton4=New Button(owindow,"Right",300,60,65,20); myButton4.OnClick = Doright; myButton5=New Button(owindow,"Left",135,60,65,20); myButton5.OnClick = Doleft; mybutton6 = New Button(owindow,"Thrust Up",     220,120,65,20);//one line mybutton6.OnClick = doup; // Create the water surface and rotate it so it's flat. myplane = oworld.CreatePlane(10000,10000); myplane.SetPosition(0,0,0); // Add a water texture. otexture = New Texture(oworld,"water"); matplane = New Material(oworld,otexture); matplane.SetMapped(ON); myplane.SetGouraud(ON); myplane.ReplaceMaterial(matplane); otexture.SetTiling(15,15); // Move the material automatically to give     // effect of moving water. // Using Anim UV matplane.AnimUV(0.0001,0.00005); myplane.SetStatic(); myplane.SetAngle(-Pi/2,0,0); myplane.SetStatic(); // Create a target plane and rotate it so it's flat. myplane2 = oworld.CreatePlane(50,50); myplane2.SetPosition(150,40,2974); matp2 =New Material(oworld,GetRGB(100,200,0)); matp2.SetFlat(ON); myplane2.SetGouraud(ON); myplane2.ReplaceMaterial(matp2); myplane2.SetAngle(-Pi/2,0,0); myplane2.SetStatic(); // Camera ocamera1.Follow(mysphere,100); oworld.Optimize(ocamera1); //oworld.Optimize(ocamera2); //Set collision detection. oworld.OnCollide = docollision; mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_STOP); // Set gravity on. mysphere.SetGravity(0.002); //Game loop While(1) { // Distance calculations // distx = (mysphere.GetX) + 150; mytext.SetText("X Distance: " + distx); disty = (mysphere.GetY) ; mytext2.SetText("Height: " + disty); if(disty<100 && Soundflag == FALSE) { // osound.SetLoop(ON,1);  warning.Play();  Soundflag = TRUE; } distz = (mysphere.GetZ)2974 ; mytext3.SetText("Z Distance: " + distz); totaldist = Sqrt(distx*distx + disty*disty +                  distz*distz);//one line totalxz = Sqrt(distx*distx + distz*distz); mytext4.SetText("x-z distance "+ totalxz); // Refresh    ocamera1.ActivateRender();    owindow.Refresh(); //fuel calculations if(fuelflag == 1) {   time2 = System.GetElapsedTime;   fueltime = time2 - ftime;   fuelleft = fuel - fueltime/1000.; } mytext7.SetText("Remaining Fuel Time: " + fuelleft); if(fuelleft<0)    {      mytext5.SetText("FUEL GONE!!");      block = TRUE;    } //End of game loop follows. } Function docollision {   if(totalxz <= 50)    {     mytext5.SetText("Congratulations! You landed          on target!");//one line     mysphere.Stop;    }    Else    {     mytext5.SetText("Back to flight school for          you!");//one line     mysphere.Stop;    } } Function doahead {    if (block == FALSE && clickcheck == TRUE)    {      ftime = System.GetElapsedTime;     mysphere.Move(5000,50);     fuelflag = 1;      clickcheck = FALSE;      thruster.Play();    } } Function doback  {    if (block == FALSE && clickcheck == TRUE)    {     mysphere.Move(5000,50);     ftime = System.GetElapsedTime;     fuelflag = 1;     clickcheck = FALSE;     thruster.Play();    } } Function doright {    if (block == FALSE && clickcheck == TRUE)    {     mysphere.MoveRight(5000,50);     ftime = System.GetElapsedTime;     fuelflag = 1;     clickcheck = FALSE;     thruster.Play();    } } Function doleft  {    if (block == FALSE && clickcheck == TRUE)    {     mysphere.MoveLeft(5000,50);     ftime = System.GetElapsedTime;     fuelflag = 1;     thruster.Play();    } } Function dostop  {     clickcheck = TRUE;     mysphere.Stop();     fuelflag = 0;     fuel = fuelleft;     thruster.Stop();   if(upflag = TRUE)    {     mysphere.SetGravity(0.004);     upflag = FALSE;    } } Function doup {    if (block == FALSE && clickcheck == TRUE)    {     mysphere.SetGravity(0.004);     ftime = System.GetElapsedTime;     fuelflag = 1;     upflag = TRUE;     clickcheck = FALSE;     thruster.Play();    } } 

We are now ready to make a full-blown flight simulator!




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