Here is a breakdown of the major topics in this chapter: I Understanding the various fields of artificial intelligence I Using deterministic algorithms I Recognizing finite state machine
Trang 1Introduction to
A rtificial Intelligence
chapter 18
Probably the thing I dislike most about some games is how the computer cheats I’m
playing my strategy game and I have to spend 10 minutes finding their units while
they automatically know where mine are, which type they are, their energies, and so
on It’s not the fact that they cheat to make the game harder, it’s the fact that they cheat
because the artificial intelligence is very weak The computer adversary should know just
about the same information as the player If you look at a unit, you don’t see their health,
their weapons, and their bullets You just see a unit and, depending on your units, you
respond to it That’s what the computer should do; that’s what artificial intelligence is all
about
In this chapter I will first give you a quick overview of several types of artificial
intelli-gence, and then you will see how you can apply one or two to games In this chapter, I’m
going to go against the norm for this book and explain the concepts with little snippets of
code instead of complete programs The reason I’m doing this is because the
implemen-tation of each field of artificial intelligence is very specific, and where is the fun in watching
a graph give you the percentage of the decisions if you can’t actually see the bad guy hiding and cornering you? Complete examples would basically require a complete game!
For this reason, I will go over several concrete artificial intelligence examples, giving only
the theory and some basic code for the implementation, and it will be up to you to choose
the best implementation for what you want to do
Here is a breakdown of the major topics in this chapter:
I Understanding the various fields of artificial intelligence
I Using deterministic algorithms
I Recognizing finite state machines
I Identifying fuzzy logic
Trang 2I Understanding a simple method for memory
I Using artificial intelligence in games
The Fields of Artificial Intelligence
There are many fields of artificial intelligence; some are more game-oriented and othersare more academic Although it is possible to use almost any of them in games, there are
a few that stand out, and they will be introduced and explained in this section
Expert Systems
Expert systems solve problems that are usually solved by specialized humans For example,
if you go to a doctor, he will analyze you (either by asking you a set of questions or doingsome analysis himself), and according to his knowledge, he will give you a diagnosis
An expert system could be the doctor if it had a broad enough knowledge base It wouldask you a set of questions, and depending on your answers, it would consult its knowledgebase and give you a diagnosis The system checks each of your answers with the possibleanswers in its knowledge base, and depending on your answer, it asks you other questionsuntil it can easily give you a diagnosis
For a sample knowledge tree, take a look at Figure 18.1 As you can see, a few questionswould be asked, and according to the answers, the system would follow the appropriatetree branch until it reached a leaf
A very simple expert system for a tor could be something like the fol-lowing code Note that this is all justpseudo-code, based on a fictional
doc-scripting language, and it will not compile in a compiler, such as Dev-C++
or Visual C++ This is not intended to
be a functional example, just aglimpse at what an expert system’sscripting language might look like
Answer = AskQuestion (“Do you have a fever?”);
if (Answer == YES)
Answer = AskQuestion (“Is it a high fever (more than 105.8 F)?”);
Chapter 18 I Introduction to Artificial Intelligence
564
Figure 18.1 An expert system’s knowledge tree
Trang 3As you can see, the system follows a set of questions, and depending on the answers, either
asks more questions or gives a solution
n o t e
For the rest of this chapter, you can assume that the strings work exactly like other variables, and
you can use operators such as = and == to the same effect as in normal types of variables
Fuzzy Logic
Fuzzy logic expands on the concept of an expert system While an expert system can give
values of either true (1) or false (0) for the solution, a fuzzy logic system can give values
in between For example, to know whether a person is tall, an expert system would do the
following (again, this is fictional script):
The Fields of Artificial Intelligence 565
Trang 4Answer = AskQuestion (“Is the person’s height more than 5’ 7”?”);
A fuzzy set would appear like so:
Answer = AskQuestion (“What is the person’s height?”);
if (Answer >= 5’ 7”)
Solution = “The person is tall.”;
end if
if ((Answer < 5’ 7”) && (Answer < 5’ 3”))
Solution = “The person is almost tall.”;
end if
if ((Answer < 5’ 3”) && (Answer < 4’ 11”))
Solution = “The person isn’t very tall.”;
As you can see from the graph, for heights greater than 5' 7", the function returns 1; forheights less than 4' 11", the function returns 0; and for values in between, it returns thecorresponding value between 5' 7" and 4' 11" You could get this value by subtracting the height from 5' 7" (the true statement) and dividing by 20 (5' 7"–4' 11", which is thevariance in the graph) In code, this would be something like the following:
Chapter 18 I Introduction to Artificial Intelligence
566
Figure 18.2 Fuzzy membership
Trang 5Answer = AskQuestion (“What is the person’s height?”);
You might be wondering why you don’t simply use the equation only and discard the if
clauses The problem with doing so is that if the answer is more than 5' 7" or less than 4'
11", it will give values outside the 0 to 1 range, thus making the result invalid
Fuzzy logic is extremely useful when you need reasoning in your game
Genetic Algorithms
Using genetic algorithms is a method of computing solutions that relies on the concepts
of real genetic concepts (such as evolution and hereditary logic) You might have had a
biology class in high school that explained heredity, but in case you didn’t, the field of
biology studies the evolution of subjects when they reproduce (Okay, maybe there is a
lit-tle more to it than that, but you are only interested in this much.)
As you know, everyone has a blood type, with the possible types being A, B, AB, and O,
and each of these types can be either positive or negative When two people have a child,
their types of blood will influence the type of blood the child has All that you are is
writ-ten in your DNA Although the DNA is nothing more than a collection of bridges between
four elements, it holds all the information about you, such as blood type, eye color, skin
type, and so on The little “creatures” that hold this information are called genes.
What you might not know is that although you have only one type of blood, you have two
genes specifying which blood type you have How can that be? If you have two genes
describing two types of blood, how can you have only one type of blood?
Predominance! Certain genes’ information is stronger (or more influential) than that of
others, thus dictating the type of blood you have What if the two genes’ information is
equally strong? You get a hybrid of the two For the blood type example, both type A and
type B are equally strong, which makes the subject have a blood type AB Figure 18.3
shows all the possible combinations of the blood types From this table, you can see that
both the A and B types are predominant, and the O type isn’t You can also see that
posi-tive is the predominant type
So, how does this apply to the computer? There are various implementations that range
from solving mathematical equations to fully generating artificial creatures for scientific
The Fields of Artificial Intelligence 567
Trang 6research Implementing a simple genetics algorithm in the computer isn’t difficult Thenecessary steps are described here:
1 Pick up a population and set up initial information values
2 Order each of the information values to a flat bit vector
3 Calculate the fitness of each member of the population
4 Keep only the two with the highest fitness
5 Mate the two to form a child
And thus you will have a child that is the product ofthe two best subjects in the population Of course, tomake a nice simulator you wouldn’t use only two ofthe subjects—you would group various subjects ingroups of two and mate them to form various chil-
dren, or offspring Now I’ll explain each of the steps.
You first need to use the initial population (all the jects, including creatures, structures, or mathematicalvariables) and set them up with their initial values.(These initial values can be universally known infor-mation, previous experiences of the subject, or com-pletely random values.) Then you need to order theinformation to a bit vector, as shown in Figure 18.4
sub-Although some researchers say that animplementation of a genetic algorithmmust be done with bit vectors, others saythat the bit vectors can be replaced by afunction or equation that will analyzeeach gene of the progenitors and generatethe best one out of the two To be consis-tent with the DNA discussion earlier,
I will use bit vectors (see Figure 18.4).You now have to calculate the fitness of each subject The fitness value indicates whetheryou have a good subject (for a creature, this could be whether the creature was strong,smart, or fast, for example) or a bad subject Calculating the fitness is completely depen-dent on the application, so you need to find some equation that will work for what youwant to do
After you calculate the fitness, get the two subjects with the highest fitness and mate them.You can do this by randomly selecting which gene comes from which progenitor or byintelligently selecting the best genes of each to form an even more perfect child If you
Chapter 18 I Introduction to Artificial Intelligence
568
Figure 18.3 Gene blood type table
Figure 18.4 Bit vectors (or binary encoding) of
information—the virtual DNA
Trang 7want to bring mutation to the game, you can switch a bit here and there after you get the
final offspring That’s it—you have your artificial offspring ready to use This entire
process is shown in Figure 18.5
A good use of this technology in games is to simulate artificial environments Instead of
keeping the same elements of the environment over and over, you could make elements
(such as small programs) evolve to stronger, smarter, and faster elements (or objects) that
can interact with the environment and you
Neural Networks
Neural networks attempt to solve problems by imitating the workings of a brain.
Researchers started trying to mimic animal learning by using a collection of idealized
neu-rons and applying stimuli to them to change their behavior Neural networks have evolved
much in the past few years, mostly due to the discovery of various new learning
algo-rithms, which made it possible to implement the idea of neural networks with success
Unfortunately, there still aren’t major discoveries in this field that make it possible to
sim-ulate the human brain efficiently
The human brain is made of around 50 billion neurons (give or take a few billion) Each
neu-ron can compute or process information and send this information to other neuneu-rons Trying
to simulate 50 billion neurons in a computer would be disastrous Each neuron takes various
calculations to be simulated, which would lead to around 200 billion calculations You can
forget about modeling the brain fully, but you can use a limited set of neurons (the human
brain only uses around 5 to 10 percent of its capacity) to mimic basic actions of humans
The Fields of Artificial Intelligence 569
Figure 18.5 Mating and mutation of an offspring
Trang 8Chapter 18 I Introduction to Artificial Intelligence
570
In 1962, Rosenblatt created something called a perceptron, one of the earliest neural
net-work models A perceptron is an attempt to simulate a neuron by using a series of inputs,weighted by some factor, which will output a value of 1 if the sum of all the weightedinputs is greater than a threshold, or 0 if it isn’t Figure 18.6 shows the idea of a percep-tron and its resemblance to a neuron
While a perceptron is just a simple way to model a neuron, many other ideas evolved fromthis, such as using the same values for various inputs, adding a bias or memory term, andmixing various perceptrons using the output of one as input for others All of this togetherformed the current neural networks used in research today
There are several ways to apply neural networks to games, but probably the most nant is by using neural networks to simulate memory and learning This field of artificialintelligence is probably one of its most interesting parts, but unfortunately, the topic is toovast to give a proper explanation of it here Fortunately, neural networks are becoming moreand more popular these days, and numerous publications are available about the subject
predomi-Deterministic Algorithms
Deterministic algorithms are more of a game technique than an artificial intelligence concept
Deterministic algorithms are predetermined behaviors of objects in relation to the universe
problem You will consider three deterministic algorithms in this section—randommotion, tracking, and patterns While some say that patterns aren’t a deterministic algorithm,I’ve included them in this section because they are predefined behaviors
n o t e
The universe (or universe problem) is the current state of the game that influences the subject, and
it can range from the subject’s health to the terrain slope, number of bullets, number of adversaries,and so on
Figure 18.6 A perceptron and a neuron
Trang 9Random Motion
The first, and probably simplest, deterministic algorithm is random motion Although
random motion can’t really be considered intelligence (because it’s random), there are a
few things you can make to simulate some simple intelligence
As an example, suppose you are driving on a road, you reach a fork, and you really don’t
know your way home You would usually take a random direction (unless you are
super-stitious and always take the right road) This isn’t very intelligent, but you can simulate it
in your games like so:
NewDirection = rand() % 2;
This will give a random value that is either 0 or 1, which would be exactly the same thing
as if you were driving You can use this kind of algorithm in your games, but it isn’t very
much fun However, there are things to improve here Another example? Okay Suppose
you are watching some guard patrolling an area Two things might happen: The guard
could move in a logical way, perhaps a circle or straight line, but most of the time he will
move randomly He will move from point A to B, then to C, then go to B, then C again,
then D, then back to A, and repeat this in a totally different form Take a look at Figure
18.7 to see this idea in action
His movement can be described in code something like this:
Trang 10To solve this problem and add a little more intelligence to your games, you can use a ing algorithm Suppose the guard spots an intruder He would probably start runningtoward him If you wanted to do this in your game, you would use the following code:
Trang 11kDistance = kIntruderPosition – kGuardPosition;
kGuardVelocity = kDistance.Normalize();
kGuardVelocity *= iGuardSpeed;
kGuardPosition += kGuardVelocity;
This code gets the direction from the intruder to the guard (the normalized distance) and
moves the guard to that direction by a speed factor Of course, there are several
improve-ments you could make to this algorithm, such as taking into account the intruder’s
veloc-ity and maybe doing some reasoning about the best route to take
The last thing to learn with regard to tracking algorithms is about anti-tracking
algo-rithms An anti-tracking algorithm uses the same concepts as the tracking algorithm, but
instead of moving toward the target, it runs away from the target In the previous guard
example, if you wanted the intruder to run away from the guard, you could do something
As you can see, the only thing you need to do is negate the distance to the target (the
dis-tance from the guard to the intruder) You could also use the disdis-tance from the intruder
to the guard and not negate it, because it would produce the same final direction
Patterns
A pattern, as the name indicates, is a collection of actions When those actions are
per-formed in a determined sequence, a pattern (repetition) can be found Take a look at my
rice-cooking pattern, for example There are several steps I take when I’m cooking rice:
1 Take the ingredients out of the cabinet
2 Get the cooking pan from under the counter
3 Add about two quarts of water to the pan
4 Boil the water
5 Add 250 grams of rice, a pinch of salt, and a little lemon juice
6 Let the rice cook for 15 minutes
573Deterministic Algorithms
Trang 12And presto, I have rice ready to be eaten (You don’t mind if I eat while I write, do you?)Whenever I want to cook rice, I follow these steps or this pattern In games, a pattern can
be as simple as making an object move in a circle or as complicated as executing orders,such as attacking, defending, harvesting food, and so on How is it possible to implement
a pattern in a game? First you need to decide how a pattern is defined For your smallimplementation, you can use a simple combination of two values—the action description
and the action operator The action description defines what the action does, and the action operator defines how it does it The action operator can express the time to execute
the action, how to execute it, or the target for the action, depending on what the action is
Of course, your game might need a few more arguments to an action than only these two;you can simply add the necessary parameters Take another look at the guard example.Remember that there were two things the guard might be doing if he was patrolling thearea—moving randomly (as you saw before) or in a logical way For this example, assumethe guard is moving in a logical way—that he is performing a square-styled movement, asshown in Figure 18.8
As you can see, the guard moves around the area in a square-likepattern, which is more realistic than moving randomly Now,doing this in code isn’t difficult, but you first need to define how
an action is represented For simple systems like yours, you candefine an action with a description and an operator The descrip-tion field describes the action (well, duh!), but the operator canhave various meanings It can be the time the action should beperformed, the number of shots that should be fired, or anythingelse that relates to the action For the guard example, the operatorwould be the number of feet to move Although this system worksfor many actions, you might want to introduce more data to thepattern Doing so is easy; you simply need to include more operators
in the action definition A simple example could be:
Trang 13And your guard pattern would be defined The last thing you need to do is the pattern
processor This isn’t hard; you simply need to check the actual pattern description and,
depending on the pattern description, perform the action like so:
This would execute the pattern to make the guard move in a square Of course, you might
want to change this to only execute one action per frame or execute only part of the action
per frame, but that’s another story
Finite State Machines
Random logic, tracking, and patterns should be enough to enable you to create some
intelligent characters for your game, but they don’t depend on the actual state of the
prob-lem to decide what to do If for some reason a pattern tells the subject to fire the weapon,
and there isn’t any enemy near, then the pattern doesn’t seem very intelligent, does it?
That’s where finite state machines (or software) enter
Finite State Machines 575
Trang 14A finite state machine has a finite number of states that can be as simple as a light switch
(either on or off) or as complicated as a VCR (idle, playing, pausing, recording, and more,depending on how much you spend on it)
A finite state software application has a finite number of states These states can be
repre-sented as the state of the playing world Of course, you won’t create a state for each ference in an object’s health (If the object had a health ranging from 0 to 1,000, and youhad 10 objects, that would mean 100,010 different states, and I don’t even want to thinkabout that case!) However, you can use ranges, such as whether an object’s health is below
dif-a number, dif-and only use the object’s hedif-alth for objects thdif-at dif-are nedif-ar the problem you dif-areconsidering This would reduce the states from 100,010 to about four or five
Let’s resume the guard example If an intruder were approaching the area, until now youwould only make your guard run to him But what if the intruder is too far? Or too near?And what if the guard had no bullets in his gun? You might want to make the guard actdifferently For example, consider the following cases:
1 Intruder is in a range of 1000 feet: Just pay attention to the intruder
2 Intruder is in a range of 500 feet: Run to him
3 Intruder is in a range of 250 feet: Tell him to stop
4 Intruder is in a range of 100 feet and has bullets: Shoot first, ask questions later
5 Intruder is in a range of 100 feet and doesn’t have bullets: Sound the alarm
You have five scenarios, or more accurately, states You could include more factors in thedecision, such as whether there are any other guards in the vicinity, or you could get morecomplicated and use the guard’s personality to decide If the guard is too much of a cow-ard, you probably never shoot, but just run away The previous steps can be described incode like this:
Trang 15Not hard, was it? If you combine this with the deterministic algorithms you saw
previ-ously, you can make a very robust artificial intelligence system for your games
Fuzzy Logic
I have already covered the basics of fuzzy logic, but this time I will go into several of the
fuzzy logic techniques more deeply, and explain how to apply them to games
Fuzzy Logic Basics
Fuzzy logic uses some mathematical sets theory, called fuzzy set theory, to work If you’re
rusty with sets, check the mathematics chapter (Chapter 19, “The Mathematical Side of
Games”) before you continue Fuzzy logic is based on the membership property of things
For example, while all drinks are included in the liquids group, they aren’t the only things
in the group; some detergents are liquids too, and you don’t want to drink them, do you?
The same way that drinks are a subgroup—or more accurately, a subset—of the liquids
group, some drinks can also be subsets of other groups, such as wine and soft drinks In
the wine group, there are red and white varieties In the soft drink group, there are
car-bonated and non-carcar-bonated varieties
All this talk about alcoholic and non-alcoholic drinks was for demonstration purposes
only, so don’t go out and drink alcohol just to see whether I’m right Alcohol damages
your brain and your capacity to code, so stay away from it (and drugs, too)
Okay, I’ll stop being so paternal and get back to fuzzy logic Grab a glass and fill it with
some water (as much as you want) The glass can have various states—it can be empty,
half full, or full (or anywhere in between) How do you know which state the glass is in?
Take a look at Figure 18.9
Trang 16As you can see, when the glass has 0 percent water, it is totally empty; when it has 50 cent water, it is half full (or half empty, if you prefer) When it has 100 percent of its size
per-in water, then it is full What if you only poured 30 percent of the water? Or 10 percent?
Or 99 percent? As you can see from the graph, the glass will have a membership value foreach group If you want to know the membership values of whatever percentage of wateryou have, you will have to see where the input (the percentage) meets the membership’sgraphs to get the degree of membership of each, as shown in Figure 18.10
Memberships graphs can be as simple as the ones in Figure 18.10, or they can be zoids, exponentials, or other equation-derived functions For the rest of this section, youwill only use normal triangle shapes to define memberships As in Figure 18.10, you cansee that the same percentage of water can be part of two or more groups, where the greater
trape-membership value willdetermine the value’sfinal membership
Chapter 18 I Introduction to Artificial Intelligence
578
Figure 18.9 Group membership for a glass of water
Figure 18.10 Group membership for a glass of water for various values
Trang 17You can also see that the final group memberships will range from zero to one This is one
of the requirements for a consistent system To calculate the membership value on a
tri-angle membership function, assuming that the value is inside the membership value (if it
isn’t, the membership is just zero), you can use the following code:
float fCenterOfTriangle = (fMaximumRange – fMinimumRange) / 2;
/* Value is in the center of the range */
fDegreeOfMembership = ((fMaximumRange - fCenterTriangle) - (fValue –
fCenterTriangle)) / (fMaximumRange - fCenterTriangle);
}
And you have the degree of membership If you played close attention, what you did was use
the appropriate line slope to check for the vertical intersection offValuewith the triangle
Fuzzy Matrices
The last topic about fuzzy logic I want to cover is fuzzy matrices This is what really makes
you add intelligence to your games First, I need to pick a game example to demonstrate
this concept Anyone like soccer?
You will be defining three states of the game
1 The player has the ball
2 The player’s team has the ball
3 The opposite team has the ball
Although there are many other states, you will only be focusing on these three For each of
these states, there is a problem state for the player You will be considering the following:
1 The player is clear
2 The player is near an adversary
3 The player is open for a goal
Fuzzy Logic 579
Trang 18Using these three states, as well as the previous three, you can define a matrix that will letyou know which action the player should take when the two states overlap Figure 18.11shows the action matrix.
Using this matrix would make the player react like a normal player would If he is clearand doesn’t have the ball, he will try to get in a favorable position for a goal If he has theball at a shooting position, he will try to score You get the idea
But how do you calculate which state is active? It’s easy—you use the group membership
of each state for both inputs, and multiply the input row by the column row to get thefinal result for each cell (It’s not matrix multiplication; you simply multiply each rowposition by the column position to get the row column value.) This will give you the bestvalues from which to choose For example, if one cell has a value of 0.34 and the other cellhas a value of 0.50, then the best choice is probably to do what the cell with 0.50 says.Although this isn’t an exact action, it is the best you can take There are several ways toimprove this matrix, such as using randomness, evaluating the matrix with anothermatrix (such as the personality of the player), and many more
A Simple Method for Memory
Although programming a realistic model for memory and learning is hard, there is amethod that I personally think is pretty simple to implement—you can store game states
as memory patterns This method will save the game state for each decision it makes (orfor each few, depending on the complexity of the game) and the outcome of that decision;
it will store the decision result in a value from zero to one (with zero being a very badresult and one being a very good result)
For example, consider a fighting game After every move the subject makes, the game logsthe result (for example, whether the subject hit the target, missed the target, caused muchdamage, or was hurt after the attack) Calculate the result and adjust the memory resultfor that attack This will make the computer learn what is good (or not) against a certainplayer, especially if the player likes to follow the same techniques over and over again
Chapter 18 I Introduction to Artificial Intelligence
580
Figure 18.11 The action matrix for a soccer player
Trang 19You can use this method for almost any game, from Tic-Tac-Toe, for which you would
store the player’s moves and decide which would be the best counter-play using the
cur-rent state of the game and the memory, to racing games, for which you would store the
movement of the cars from point to point and, depending on the result, choose a new way
to get to the path The possibilities are infinite, of course This only simulates memory,
and using only memory isn’t the best thing to do—but it is usually best to act based on
memory instead of only pure logic
Artificial Intelligence and Games
There are various fields of artificial intelligence, and some are getting more advanced each
day The use of neural networks and genetic algorithms for learning is pretty normal in
today’s games Even if all these techniques are being applied to games nowadays and all
the hype is out, it doesn’t mean you need to use it in your own games If you need to
model a fly, just make it move randomly There is no need to apply the latest techniques
in genetic algorithms to make the fly sound like a fly; random movement will do just as
well (or better) than any other algorithm There are a few rules I like to follow when I’m
developing the artificial intelligence for a game
1 If it looks intelligent, then your job is done
2 Put yourself in the subject’s place and code what you think you would do
3 Sometimes the simpler technique is the needed one
4 Always pre-design the artificial intelligence
5 When nothing else works, use random logic
Summary
This chapter has provided a small introduction to artificial intelligence Such a broad
topic could easily take a few sets of books to explain—and even then, many details would
have to be left out The use of artificial intelligence depends much on the type of game
you are developing, so it is usually also very application-specific While 3D engines can be
used repeatedly, it is less likely that artificial intelligence code can Although this chapter
covered some of the basics of artificial intelligence, it was just a small subset of what you
might use, so don’t be afraid to experiment!
Trang 20Chapter Quiz
You can find the answers to this chapter quiz in Appendix A, “Chapter Quiz Answers.”
1 Which of the following is not one of the three deterministic algorithms covered
2 Can fuzzy matrices be used without multiplying the input memberships?
Why or why not?
A No, it is absolutely necessary to multiply the input memberships
B Yes, but only after negating the matrix
C Yes, it is possible using AND and OR operators, and then randomly
selecting action for the active cell
D Yes, it is possible using XOR and NOT operators after multiplying the matrix
3 Which type of system solves problems that are usually solved by specializedhumans?
A Expert system
B Deterministic algorithm
C Conditional algorithm
D If-then-else
4 Which type of intelligence system is based on an expert system, but is capable
of determining fractions of complete answers?
Trang 216 Which type of intelligence system solves problems by imitating the workings
7 Which of the following uses predetermined behaviors of objects in relation
to the universe problem?
Trang 22This page intentionally left blank
Trang 23The Mathematical
Side of Games
chapter 19
As you might already know, math is an extremely important subject in high-level
computer programming, especially in game programming Behind the scenes, in
the graphics pipeline, and in the physics engine, heavy math is being processed by
your computer, often with direct implementation in the silicon (as is the case with most
graphics chips) While a huge amount of heavy math is needed to get a polygon on the
screen with a software renderer, that is all handled by highly optimized (and fantastically
complex) mathematics built into the latest graphics processors Vectors, matrices, functions,
and other math-related topics comprise an indispensable section in any game-programming
curriculum In this chapter, I will go over basic linear algebra, such as vector operations,
matrices, and probability, with a bit of calculus when I get into the basics of functions
Please note that this is an extremely simple primer on basic algebra and calculus and
should accomplish little more than whetting your appetite For a really solid treatment
of game mathematics, please refer to Mathematics for Game Developers (Course
Tech-nology PTR, 2004) by Christopher Tremblay, who has tackled the subject with a tenacity
that is sure to enhance your math skills
Here is a breakdown of the major topics in this chapter:
Trang 24Trigonometry is the study of angles and their relationships to shapes and various other
geometries You will use some of the material covered here as support for some advancedoperations you will build later
Visual Representation and Laws
Before I go into the details of trigonometry, let me introduce a new concept—radians A
radian is a measurement of an angle, just like a degree One radian is the angle formed in
any circle where the length of the arc defined by the angle and the circle radius are of samelength, as shown in Figure 19.1 You will use radians as your measurement because theyare the units C++ math functions use for angles Because you are probably accustomed tousing degrees as your unit of measurement, you need to be able to convert from radians
to degrees and vice versa As you might know,/ radians is the angle that contains half acircle, as you can see in Figure 19.2 And you probably know that 180 degrees is also theangle that contains half a circle Knowing this, you can convert any radian unit to degrees,
as shown in Equation 19.1, and vice versa using Equation 19.2
Chapter 19 I The Mathematical Side of Games
586
Figure 19.1 Relation of the arc length and
radius of the circle
Figure 19.2 Half a circle denoted by
radians and degrees
Trang 25double DegreeToRadian(double degree)
Now that you know what a radian is, I’ll explain how to use them Take a look at Figure 19.3
From the angle and the circle radius, you can get the triangle’s sides and angles If you
exam-ine that circle a little bit closer, you will see that in any triangle that contains the center of
the circle and the end of the arc as vertices, the hypotenuse of that triangle is the line formed
from the circle’s center to the end of the arc
Now you need to find the two other lines’
lengths that form the triangle You will findthese using the cosine and sine functions
The three equations that are important ingeometry are cosine, sine, and tangent, andthey are directly related to the triangle Seethe cosine Equation 19.3, the sine Equation19.4, and the tangent Equation 19.5
Trigonometry 587
Equation 19.1
Equation 19.2
Figure 19.3 A triangle formed by a circle
radius and an angle;/ radians = 180 degrees
Trang 26You can calculate these trigonometric operations using the MacLaurin series, but that isbeyond the scope of this book Now you can determine the length of the adjacent side ofthe triangle on the circle by using the cosine, as shown in Equation 19.6.
What if you want to know the angles at each side of the triangle? You use exactly the sameequations as you used before to get the sine or the cosine When you have them, you use theinverse of those operations to get the angles Taking the triangle in Figure 19.3, you find two
of the angles You don’t need to find one of the angles because you already know that the angle is a right angle triangle, and as such, the angle formed is 90 degrees, or one-half/
tri-Chapter 19 I The Mathematical Side of Games
Trang 27What is the difference between the two equations? If you look carefully, you are trying to
get the angle using the cosine and the opposite side You do this because the opposite
side of the angle is actually the adjacent side in relation to that angle So what does this
mean? It means that the terms adjacent and opposite are relative to the angle to which they
are referred In the second calculation, the opposite side should actually be the adjacent
side of that angle Table 19.1 shows you the list of trigonometric functions This might
seem complicated, but it will become clearer when you start using all of this later
Angle Relations
A couple of relations can prove useful when you are dealing with angles and
trigonomet-ric functions One of the most important relations is the trigonomettrigonomet-ric identity shown in
Equation 19.9
This equation is the base of all the other relations To be honest, these relations are used
only for problem solving or optimizations For that reason, I will not go over them in detail;
I will simply show them to you so you can use them at your discretion The following
equa-tions are derived from Equation 19.9 and should be used to optimize your code
Trigonometry 589
Table 19.1 C Trigonometric Functions
Trigonometric C Function C Function Inversed
cosine cos acos
sine sin asin
tangent tan atan/atan2
* These functions are all defined in math.h.
Equation 19.9
Equation 19.10
Trang 28Now you are done with trigonometry Trigonometry isn’t very useful per se, but it willprove an indispensable tool later when you use it with other concepts, such as vectors ormatrices.
Vectors
A vector is an n-tuple of ordered real values that can represent anything with more thanone dimension—for example, a 2D or 3D Euclidean space Basically, vectors are nothingmore than a set of components
Vectors describe both magnitude and direction In the two-dimensional case, the X and Ycomponents represent the distance from the relative origin to the end of the vector, as youcan see in Figure 19.4 Because you are using a 2D world, you define vectors using twocomponents for convenience, with a commonly known notation (x, y) You can also rep-resent just one component of the vector by using a subscript either with the order of theelement or with the component identification, as shown in Equation 19.14
#include <math.h>
typedef struct vector2d
Chapter 19 I The Mathematical Side of Games
590
Equation 19.11
Equation 19.12
Equation 19.13
Trang 29Addition and Subtraction
Vectors can be added or subtracted to form new vectors You can see in Equation 19.15
that the addition of two vectors is completed component by component, which is true for
subtraction as well
591Vectors
Trang 30Equation 19.15 also shows that vector addition can be done in any order, but this isn’t truefor vector subtraction If you take a look at Figure 19.5, you can see how the same vectorssubtracted in different order produce a vector that is the same in length but different inorientation Before I move on, I want to create your addition method.
vector2d vector2d_add(vector2d first, vector2d second)
{
vector2d newvector;
newvector.components[0] = first.components[0] + second.components[0];
newvector.components[1] = first.components[1] + second.components[1];
return newvector;
}
As you can see in Figure 19.6, the subtraction of two vectors gives you the distancebetween them, but it isn’t commutative If you subtract A < B you get the distance from A
to B, whereas in B < A you get the distance from B to A This is shown in Equation 19.16
Chapter 19 I The Mathematical Side of Games
592
Figure 19.5 Addition of two vectors
Figure 19.6 Subtraction of two vectors in different order
Trang 31n o t e
In Figure 19.6, you can see that the product of the subtraction has its origin on the end of the first
vector This is incorrect The vector origin should be the origin of the world
To finalize this section, let’s build the subtraction function
vector2d vector2d_subtract(vector2d first, vector2d second)
{
vector2d newvector;
newvector.components[0] = first.components[0] - second.components[0];
newvector.components[1] = first.components[1] - second.components[1];
return newvector;
}
Scalar Multiplication and Division
You can scale vectors by multiplying or dividing them by scalars, just like normal
scalar-to-scalar operations To do this, you multiply or divide each vector component by the
scalar You can see this in Equation 19.17, which shows multiplication of each of the vector
components by a scalar to produce a new vector
In code you have:
vector2d vector2d_multiply(vector2d vect, double multiplier)
{
vector2d newvector;
newvector.components[0] = vect.components[0] * multiplier;
newvector.components[1] = vect.components[1] * multiplier;
Trang 32You do the same thing for division, as you can see in Equation 19.18.
To end the normal operations, let’s build a division function
vector2d vector2d_divide(vector2d vect, double divisor)
{
vector2d newvector;
newvector.components[0] = vect.components[0] / divisor;
newvector.components[1] = vect.components[1] / divisor;
return newvector;
}
Length
The length is the size of the vector The length is used in several other vector operations,
so it should be the first one you learn If you remember the Pythagorean Theorem, youknow that the square of the hypotenuse is equal to the sum of the square of each side Youuse the same theorem to get the length of the vector, as you can see in Equation 19.19
As usual, I’ll write a function to calculate the length of a vector
double vector2d_length(vector2d vect)
As you saw earlier, vectors have both an orientation and a length, also referred to as the
norm Some calculations you use will need a vector of length 1.0 To force a vector to have
Chapter 19 I The Mathematical Side of Games
594
Equation 19.18
Equation 19.19
Trang 33a length of 1.0, you must normalize the vector—in other words, divide the components of
the vector by its total length, as shown in Equation 19.20
vector2d vector2d_normalize(vector2d vect)
{
vector2d newvector = vect;
double length = vector2d_length(vect);
Finding the perpendicular of a vector is one of those operations you’ll use once a year, but
let’s briefly talk about it anyway A vector perpendicular to another is a vector that forms
a 90-degree angle, or a half-/ radians angle with the other In Figure 19.7, you can see that
vector B forms a 90-degree, counterclockwise angle with vector A
Finding the perpendicular vector of a 2D vector iseasy; you simply need to negate the Y componentand swap it with the X component of the vector, asshown in Equation 19.21
Vectors 595
Equation 19.20
Figure 19.7 A perpendicular vector
forming a 90-degree, counterclockwise
angle with another vector
Trang 34Just one little thing… You see that reversed T in Equation 19.21? That is the lar symbol.
perpendicu-vector2d perpendicu-vector2d_perpendicular(perpendicu-vector2d vect)
This equation gives a little more information, don’t you agree? In case you didn’t know,
ø is the smallest angle formed by the two vectors With a little thought and by combiningEquations 19.22 and 19.23, you can get the equation to find the smallest angle of two vec-tors (see Equation 19.24)
Chapter 19 I The Mathematical Side of Games
596
Equation 19.21
Equation 19.22
Trang 35You finally have some use for the dot product If you calculate the arc cosine of the dot
product of the two vectors divided by the product of their lengths, you have the smallest
angle between them Now you can build the angle function
double vector2d_angle(vector2d first, vector2d second)
The perp-dot product is nothing new It is the dot product of a calculated perpendicular
vector This operation is mostly used in physics, as you will see later How do you find the
perp-dot product? Easy—you find the perpendicular of a vector and calculate the dot
product of that vector with another, as shown in Equation 19.25
Vectors 597
Equation 19.23
Equation 19.24
Equation 19.25
Trang 36double vector2d_perpdotproduct(vector2d first, vector2d second)
{
return vector2d_dotproduct(vector2d_perpendicular(first), second);
}
Matrices
A simple way of defining a matrix is to say that it is a table of values You can see in
Equation 19.26 that a matrix is defined by a set of rows and columns The number of
columns is given by p and the number of rows by q You can also access any element of the matrix using the letter i for the row and the letter j for the column This is shown in
Equation 19.27
Addition and Subtraction
Matrix addition and subtraction is done exactly the same way as the vector addition andsubtraction You add (or subtract) each element of one matrix to (or from) the other toproduce a third matrix, as shown in Equation 19.28 (for the addition operation)
Matrix addition is commutative (that is, independent of the order), but this isn’t the casefor subtraction, as you can see in Equation 19.29
Scalars with Multiplication and Division
Again, to multiply or divide a matrix by a scalar, you multiply or divide each matrix ment by the scalar, as shown in Equation 19.30 for multiplication
ele-Chapter 19 I The Mathematical Side of Games
598
Equation 19.26
Equation 19.27
Trang 37This is exactly the same for the division process, shown in Equation 19.31.
Scalar operations in matrices are pretty easy and usually unnecessary Next I will go over
the most useful matrix operations
Special Matrices
There are two special matrices I want to go over—the zero matrix and the identity matrix
First, the zero matrix is a matrix that, when added to any other matrix, produces the
matrix shown in Equation 19.32
599Matrices
Equation 19.28
Equation 19.29
Equation 19.30