Mathematics in Videogames
By:
(alphabetical order )
Angela Ren
Christine Lee
Faizan Khalid
Michelle Wong
Minna Lee
Evolution of Games
Wolfenstein, one of the earliest FPS (first person shooter)
games
Evolution of Games Cont.
Doom, the next breakthrough in graphics
Evolution of Games Cont.
Quake, this was a huge leap in the quality of
graphics in computer games
Evolution of Games Cont.
Quake III, one of the newest FPS games around with
more detailed images
Numbers
• Integers
• Fixed point numbers
• Floating point numbers
• Discreteness
Spatial discreteness
Temporal discreteness
Integers
• Integers can be stored exactly in binary and have
efficient methods for addition and subtraction, as well
as slightly less efficient methods of multiplication and
division.
• When used in computers, integers have upper and
lower limits. This limit is important when operating
certain operations as overflow can occur.
• Integers have been favored for storing variables used in
video games because operations on them were much
faster than on other types of numbers.
Fixed Point Numbers
• Efficient way of dividing an integer by a scale
factor:
when the scaling factor is a power of two the
division can be easily performed by using binary
shifts, when number is stored in this manner, it is
called a fixed point number, the integer
represents a number with both integer and
fractional parts, separated by a binary point.
Floating Point Numbers:
Fixed Ranges
• The main disadvantage of fixed point numbers is
that they lack precision over a large range: they
cannot hold very large or very small numbers.
• Solution: floating point numbers
– Two parts: 1. A signed exponent (the range shift)
» 2. Signed mantissa (raw value)
*turn a floating number into a fixed point
number by shifting the mantissa by and
number of bits specified in the exponent.
Discreteness
• Discreteness-values in computer games
often have integer values.
– Time is not continuous (split up into frames
of 1/60th of a second)
– Character positions are not continuous (they
are displayed on pixel boundaries)
*This can be very useful as an approximation
to a smooth universe.*
Spatial Discreteness:
Aligning objects to grid boundaries
• On nearly all video games, characters must
be placed on integral pixel boundaries.
• The use of fixed point (or floating point)
numbers for game objects means the objects
are held with greater precision than is
shown on the screen.
Temporal Discreteness:
Game Frames
• The concept of games frames is fundamental to
traditional games programming.
• Most games are based on the ticking of a clock
which happens a fixed number of times per
second. Every time this clock signal is
processed, the game executes one cycle of
thought. Operations that may have been taught
to calculate all at once have to be done one step
at a time.
Temporal Discreteness:
Game Frames
• The game frame is not the same as the display frame.
• The game is run in a separate “thread” from the display
code, so it should be immune to the display code being
bogged down by too many objects or a scene being too
complex.
• The shared variables that are written by the game code and
read by the display code should be double buffered to
prevent synchronization problems.
Shading
• Flat Shading
• Gouraud Shading
• Phong Shading
• Quadratic Shading
Flat Shading and
Gouraud Shading
• Flat shading simply assigns a single color to a
polygon. It is very simple and fast, but makes the
object look very artificial.
• Colors are assigned to each vertex, then they are
blended across the face of the polygon. Since each
vertex is typically associated with at least three
distinct polygons, this makes the object look
natural.
For example…
Phong Shading
• Introduced by Phong, is the appropriate shading
method to go with his illumination model.
• Triangles are shaded by linearly interpolating the
surface model the surface normal and re-
revaluating the illumination equation at each pixel.
• Phong shading is superior to other methods but it
is computationally demanding and very expensive.
Quadratic Shading
• A quadratic can express the shading maximums
and minimums that appear within a triangle’s
interior.
• Can approximate light distribution better than
Gouraud shading.
• Can be constrained to enforce continuity across
triangle edges, but this increases computation
because the quadratic has more degrees of
freedom (6) so it needs more constraints.
Geometry, Vectors and
Transformations
• In order to understand how FPS games work,
geometry, vectors and transformations are
required.
• Geometry: is the study of shapes of various sort.
• Vectors: mathematical way of representing a
point.
• Transformations: moves a point (or an object,
or even an entire world) from one place to
another.
Geometry
• Simple shapes: a point and a line ― a straight
line.
• Complex shapes: a plane ― a flat sheet, like a
piece of paper or a wall and a solid ― a cube or a
sphere.
Simple geometric figures
Vectors
• A vector is 3
numbers: usually x,
y, z for three
different directions
to get one point.
• For example, when
x=3, y=1, z=5 this
point can be found
by using the 3-
Dimensional (x, y, z)
coordinate plane.
Picture of a vector and directions
Transformation
• Transformation, moving the object across a plane, is
also called Translation
• Another type of transformation is called Rotation
Translations and rotations
3D Graphics
• The basic idea of 3D graphics is to turn a mathematical
description of a world into a picture of what that world
would look like to someone inside the world.
• The mathematical description could be in the form of a
list, for instance: there is a box with center (2,4,7) and
sides of length 3, the color of the box is a bluish grey.
To turn this into a picture, we also need to describe
where the person is and what direction they are
looking, for instance: there is a person at (10,10,10)
looking directly at the center of the box. From this we
can construct what the world would look like to that
person.
3d Graphics cont.
•What I just described above is similar to what the
computer is doing (50 times a second!) every time you
run around shooting hideous monsters in Quake, although
the details are slightly different.
•In computer games (at the moment) the description of
the world is just a list of triangles and colors. The newest
computer games are using more complicated descriptions
of the world, using curved surfaces, however in the end it
always reduces to triangles. For instance, a box can be
made using triangles as illustrated below:
3d Images
Cartesian Coordinates
• In 3-D games, we use x, y, and z axes to
graph coordinates in a game.
• X-coordinate is the horizontal value
• Y-coordinate is the vertical value
• Z-coordinate is the depth value
Polar Coordinates
• Polar coordinates are
good for figuring out
the velocities of
objects in 2-D games
(sports games, etc)
• A person can be
running in a particular
direction and then
change angle by a
certain amount each
game frame, while
maintaining the same
speed.
Quaternions
• Quaternions: noun: Any
number of the form a +
bi + cj + dk where a, b, c,
and d are real numbers,
ij = k, i2 = j2 = -1, and ij
= -ji.
• Quaternions are used to
process rotations in
three dimensions.
Matrices
• Matrices are used to
change your point of
view in the videogame.
• Objects and
characters in a game
are positioned with
coordinates:
(x, y, z).
To get your coordinates to transform, multiply
by a matrix:
• Rotate around x-axis:
• Scale to size:
• Translation:
Vectors
Vector Products:
• Vector products determine an
object’s seen surface and hidden
surface. This is done by a process
that uses both vector products.
• Various lighting scenarios, ambient
light, parallel light and point source
light are used to give the game a
touch of realism.
Hidden Surfaces
• We need to cover the set
of points that define the
object with a convex
polygon mesh.
• By taking cross products
of two defined edge
vectors, we generate a
“normal” vector to the
plane of the polygon
Hidden Surfaces
• Because the normal will point out from
the object’s surface, this allows us
determine which section is visible and
hidden to the object.
• Another way to determine the hidden
surfaces is when the dot product of the
vector observer, V2, and the normal, n, is
positive.
Light
• A brightness of
a certain part of
an object is
dependent on
the component
of reflected light
reaching the
observer.
Light cont.
• N is a unit normal R = 2(N•L )N – L
• Component for reflected light (O•R)O
• O is a unit vector in the direction of the
observer.
• Scaling factors on the length of this vector
is used to generate the desired effect.
Inertia
•Newton’s first law states that unless there is an
obstacle or another force on an object’s path, it will
move in a straight line (inertia)
•In order to apply this knowledge, we need to make sure
that we keep in track of the object’s velocity, and this
should be added to the object’s position each game
frame
•We should change the velocity to handle various forces
that act upon the object, not simply change the object's
position.
Gravity
•Gravity is a force that pushes two things together
•The force of gravity is defined by:
•G is the universal gravitation constant, m1 is the mass of
the first object and m2 is the mass of the second object,
and r is the distance between them
•The effect of gravity on earth’s surface is of constant
acceleration at which point you can use F=mg, where m is
the mass of an object and g is 9.8 N/m2
Gravity cont.
•So to make objects fall, add an acceleration constant to
their y velocity every game frame. Then add the y velocity
to the y position.
•You can change the acceleration constant to change the
feel of the game world. When the constant is low, the
game has a lunar landscape feel, and the jumps seem to go
in slow motion. But when the constant is high, the jump is
way faster and the person seems to run faster as well.
Bouncing
•When an object in the game hits the background or
another object, it is called a collision. There are two types
of collision: inelastic and elastic (use of physics)
•When an object hits a horizantal background, it bounces
up. This is done by multiplying the y velocity vector by a
negative number bween 0 to –1.
•This number is related to the coefficient of the level of
“bounciness”
•The higher the number is, or the closer it is to 0, the less
bouncy it is.
Bouncing Cont.
•Similar to the horizontal backgrounds discussed earlier,
same rules apply for vertical backgrounds.
•With vertical backgrounds, you do the same but instead
you multiply by the x velocity vector.
•Other backgrounds, excluding horizantal and vertical,
includes the use of geometry
•If an objects hits a diagonal, or any non vertical or non
horizantal background, then you measure the angle.
•But as the measure of the angle changes, the speed does
as well.
Friction
In the real world,
once you stop
pushing things
they generally
slow down and
stop.
Friction
You can simulate friction by multiplying
an object's velocity by a constant value
between 0.0 and 1.0 each frame.
This value should be only slightly less than
1.0, since the lower it is the faster the
object will slow down.
Experimenting with different values to see
which numbers look right in your game
Jumping
: User should be
able to control
how high their
game character
jumps by holding
down a button.
: Game designer
control gravity.
: THIS GOES
AGAINST THE
LAWS OF
PHYSICS!!!!
Newton's Differences:
Simplifying the physics maths
A object fired into the air under gravity will
basically follow the path of a parabola
(ax2 + bx + c )
Take differences of the results of this function,
and they form a more simple pattern.
Newton's Differences:
Simplifying the physics maths
Figure five
Velocity Acceleration
Take differences of
x x2 D x2 D D x2 D D D x2
the results of this
function. 0 0 1
1 1 3 2 0
Repeating that 2 4 5 2 0
step yields a
constant number. 3 9 7 2 0
This is the
acceleration of the 4 16 9 2 0
object! 5 25 11 2
6 36
Calculus:
The proof behind the simplifications
These differences are in fact the special case
of differentiation
Call distance X and time t
Velocity=dX/dt
Acceleration=dV/dt
Calculus:
The proof behind the simplifications
Velocity Acceleration
Figure five
dX/dt = 2t dX/dt = 2
t t2 D t2 D D t2 D D D t2
0 0
0 1
1 1 2 3 2 2 0
2 4 4 5 2 2 0
3 9 6 7 2 2 0
4 16 8 9 2 2 0
5 25 10 11 2 2
6 36
Intelligent Motion
The deterministic
algorithms for enemy
movement used in most
games:
•Chase
•Evade
•Pattern
•Response
•Random choice.
Intelligent Motion
Deterministic algorithms are simple AI techniques
Use a set of variables as the input and then
use some simple rules to drive game objects
based on these inputs.
Perceived Smoothness:
Removing jerkiness
For motion to be smooth, it should be continuous to the
greatest extent.
• Position - if this changes suddenly, the object
appears to jump.
• Velocity - sudden changes of velocity seem
very line based.
• Acceleration - changes of acceleration are
not so noticeable in a game.
Chase Algorithm
For example, when an enemy is programmed
to follow player
If the player is to the left of the enemy, move
enemy left. If it is to the right, move right.
Eugene Jarvis’ Defender
Simple Chase Algorithm
const int xstart = 12, xcentre = 0, xvel = 2;
int frame, xpos;
for (frame = 0, xpos = xstart;; plot(frame++, xpos))
{
if (xpos xcentre)
xpos -= xvel;
}
Chase Algorithm (with acceleration)
const int xstart = 12, xcentre = 0, xacc = 1;
int frame, xpos, xvel;
for (frame = 0, xpos = xstart, xvel = 0;; plot(frame++,
xpos))
{
if (xpos xcentre)
xvel -= xacc;
xpos += xvel;
}
Chase Algorithm (with acceleration)
xpos
time
Some Calculus
•If we call distance x and time t, we can calculate velocity v
as dx/dt and acceleration a as dv/dt.
•If an object starts at position x, and has a velocity v which
doesn't change, then after t seconds, the position of the
object is xt=x+v×t.
•Example: a fighter throws a grenade from a high cliff. his
position is (100,150,150) and he throws a rock with
velocity (5,2,0). Where is the rock after 10 seconds? To
work this out, we use the formula above: xt=x+v×t. In this
case (after 10 seconds) the rock is at
(100,150,150)+(5,2,0)×10=(100,150,150)+(50,20,0)=(15
0,170,150).
Simulation Games
• How is all this stuff actually used in computer games?
Here's a simple example:
• Every time you fire a bullet from your plane in your
favorite flight simulator, the computer has to work
out its position using calculations. The computer has
to do these calculations 50 times a second for every
bullet in the sky, and on top of that it has to do much
harder calculations for the planes as well
• It is much more complicated, because in the real
world, there are things like wind and friction
because of the air.
Simulation Games cont.
•Here is how you might include wind in the calculations.
Suppose the wind causes the bullet to have an additional
acceleration of w, and the wind is blowing at a constant
rate (the wind speed isn't changing).
•Now we have the acceleration on the bullet is g+w.
Friction in the air is much more complicated though,
because the amount of friction depends on the speed of
the bullet.
•In fact, the friction causes an additional acceleration of -
kv, where k is some positive number and v is the velocity.
So now the acceleration is g+w-kv
•In other words, the acceleration is changing as well as
the velocity!
Works Cited
CMP Media. “Mathematics in Videogames.” July 2001. 18 May 2006
Goodman, Dan. “The Use of Mathematics in Computer Games.” May
2000. 18 May 2006
Wilkins, Kevin. “Mathematics for Computer Games Technology.” 18
May 2006
Vlasic, Daniel. "Fake Phong Shading." 17 May 2002. MIT. 18 May
2006
.