Sphere3


In this program, we will study the following:

  • SetStatic and Optimize commands

  • Collision detection

  • Functions

  • Hide and Show commands

These items will allow us to instruct the computer to check for collisions between objects, and they will make one of the 3D objects invisible should a collision occur. In addition, we will be able to make objects reappear if a button is clicked. Figure 4.4 shows an example of this occurring.

click to expand
Figure 4.4: Sphere3 screenshot. Collision detection activated. The use of functions introduced.

The SetStatic Command

A static object is one that you designate as static in your program; that is, that the object does not move. For example, the statement

 sphere2.SetStatic(); 

would tell the computer that  sphere2 will not move. We do this in order to simplify the process for the computer when the program runs. If an object is specified as static, then the computer can ignore it when it is making certain calculations, which allows the program run faster. If any objects are specified as static, you must insert the following statement anywhere after the SetStatic commands:

 myworld.Optimize(mycamera); 

This statement tells the computer to optimize the calculations in myworld , putting the earlier SetStatic command into effect. Without optimizing the camera, using the SetStatic command has no benefit.

Collision Detection

Collision detection refers to the process of determining if two objects are touching each other. You will agree that collision detection is crucial for game creation. For example, without collision detection, you would have no way of knowing if a missile hits a target.

Collision detection can be expensive from a computational point of view in that if done unwisely, it could slow down your program. Fortunately Jamagic makes this task simple for you.

Why is collision detection time-consuming ? Consider how you would determine visually if two objects are touching, for example, a baseball and a bat frozen in time and pretty close to each other. If you were viewing the scene from the pitcher s viewpoint, it would not be easy for you to be sure if the two are touching ”you would have to see it from several different angles to be sure. It sounds complicated when you break it down, and it is, but it s even harder for the computer.

Visually, it s easy for you to check only the important parts of the bat and ball for contact. For example, why bother checking the back of the bat or the back of the ball if only the surfaces facing each other will come in contact? The computer, unless it has some algorithm (or method) for determining which triangles of the bat are close to which triangles on the ball, needs to check them all for collision. If there is some algorithm for determining which triangles to check, it still has to check a large number of triangles for collision since the program realistically cannot know exactly which two triangles to check; in addition, the algorithm itself is probably time-consuming.

Now that you know how complicated this is for two objects, consider checking for collision between three or more objects: say the pitcher, the bat, and the ball. The program would then have to check, at very close intervals in time, when all three are relatively close to each other and when the triangles on each object might collide. Perhaps you now have a feeling for how time-consuming these calculations can become, especially with objects that have lots of polygons in them for better appearance.

Jamagic does some of the work for you by only checking for collisions between objects that are in the same predetermined three-dimensional zones. You can further reduce the computational burden on your computer by writing your code so that the program checks for collisions only for nonstatic objects (objects that you have not declared as static) that matter. For example: if it s not important to the outcome of a game if a person collides with other objects in the game, then do not enable collision detection for that person.

Here are some of the commands you will need to know. They deal with detecting collisions between an object called mysphere (or any name ) and any static object (that is, an object you specified as static):

 mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_SLIDE); 

This command tells Jamagic that collision detection is activated ( TRUE ) for the object mysphere , and that if mysphere collides with any static object, it should slide along the boundary of the object it collides against. Alternately we could say

 mysphere.SetCollision(TRUE,Object.COLLISION_TYPE_STOP); 

In this case, mysphere colliding with a static object will result in mysphere stopping its motion. You would then only be able to move it in a different direction than the one that caused the collision.

If you want to consider collisions between mysphere and any other nonstatic object, then you need another SetCollision statement, like the following, for the other object:

 mysphere2.SetCollision(TRUE,Object.COLLISION_TYPE_SLIDE); 

Functions

In this program and those that follow, we will use functions extensively, which we will now explain. Functions are sets of instructions that tell the program what to do, and they are super-important because they are so useful. In fact, functions are used in just about every programming language there is.

Consider the following statement:

 oworld.OnCollide = docollision; 

This command tells the computer that if any collision occurs in our world, then it should do whatever the docollision function tells it to do. The docollision is a part of your program that you will add to the game loop.

Let s say that you were building a model car with four wheels, and that constructing the wheels was a pretty long process. Instead of listing the instructions for building the wheels four times, it would make sense to list them just once. Each time you get to a part where you need to build a wheel, you could then simply refer the reader to the wheel-building instructions.

A function is very similar. It is a set of instructions that, in order to save space and conserve effort, you do not feel like repeating over and over. It makes sense to use a function to specify what action will be taken when objects collide, especially if more than two objects are being checked for collision or if collisions can occur many times.

How and where do you write the instructions in your function? In Jamagic, typically you will put them outside of the game loop, at the very end of your program. Here s the code that contains the docollision function :

 Function docollision {      mysphere.Hide;      mysphere.ReplaceMaterial(mat2); } 

Recall that this function is called when there is any collision. The brackets designate where the function begins and where it ends.

Incidentally, did you notice how the function code is indented? This is not necessary, but it makes it easier to determine which parts are inside the loop. When the projects become more complicated, indenting will be a big help in deciphering your own code.

Hide and Show Commands

When the function is called it will do two things: make mysphere invisible using the Hide command, and change the material applied to the surface of mysphere to mat2.

The Hide command does not destroy the sphere; it merely makes it invisible. It s possible to make the object reappear using the myobject.Show statement, as illustrated below:

 if(Keyboard.IsKeyDown(Keyboard.F4))  {  mysphere.Show;  } 

Before you type in the program, you should know that sometimes you may want to send some information to the function you are calling ”say the speed or position of an object. In this case, you might include that information (or arguments) in parentheses after the code for the docollision function. However, there is no need to do this if the variables you want to send to or from the function are values that have been initialized (or given a value to) earlier in the program. More on this later.

Note  

In the following code, please note that we have modified the Move commands to make the objects move more quickly.

Please type in the code below carefully . You may find that you are better off just modifying the previous program:

Sphere3 Code

 // // Sphere3: Third Spherelander game. Will add collision // detection // Hide Command, Functions // Upon collision of two spheres, 1st sphere will disappear, // and will // change color. // Set static command. // Optimize camera command. // Create a new world with a new camera. oworld = New World(); ocamera = New Camera(oworld); // 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");// should go with previous 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; oworld.Optimize(ocamera); //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); // Camera ocamera.SetPosition(0,0,3000); //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;  } } Function docollision {      mysphere.Hide;      mysphere.ReplaceMaterial(mat2); 

}

Please run the program and experiment with it. You will see that the program has done exactly what we ve told it to do. When a collision is detected between the moving sphere and a static object, the moving sphere becomes invisible. When you press the F4 key, mysphere becomes visible again, but the collision has altered its color. You might want to note the difference between the moving sphere s color and that of the stationary sphere after the collision. They both use the same material, but the moving sphere still has Gouraud shading activated.




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