Making More Complicated Shapes Using Triangles

Making More Complicated Shapes Using Triangles

If games consisted of triangles moving across the screen, we could have finished this chapter already. But as you know, life is a lot more interesting than that. In this section, we will learn how to make more complicated shapes using the triangle as our basic building block. Triangles are used in Jamagic to construct all 2D and 3D forms. We will first make 2D surfaces, followed by 3D surfaces.

Please examine the rectangle shown in Figure 3.12, placed on a 3D coordinate system. This rectangular shape is composed of two triangles. Triangle 1 consists of points 1, 2, and 3 and Triangle 2 is composed of points 3, 4, and 1 (did you notice we gave the same winding order for both, and that they would both be visible to you from your position since the triangles would screw out toward you?).

click to expand
Figure 3.12: Rectangle made of two triangles.

We will make a completely new program now to construct our rectangle. Let s call it Project1b. If you have forgotten how to make a new program, please go back and review the early portion of this chapter.

In the following pages, we explain each line of the code; much of it will already be familiar to you! As before, take the time to understand everything we are doing and wait to add the new code until later on, when we list the entire program.

 oworld = New World(); ocamera = New Camera(); oobject = New Object(); 

These first three lines are pretty similar to those in our first program. However, we are making a new 3D world called oworld , a new camera called ocamera , and a new object called oobject . The only difference is that we are calling our new object oobject instead of opolygon .

Next , we will specify the four points necessary to define the two triangles that will compose our rectangle. Here is the first point:

 Point1 = New Point Array(5,100,100,0,0,0); 

This line creates a point called Point1 (which you could have called almost anything else). The line New Point Array tells Jamagic that we are creating a point that will be part of a polygon. An array is a list of parameters or numbers . In this case, the parameters are in the parentheses. We also could have made the points exactly as we did in the previous program using the New Point3D statement. The advantage of using the array is that you can re-use it later to draw other points at the same location. The New Point3D statement actually makes a point, the point array is a location of a point.

Let s skip over the first parameter inside the parentheses (5) and move to the second, third, and fourth parameters: “100, “100, 0. These three numbers give the x, y, and z coordinates of the point.

The last two parameters are the u, v mapping coordinates. They specify that Point1 will be associated with the lower-left corner of the texture.

If we were not interested in adding a texture to our object, we would have left the last two parameters out of the parentheses. If this were the case, you might notice that there would only be three parameters inside the parentheses. With the u, v coordinates, there are five parameters inside the parentheses. Hence the first parameter, 5, would have been a 3 if the u, v coordinates were left out.

The next three statements specify the remaining three points and their u, v coordinates:

 Point2 = New Point Array(5,100,100,0,1,0); Point3 = New Point Array(5,100,100,0,1,1); Point4 = New Point Array(5,100,100,0,0,1); 

The two resulting triangles are those that were illustrated in Figure 3.12.

We now need to tell Jamagic to put the four points into our object, oobject , as shown here:

 oobject = AddPolygon(Point1,Point2,Point3); oobject = AddPolygon(Point3,Point4,Point1); 

This may seem a little mysterious . Why not put them all in at one time? The answer is that, in order to add shapes to an object, they must be added as individual triangles.

The last lines of code remain unchanged from the first program: they apply the texture, allow the rectangle to move using the Walk command, and establish the game loop.

There s just one more detail to take care of before you can run the program. This program does not have the image Firstpic in it, so you need to import it in. There is no need to redraw it since we have already done it. However, if you wished to have another texture on this new image, you will have to import another one or draw another one.

To import the image from the first program, right click Pictures and then click Import. In the Projects window select Hard Disc (C:) (you will need to use the scroll bar to get to it), then select Program Files, then Ja-magic , then Projects, and then double-click Project1 (which is where our picture lives).

Once in Project1, select Pictures, then double-click our image, Firstpic . The image will be added to our program.

Now that we have imported the image to our project, it is time to type in the code. Here it is, complete. You may wish to use cut-and-paste (to cut-and-paste, first highlight the lines you wish to copy by holding the Shift key while pressing the arrow keys, then press Ctrl + C to copy, and then Ctrl + V to paste the copied lines wherever you wish) from your previous program, but at this point, it s probably best for you to get more familiar with the code by doing the keyboard entry yourself!

 //Program for making a square using triangles // Create the world. oworld = New World(); ocamera = New Camera(oworld); // Create an empty object. oobject = New Object(oworld); // Create several points for polygons (with UV). Point1 = New PointArray(5,100,100,0,0,0); Point2 = New PointArray(5,100,100,0,1,0); Point3 = New PointArray(5,100,100,0,1,1); Point4 = New PointArray(5,100,100,0,0,1); // Create three polygons with these points. oobject.AddPolygon(Point1,Point2,Point3); oobject.AddPolygon(Point1,Point3,Point4); otexture = New Texture(oworld,"Firstpic"); omaterial = New Material(oworld,otexture); // Add the new material to our object. oobject.ReplaceMaterial(omaterial); ocamera.SetPosition(0,0,700); oobject.Walk(); // Wait for the user to close the window. While(1) { } 

Now run it. The image should be perfectly mapped onto the rectangle, and the rectangle should respond to the arrow keys, as before (see Figure 3.13).

click to expand
Figure 3.13: The image mapped onto the rectangle.

 On the CD    If you are experiencing any difficulties, please check your code carefully . Did you type the commands exactly as specified? Did you forget to finish a line with a semicolon? Don t forget that Jamagic will show you where your errors are with the small red square to the left of the offending code. If you wish, you may load this program from the CD-ROM that came with the book.

If you think about it, you can now make some more complicated shapes using the AddPOlygon command.

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: