O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The final step is to take the square root of both sides:
(1.38)
Simplifying equations can get a lot harder than this of course, but these few
rules are enough for you to understand any of the simplifications presented
in this book.
Trigonometry
Trigonometry is based on the study of triangles. The word comes from the
Greek words trigon, for triangle, and metry, for measure. It is an enor
-
mously useful field of mathematics and has many practical applications in
computer science. In the game AI field, you will find it used for line-of-
sight (LOS) calculations, collision detection, some aspects of pathfinding,
etc. Lots of AI is really math-dependent when you boil it down; you will be
wise to learn it well.
Rays and Line Segments
A ray is a line with one endpoint. It is of infinite length and is defined by a
direction (usually expressed as a normalized vector; see the section on vec-
tors later in this chapter) and an origin. Figure 1.6 shows a ray situated at
the origin.
A line segment is a piece of a line and is defined by two endpoints. Fig-
ure 1.6 also shows a line segment defined by the two endpoints p1 and p2.
10 | Chapter 1
Mathematics
2yx=
Figure 1.6. A line segment and a ray
Angles
An angle is defined as the measure of divergence of two rays that share the
same origin. See Figure 1.7.
You may be used to thinking of angles in terms of degrees. Walls in most
homes are typically at 90 degree angles, for example, and circles are 360
degrees around. Mathematicians prefer to measure the magnitude of an
angle using radians. Radians are a unit of measurement based upon a circle
of unit radius — a radius of 1 — centered at the origin. The radius of a cir-
cle is the distance from the center of the circle to its perimeter. Drawing the
two rays from Figure 1.7 onto the same diagram as the unit circle, we get
Figure 1.8. The length of the curved line segment between the two rays —
shown in the diagram as a dotted line — is the angle measured in radians
between them.
A Math and Physics Primer | 11
Mathematics
Figure 1.7. An angle
Figure 1.8. The length of the dotted line is the angle in radians between the two rays.
Now that you know what a radian is, let’s calculate how many radians there
are in a circle. You may remember the Greek symbol p (pi) from your
school days. It’s a well-known and frequently used mathematical constant,
and has a value of 3.14159 (to five decimal places). You can use pi to cal
-
culate the circumference of a circle — the distance around the entire
perimeter — using the equation:
(1.39)
Using this equation to determine the perimeter of a unit circle gives the
number of radians in a circle. That’s because the number of radians in a cir
-
cle is the length of the perimeter of a circle with a radius of 1. So we just
substitute 1 for r in equation (1.39) to get:
(1.40)
Therefore, there are 2 p radians in every circle.
z
TIP Now that you know how many radians make up a circle, you can convert
between radians and degrees if you ever have to. There are 360 degrees in a
circle, so that means:
360º = 2 p rads
Dividing both sides by 360 we get:
1º=2p /360 rads
Angles are usually denoted using the Greek letter theta, which looks like
this: q.
Triangles
A triangle consists of three line segments connected at their ends. A trian
-
gle’s inner angles always add up to p radians (180 degrees). Figure 1.9
shows the different types of triangles you can encounter.
12 | Chapter 1
Mathematics
2perimeter rp=
22(1)2perimeter r num radianspp p== ==
Figure 1.9. Different types of triangles

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required