The Point Defined

 <  Day Day Up  >  

In the world of game programming, you will deal with many different types of functions. In general, a function is a rule that takes in information (input) and returns new information (output). In programming, you can write functions that take in various types of input, such as variables , text, or numbers , and return a different type of information or even perform an action as output. In a well-written program, you should compartmentalize repeatable tasks in functions so that the code is kept simple and clean. Let's take a look at an example of a well-commented and useful function:

 // purpose: determine whether a number is a power of 2 // input: the number being checked // output: true if it's a power of 2, else false bool powOfTwo(int num) {     return !(num & (num  1)); } 

This function is passed in any integer and returns a true or false answer on whether the number is a power of 2 ”for example, 16, 32, 64, ....

However, in mathematics a function is limited to just numbers: It takes in a number, applies a rule to it, and then returns a new number. There are two ways to define a function in mathematics. First, you could list a set of ordered pairs consisting of an input followed by its corresponding output. For example, the following ordered pairs define a function: (0,0), (1,2), (2,4), (3,6), and (4,8). You also might have seen this written in tabular form, as shown in Table 1.1.

Table 1.1. Ordered Pairs in Tabular Form

x

y

1

2

2

4

3

6

4

8

Although setting up a lookup table like this is simple and fast, this approach has some problems. If you feed a number into the tabular function and no entry is listed for it, you get an error message. Looking at Table 1.1, what would happen if you had an input of 5? There's no entry for an input of 5, so the computer wouldn't know what to do with it. Related to that is another issue with using a lookup table: An entry must exist for every possible input, and that could lead to a very large table that takes up a lot of storage. This leads to a better method.

The second way to define a function is by using a formula , or an equation that relates two numbers. The function just discussed can also be written as y = 2 x , where x is the input and y is the output. Try plugging each of the ordered pairs into the equation.

Does it work? Then clearly both definitions describe the same function. The benefit of using this second approach, though, is that it accommodates any numeric input, not just the five numbers listed in the table. With a function that can take any real number as input, this approach uses a great deal less storage space than a table.

Now let's look at an area in which functions are commonly used: putting things on the screen. When you write game programs, often you ask the computer to place objects on the screen, but how does the computer know where to put them? Traditionally, game programmers use the Cartesian coordinate system to pinpoint any particular location on the screen. The Cartesian coordinate system consists of a horizontal x-axis and a vertical y-axis. Each individual point can be defined by an ordered pair with an x-coordinate followed by a y-coordinate, written as ( x , y ). The origin is the point where the two axes intersect; its coordinates are (0,0). From the origin, positive x values are to the right, and negative x values are to the left. Likewise, positive y values go up, and negative y values go down.

Example 1.1: Graphing 2D Locations

You want to place six objects (A through F) on the screen. Graph their locations on a Cartesian coordinate system: A(0,0), B(1,2), C(4,3), D(“1,2), E(“2, “1), and F(3, “2).

Solution

The points are graphed in Figure 1.1.

Figure 1.1. Points A through F graphed on the Cartesian coordinate system.

graphics/01fig01.gif

Now you can use functions to move objects around on the screen based on their x and y coordinates.

NOTE

Be careful not to confuse the Cartesian coordinate system with screen coordinates. Notice that the positive y-axis points up on the Cartesian system. Unfortunately, monitors have been set up to read top to bottom, so screen coordinates use down for the positive y direction. See Figure 1.2.

Figure 1.2. The screen coordinate system.

graphics/01fig02.gif


In the overall rendering pipeline, the last step is to convert from Cartesian coordinates to screen coordinates. Chapter 6, "Transformations," addresses how to do that. For now, keep in mind that everything you do will be within the Cartesian coordinate system, meaning that positive y is up.

This same coordinate system can be extended to 3D as well. All you have to do is add a third axis: the z-axis. Unfortunately, there are no industry standards at this point. Some packages use a y -up world, and some use a z -up world. Some engines use a right-handed system, and others use a left. What does all this mean? In the end it's quite arbitrary what you name each axis. Just be clear on what labels you are using for each individual project. This book uses the y -up, right-handed coordinate system, as shown in Figure 1.3.

Figure 1.3. The y-up, right-handed coordinate system.

graphics/01fig03.gif

Notice in this system that the positive x-axis is to the right, the positive y-axis is up, and the positive z-axis comes out of the screen toward the viewer. The y -up name is quite obvious, but the right-handed label might not make much sense. Using your right hand, fold your pinky and ring fingers down. Point your thumb to the right (positive x direction) and your pointer finger up (positive y direction). You'll see that your middle finger comes forward (positive z direction). Try doing the same thing with your left hand. If your left thumb points to the right (positive x direction) and your pointer finger is up (positive y direction), your middle finger is forced to point backward (positive z direction for the left-handed system). I've chosen to use the y -up, right-handed system for several reasons:

  • It's the system used in traditional mathematics.

  • It's the system used by a majority of programmers (but not all).

  • It's the system used by OpenGL.

Using this system, you can pinpoint any location in 3D space by giving its coordinates as an ordered triple ( x , y , z ).

Example 1.2: Graphing a 3D Location

Give the coordinates of point P pictured in Figure 1.4.

Figure 1.4. A 3D point P.

graphics/01fig04.gif

Solution

To get to point P, you must travel two units to the right, four units up, and five units forward, so the point can be described by the ordered triple (2,4,5).

Traditional mathematicians use the parentheses notation. However, you might see some programmers use notations such as <2,4,5> or [2 4 5] to represent the same point. We'll discuss these notations more in Chapters 4, "Vector Operations," and 5, "Matrix Operations." In code, there is also more than one way to store coordinates. Many programmers will prefer to use simple data types, while others will choose the lengthy (though powerful) object-oriented method and wrap the points in a class or structure.

 // Some various data types which can be used to store points // 1. an array of floats.  The advantage lies in speed, ease, //    and low memory cost float 3dPoint[3]; // 2. a structure holding 3 floats. The advantage lies in the //    ability to overload operators and define functions struct 3dPoint {     float x, y, z;     // The overloaded addition operator     3dPoint operator+(const 3dPoint &P2) const     {         3dPoint temp = {    this->x + P2.x,                      this->y + P2.y,                      this->z + P2.z                     };         return temp;     } }; 

Using this structure, adding two points is as simple as follows :

 3Dpoint A, B, C; C = A + B; 

Also, it is a good idea to get into the habit of thinking of coordinate systems as they appear in games. In almost all 2D games, the world's coordinate system is defined by the origin, (0, 0), appearing at the top-left corner of the screen with the x and y values increasing to the right and the bottom. Generally , screen coordinates are in pixels, so if your game is running in 800x600 resolution, your screen coordinates would extend 800 right along the X axis and 600 down along the Y axis. In 3D games, it is important to remember that most games are written facing down the negative Z-axis, and the size of the units is generally pre-determined by whatever API you happen to be using.

2D points are represented by an ordered pair ( x , y ).

3D points are represented by an ordered triple ( x , y , z ).


Self-Assessment

1.

Give the coordinates of the 2D points A through E shown in Figure 1.5.

Figure 1.5. 2D points A through E.

graphics/01fig05.jpg

2.

Plot the following points on the grid provided in Figure 1.6: A(3,1), B(“3,1), C(0,2), D(“2,0), E(“1, “2).

Figure 1.6. A blank grid.

graphics/01fig06.jpg

3.

Give the coordinates of 3D point P shown in Figure 1.7.

Figure 1.7. 3D point P.

graphics/01fig07.gif

4.

Give the coordinates of 3D point Q shown in Figure 1.8.

Figure 1.8. 3D point Q.

graphics/01fig08.gif


 <  Day Day Up  >  


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net