Chapter 11: Physics: Pachinko


Making things move in a realistic way is the domain of games that do simple physics modeling. Making balls bounce or cars drive are examples of things that require us to use equations given in a basic course on physics to simulate reality. Simulating inertia, friction, gravity, and impacts are ways of making a game seem more realistic. Your player will have an intuitive sense of how objects interact in a game if they mirror what they would expect to happen in nature.

To start our discussion of game physics, we need a way of describing things like forces and velocities. For this, we are going to employ the concept of vectors.

Vectors

You have probably seen vectors before in a high school algebra class or in college. If not, never fear; they are not difficult to understand.

A vector is defined as a direction with a magnitude. To illustrate , we grab our Cartesian plane again. A vector is drawn as an arrow that begins at the origin. The direction of the arrow is the direction of the vector. The length of the arrow is the magnitude of the vector. Figure 11.1 shows a sample vector.

click to expand
Figure 11.1: A vector is drawn starting at the origin and going to the point (7,6).

For example, you can think of the speed and direction that an object is moving as a vector. (Technically, this is velocity, which is covered in detail in the next section.) The direction of the movement is a line from the origin to the point at the end of the vector. The speed is the length of the line or the magnitude of the vector.

We want to keep this information in a vector because it will make the math simple when we need to alter the vector later. You can perform simple mathematical operations such as addition and subtraction on vectors. In fact, using vectors eliminates the need for trigonometry in many simple games. Because trigonometry is costly in terms of CPU usage, simplifying the math with vectors can not only make the code easier to write, but it can speed it up as well. When we do need to use trigonometry, we can still use vectors.

If we want to create a vector, we start by thinking of a point on the plane. A vector is an arrow from the origin to that point. To find the magnitude of the vector, we need to find the distance from the origin to the endpoint. The distance formula can compute that for us. Let's be formal now; the following is the definition of one possible vector:

As a convention in this book, any variable that represents a vector is presented in bold. Any variable that denotes a number remains in a normal font.

If we want to talk about the magnitude of this vector, we put double vertical bars around it, as follows :

As I've mentioned, we can find v 's magnitude by finding the distance from the origin to the point at the end of the vector. This is demonstrated in the following:

In the previous example, we're just walking through the steps with the distance formula that we've seen in previous chapters to find the length of the vector.

As you can see, each vector has two pieces of information. These are referred to as components of the vector. Each vector has an x and y component, which corresponds to the two numbers contained by the vector. In our previous example, the x component is 7 and the y component is 6.

Caution  

Don't confuse components of a vector with components in Flash MX. An entire area of Flash is devoted to components, but they are in no way related to the components of a vector.

I said before that we draw vectors beginning at the origin. That is typically the case; however, on occasion, they might be drawn elsewhere. When this happens, it becomes convenient to refer to either end of the vector. To do so, we call the point at the origin the initial point and the point at the end of the arrow the terminal point . These two terms will come up again in the following sections.

Mathematics defines a set of operations on vectors, similar to operations on numbers. Although we won't need all of these operators in this chapter's game ( Pachinko ), they will be important in many other types of games that employ simple physics, so I want to go through them all to properly prepare you.

Addition and Subtraction with Vectors

Addition is defined for vectors, which means you can add two vectors. It works like this: To add two vectors, simply add each of the components, as in the following formula:

Therefore, if we have two vectors, <2, 1> and <2, 4>, and we add them, the result is <4, 5>. You can see this visually by drawing both vectors and then moving the second vector so that its initial point is at the terminal point of the first. Look at Figure 11.2 to see what I mean.

click to expand
Figure 11.2: v1 is added to v2, and the result is v3.

Subtraction is also defined for vectors. It works the same way as addition; you simply subtract each of the components of the second vector from the components of the first, as in the following example:

You can also see this on the Cartesian plane by moving the second vector so that its terminal point is at the terminal point of the first vector, and then switching the terminal and initial points of the second vector. Figure 11.3 illustrates this.

click to expand
Figure 11.3: v2 is subtracted from v1 and the result is v3 .

Scalar Multiplication with Vectors

If you have a vector and you multiply both of its components by some number, you have scaled the vector. That is, you have changed its magnitude but not changed its direction. This comes up all the time when using vectors, so let's look at it in the form of a formula:

In this example, the value of k is called the scalar . k is a number, so it's not shown in bold. To scale the vector v by the scalar k, you multiply k into each of the components of v . Figure 11.4 shows an example with v as <2, 3> and k as 2.

click to expand
Figure 11.4: The vector v <2, 3> is multiplied by the scalar k (2).

Normalizing a Vector

We need to define a special kind of vector called a normal vector. A normal vector is any vector that has a magnitude of exactly 1. The following is a list of some normal vectors:

The concept of normalizing a vector means transforming some given vector so that its direction is the same but its magnitude is 1. You should be able to come up with a formula that does this on your own. I urge you to consider doing so before you read what follows, which is an explanation of how to normalize a vector.

Because we want to take any vector and give it a different magnitude but not a different direction, the issue is scale. The scalar multiplication we just covered can be used to do that. We need to calculate how much to scale it by to make a vector with magnitude 1. To scale anything, we need to find a ratio between the required scale and the current scale. In our case with normalizing vectors, that means we need a ratio between the required magnitude (1) and the current magnitude ( v ). We get this by using the magnitude (distance) formula as follows:

In the preceding example, x and y refer to the components of the vector. To perform the normalization, we use the preceding ratio as a scalar multiple to the vector. In other words, we multiply the ratio into each of the components. This leaves us with a normalized vector. Now let's look at it as one piece:

Figure 11.5 shows an example of normalizing a vector.

click to expand
Figure 11.5: Vector v is normalized by scaling it down so that its magnitude is 1.

Scaling a Vector

We can take the way we normalized a vector and apply it so that we can scale a vector to any arbitrary magnitude. We would simply replace 1 in the ratio with the desired magnitude. If the desired magnitude is m, we have the following formula:

In this way, normalizing a vector becomes nothing more than scaling the vector to a magnitude of 1.

Figure 11.6 shows vector v being scaled to have a magnitude of 4.

click to expand
Figure 11.6: Vector v is scaled so that its magnitude is 4.
Caution  

Scaling and normalizing vectors works only when a vector is not zero. A zero vector is a vector in which both components are 0, as in <0, 0>. A zero vector cannot be normalized and remains zero when scaled.

Vector Multiplication: The Dot Product

In general, multiplication is not defined for vectors. The act of multiplying the components of two vectors to find some new vector does not give you a new vector of any particular use. Therefore, it's not even a defined operation.

Instead of multiplication with vectors, there is a similar operation defined that takes the place of multiplication, and its name is the dot product . Before we break right into it, I need to introduce two vectors at the same time. To make the naming convention easy to follow, I'll be defining vectors as follows from now on.

Note  

In some specific instances, it actually can be useful to multiply the components of a vector to form a new vector, but you won't find it in textbooks very often. It's generally termed component-wise multiplication .

The vectors will take names such as u , v , and w and will contain components named with the same letter and a subscript number. The subscript number 1 will be used for the x component and the number 2 will be used for the y component, as follows:

Notice that the components are not bold because they represent numbers, not vectors. Now let's get back to the dot product.

The dot product takes the multiplication of the components but them adds them together, as shown in the following formula:

Notice that the dot product is written as a dot between the vectors. The result from the dot product is a number, not a vector. In other words, when you take the dot product of two vectors, the result is just one number instead of two components.

You might be saying "So, what the heck is that good for?" Well, let me tell you.

It just so happens that the dot product of two vectors is exactly equal to the cosine of the angle between the vectors, multiplied by the magnitude of each vector. In other words, given two vectors u and v , if the angle between them is theta, we have the following dot product equation:

Although this might not seem like we have a value that is any more useful than before, we can calculate an important piece of data from this. Usually when dealing with vectors, it is useful to know the angle at which vectors intersect each other. Although this value isn't the angle itself, the angle is in there. Luckily the other pieces of information that make up the dot product are elements that we know. We can calculate the magnitude of the two vectors and then factor them out. What we are left with is the cosine of the angle. Using arccosine , we can factor that out as well, and we are left with that nugget of useful information: the angle.

Let's just go through the math quickly to make sure this is clear. As you can see, the magnitudes of both vectors are multiplied to the cosine of theta. This is all equal to the dot product of the two vectors. We take the dot product and divide it by the two magnitudes and find the arccosine of the resulting number. What arccosine returns is the angle expressed in radians:

Look at Figure 11.7 to see the relationship of theta to the vectors.

click to expand
Figure 11.7: Given two vectors v and u , we can compute the angle between them using the dot product divided by the product of their magnitude.

This is a pretty nifty way to find an angle between two vectors. Can you imagine trying to do it with right triangles ?

Now that we have the essential theory of vectors, I want to briefly look at how we'll be using vectors in ActionScript in the larger context of our physics equations.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

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