Sphere4


In this project we will learn about:

  • Fog

  • Quitting the program using any key

  • Adding and rotating a plane

Using fog, you can develop some interesting depth effects. For instance, as you move the sphere away into the fog, you will notice that it becomes more and more difficult to see, until it s entirely invisible. On the other hand, objects that come toward you out of fog give you a very realistic perception of decreasing distance that increasing the size alone cannot give.

We will also add a plane surface to our object. Planes are two-dimensional, and are great for establishing apparent surfaces. Pay attention to this discussion because we will be landing on a surface like this one in a soon-to-come project!

Figure 4.5 shows how fog and planes can be used.

click to expand
Figure 4.5: Sphere4 project using fog and planes.

Fog

The fog will be added with the statements discussed here. Once again, the statement will be given first and an explanation will follow.

 ocamera.EnableFog(ON); 

This statement tells the computer to put a fog effect on our camera ocamera.

 ocamera.SetFogColor(GetRGB(30,0,0)); 

You can figure this one out yourselves ”we are setting the color of the fog using the RGB scale.

 ocamera.SetFogMaxZPosition(5000); 

This command tells the computer that nothing will be visible that is more than 5000 units from the camera.

 ocamera.SetFogMinZPosition(3000); 

This command establishes when the fog will first become visible. In this case, no fog effects are visible unless an object is beyond the specified distance of 3000 units. Of course, numbers other than 5000 or 3000 must be used, but just remember that the MaxZ value must always be greater!

For objects that are between MaxZ and MinZ , Jamagic makes them hazier the further they are from the camera. In other words, objects close to MinZ will be more clearly visible, while those close to MaxZ will be almost invisible.

Ending the Game with the Esc Key

We also want to add a feature that will allow us to terminate the game using the Esc (Escape) key. This is easily done with the following self-explanatory statements:

 if(Keyboard.IsKeyDown(Keyboard.Escape)) {    End;   } 

The CreatePlane Command

We will be adding a two-dimensional plane with the following commands:

 myplane = oworld.CreatePlane(500,500); 

With this command we create a plane, myplane , and we place it in our world, oworld . The dimensions of the plane are (500, 500).

 myplane.SetPosition(0,200,0); 

With this command, we locate the plane s center at (0, “200, 0).

 matp =New Material(oworld,GetRGB(0,100,50)); 

Here we create a new material surface that we will apply to our plane.

 matp.SetFlat(ON); myplane.ReplaceMaterial(matp); 

In these code lines, we apply the material in a flat mode and instruct Jamagic to put the material onto myplane .

Now we need to change the orientation of the plane. By default, the plane gets made as if it were lying on the x and y axes. In other words, the plane will be like a picture hanging in front of you, as you look from the negative z axis.

We want to make the plane seem like a floor surface, so we need to rotate the plane through pi/2 radians (90 degrees) in the negative direction, rotating it about the x axis. We change the plane s angle with the following command:

 myplane.SetAngle(-Pi/2,0,0); 

After rotating the plane, if we also wanted to change the angle of the plane about the y or z axes, we could do so using the last two parameters in the parentheses.

Finally we make the plane a static object with the following command:

 myplane.SetStatic(); 

Please type in the program carefully and run it. Observe what happens when the sphere gets far enough away ”it should become more and more difficult to see, until it is totally invisible. In Figure 4.5, the spheroid far off on the right seems hazy from the start of the program , as does the static sphere. This is because they are both between the MinZ and MaxZ values.

Sphere4 Code

 // // Sphere4: 4th Spherelander game. Will add fog commands // to previous program. Beware to add fog in program before //adding objects // to program, or the fog will not be applied to objects // until moved. // Distances in fog commands are relative to the camera. // //Will add a plane and rotate it down - pi/2 radians   // about //the x axis. 
 // Create a new world with a new camera. oworld = New World(); ocamera = New Camera(oworld); 
 // Fog commands ocamera.EnableFog(ON); ocamera.SetFogColor(GetRGB(30,0,0)); ocamera.SetFogMaxZPosition(5000); ocamera.SetFogMinZPosition(3000); 
 // Create a sphere with Gouraud shading. mysphere = oWorld.CreateSphere(100,100,100,10,10); mysphere.SetPosition(0,0,0); omaterial = New Material     (oworld,GetRGB(50,150,200),"lightblue");//one line mysphere.SetGouraud(ON); mysphere.ReplaceMaterial(omaterial); //Create a 2nd sphere with flat solid color. sphere2 = oworld.CreateSphere(100,100,100,10,10); sphere2.SetPosition(300,100,1000); mat2 = New Material(oworld,GetRGB(0,200,0),"green"); mat2.SetFlat(ON); sphere2.ReplaceMaterial(mat2); sphere2.SetStatic(); //Create a 3rd sphere with flat mesh. ball3 = oworld.CreateSphere(100,50,50,10,5); ball3.SetPosition(500,500,1000); mat3 = New Material     (oworld,GetRGB(50,150,200),"lightblue");//one line ball3.ReplaceMaterial(mat3); ball3.SetStatic(); // Create a plane and rotate it so it's flat. myplane = oworld.CreatePlane(500,500); myplane.SetPosition(0,200,0); matp =New Material(oworld,GetRGB(0,100,50)); matp.SetFlat(ON); myplane.ReplaceMaterial(matp); myplane.SetAngle(-Pi/2,0,0); myplane.SetStatic(); // Camera ocamera.SetPosition(0,0,3000); oworld.Optimize(ocamera); //Set collision detection on. oworld.OnCollide= docollision; mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_STOP); //Game loop While(1) { if(Keyboard.IsKeyDown(Keyboard.UP))  {    mysphere.Move(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.DOWN))  {    mysphere.Move(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.RIGHT))  {    mysphere.MoveRight(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.LEFT))  {    mysphere.MoveLeft(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.SHIFT))  {    mysphere.MoveDown(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.CAPSLOCK))  {    mysphere.MoveUp(100,0.5);  } if(Keyboard.IsKeyDown(Keyboard.F4))  {    mysphere.Show;  } if(Keyboard.IsKeyDown(Keyboard.ESCAPE))  {    End;  } // Game loop end follows. } Function docollision {     mysphere.Hide;     mysphere.ReplaceMaterial(mat2); } 



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