MathHonorsTalk-v2

advertisement
You'll Use Everything:
Mathematics in the Gaming Industry
Mu Alpha Theta Initiation
Daniel R. Collins
May 27, 2015
Question

“What will I use this for?”

We don't know what you'll do for work!

My teachers certainly didn't.


3D game industry invented while I was in
school.
Consider this a case study.
Title of the Talk

“You'll use everything you've ever learned”

Johnny Carson to Steve Martin

Tonight Show, 1976

See: Martin, “Born Standing Up” (2008)
My Time in the Game Industry

Hired after college with a math degree

Papyrus Design Group, 1996-1998


Genetic Anomalies, 1998-2000


PC racing simulations (IndyCar, NASCAR)
Encrypted virtual collectible card games
Both in Boston, MA
NASCAR Racing 2 (Papyrus, 1996)
Star Trek: ConQuest Online (GA, 2000)
After My Time in the Game Industry

Pen-and-paper role-playing games (D&D)

Campaign, Fight On! magazines

Judge, “One Page Dungeon Contest”

Self-published rules expansions

Blog: “Delta's D&D Hotspot”

Contacts in computer game industry
About the Game Industry


Top-level games cost as much to make as
movies

Grand Theft Auto V: $140M

Star Wars The Old Republic: $200M
Greatest demand on hardware usage

Push to limits: CPU, memory, graphics cards, video
refresh FPS, sound, etc.
Skills: Computer Programming

“Close to the metal”

Mostly in C/C++ and some Assembly

Can't afford overhead from fancier languages


No automatic garbage collection as in Java,
et. al.
Know memory management and be careful of
lost pointers (memory leaks, crashed
systems, etc.)
Trigonometry

My first 2D game (age 13)

At what angle is the bad guy shooting?
Solving Physics Formulas

Regular need to find and read physics
formulas

Solve for desired variable (“literal equation”)

Enter as program code
Example: Doppler Effect (NASCAR Racing 2)
/* find engine RPMs, relative speeds between mic & source */
v = tmp->longSpeed;
v = BETWEEN(- 2 * 250 * MPH, v, 2 * 250 * MPH);
pitch = engPitch(abs(v), i);
vol = engVol(abs(v), i);
dl = trackDist(Dlong, tmp->dlong);
dt = tmp->dlat - Dlat;
d = magnitude(dl,dt);
if(!d)
d = 1;
Ospeed = muldiv(Speed, dl, d) + muldiv(Latspeed, dt, d);
Sspeed = muldiv(v, dl, d) + muldiv(tmp->latspeed, dt, d);
pitch = muldiv(pitch, SPEED_OF_SOUND + Ospeed, SPEED_OF_SOUND +
Sspeed);
Example: Doppler Effect (NASCAR Racing 2)
Analytic Geometry in RARS

RARS: Robot Auto Racing Simulator

Hobby AI-writing competition
Analytic Geometry in RARS



Racetrack geometry given in terms of straight
line segments and circular arcs.
Fastest velocity is on the largest circle that
can be carried through a corner
So: Granted outside of straight and inside
point of curve (apex), solve with the fastest
circle through the corner
Analytic Geometry in RARS
Analytic Geometry in RARS
// This function takes 3 control points and solves for a circle through them.
// Takes index of apex cp as input; returns center (x,y) and radius.
static void cpSolveCircle (int apex, double *x, double *y, double *radius)
{// ...
//
// Our strategy is to draw two lines (parameterized), bisecting each set
// of control points, and find the center at their intersection.
//
Tpline pline1, pline2;
//
x1
x2
x3
set 3 points
= cp_data[cpBefore(apex)].x;
= cp_data[
apex
].x;
= cp_data[cpAfter (apex)].x;
y1 = cp_data[cpBefore(apex)].y;
y2 = cp_data[
apex
].y;
y3 = cp_data[cpAfter (apex)].y;
// compute centers between each pair of points
pline1.cx = (x1+x2)/2; pline1.cy = (y1+y2)/2;
pline2.cx = (x2+x3)/2; pline2.cy = (y2+y3)/2;
// compute differences (direction vectors) between each pair of points
pline1.dx = -(y1-y2);
pline1.dy = (x1-x2);
pline2.dx = -(y2-y3);
pline2.dy = (x2-x3);
// intersect these lines
double t1;
plinesIntersect(&pline1, &pline2, &t1);
plineEvaluate(&pline1, t1, x, y);
// calculate radius: dist from center to 1 point
*radius = dist(*x, *y, x1, y1);
}
Linear Algebra: Vectors


Vectors used to represent forces in any kind
of 3D game physics engine
Following example from simple 2D RARS
Linear Algebra: Vectors
Linear Algebra: Matrices




Matrices are used to represent position,
orientation, movement and rotation in space.
For 3D: use 4x4 matrices
Additional components used to indicate
scaling (note ellipsoids in following
examples: compressed sphere primitives).
Example: “Film” ray-trace render engine
written in Pascal (1995-2000).
Linear Algebra: Matrices
Linear Algebra: Matrices
Linear Algebra: Matrices
{ SAV-DD.DAT:
0
1
0
0
0
0
1
0
0
0
0
1
0
Sathar Destroyer with planet & sun }
0
0
0
1
BEGIN
{bridge}
0
8.50000023841858E-0001
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
.767 .437 .110 .767
.767 .437 .110 .767
0.8 0.85 0 0
{bridge 1}
0
3.49999994039536E-0001
0.00000000000000E+0000
0.00000000000000E+0000
7.50000000000000E-0001
.767 .437 .110 .767
.767 .437 .110 .767
0.8 0.85 0 0
0.00000000000000E+0000
8.50000023841858E-0001
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
8.50000023841858E-0001
1.31999998092651E+0001
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
1.00000000000000E+0000
0.00000000000000E+0000
3.49999994039536E-0001
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
3.49999994039536E-0001
1.31999998092651E+0001
0.00000000000000E+0000
0.00000000000000E+0000
0.00000000000000E+0000
1.00000000000000E+0000
More Analytic Geometry



In the ray-trace renderer, need to physically
simulate/compute each light-ray intersection
with primitive objects (spheres, cylinders,
cubes, etc.)
This is done recursively to handle reflections,
refractions, and shadows.
Following example shows code for checking
if a 3D vector intersects with a unit sphere
More Analytic Geometry
{Intersections on the primitive SPHERE:
centered at (0,0,0) with radius 1.
Returns t, point of intersection (if less than inbound t);
normal, outward normal to the surface
(assuming HitSphere and "want_normal")
Consult Watt (pp. 163-164) and Hill (pp. 624-627).
}
FUNCTION Hit_Sphere(var thisRay: ray; var t: double; want_normal: boolean;
var normal: vector4): boolean;
VAR a, b, c, determ, determ_root, trial_t: double;
BEGIN
WITH thisRay DO BEGIN
{* default *}
Hit_Sphere := FALSE;
{* coefficients for quadratic *}
a := dir[1]*dir[1] + dir[2]*dir[2] + dir[3]*dir[3];
b := _2 * (dir[1]*start[1] + dir[2]*start[2] + dir[3]*start[3]);
c := start[1]*start[1] + start[2]*start[2] + start[3]*start[3] - _1;
determ := b*b – 4*a*c;
{* analyze intersections *}
if (determ < AlmostZero) then Exit;
determ_root := sqrt(determ);
More Analytic Geometry
{* smaller "-" t positive: looking in at near wall *}
If -(b+determ_root) > AlmostZero then begin
trial_t := -(b + determ_root)/(_2*a);
if trial_t > t then Exit;
{* obscured *}
Hit_Sphere := TRUE;
t := trial_t;
if want_normal then
{a unit vector}
LoadVec4(start[1]+t*dir[1], start[2]+t*dir[2], start[3]+t*dir[3], normal);
End
{* larger "+" t positive: looking out at far wall *}
Else if (determ_root-b) > AlmostZero then begin
trial_t := (determ_root - b)/(_2*a);
if trial_t > t then Exit;
{* obscured *}
Hit_Sphere := True;
t := trial_t;
if want_normal then
{a unit vector}
LoadVec4( -(start[1]+t*dir[1]), -(start[2]+t*dir[2]), -(start[3]+t*dir[3]), normal);
End;
END;
END;
More Analytic Geometry

What about a torus shape?

Example: Ring on a space station


Reduces to a 4th-degree equation (a ray can
intersect the surface up to 4 times)
Need code for the general quartic formula

The highest-degree equation for which any closed
formula exists (Abel 1824)
More Analytic Geometry
Game Theory





Surprisingly: never mentioned in workplace
I use some concepts for design of tabletop
games
Example: Design of pieces or options in a
strategy or role-playing game
Don't want pieces so undervalued they never
get used in play (wasted design effort)
Iterated Elimination of Dominated Strategies
(IEDS; Aumann, 1976)
Game Theory




Simulation for Game Design (Collins,
presented at Johns Hopkins, 2014)
Simulate the game in a computer and run
each piece against all others thousands of
times
If a piece usually loses against all or most
other pieces, adjust cost downwards (or viceversa)
Balance iteratively; hardest part of game
design
Statistics



Of course, this procedure (with simulated dice
rolls, random terrain, weather, etc.) is only a
sampling of the possible play space, not a
complete census
So how many trials are needed to be
confident in the results (does X really beat Y)?
Answered by formula relating margin of error
for a proportion, confidence level, and sample
size
Statistics

For 95% confidence, z = 1.96 ≈ 2

For a proportion (0 to 1), max σ = 0.5

Assume desired margin of error E = 1% = 0.01

Then n = 10,000
Book of War (Collins, 2011)
Book of War (Collins, 2011)
Book of War (Collins, 2011)
Back to Computer Science


Once the “basic” units for a game have been
established, can cost-balance new additions
by a binary search on best-balanced cost
Simulating play against all other units in
system 10K times each per cycle
Back to Computer Science
// Binary search for best cost
while (highCost - lowCost > 1) {
int midCost = (highCost + lowCost)/2;
newUnit.setCost(midCost);
int midWins = assessGameSeries(newUnit, baseUnits);
if (midWins < expectWins) {
highCost = midCost;
highWins = midWins;
}
else {
lowCost = midCost;
lowWins = midWins;
}
}
Calculus Example


D&D 3rd Ed.: “If you were to draw a circle
using the measurements on the grid, with the
chosen intersection at the center, then if the
majority of a grid square lies within that
circle, the square is part of the spell's area.”
(DMG p. 69)
In the 3 in. radius circle on the next slide, is
the majority of the shaded square in the cicle
(in the spell's area)?
Calculus Example
Conclusions




Not exactly a recommendation for the
industry!
Very long working hours (“crunch time”), very
volatile, hit-driven industry
See “EA Spouse” blog posts
Also: Limited lifespan of the media works/
systems/ platforms
Conclusions


Consider the instersection of: (1) What are
your talents/joys, and (2) What will
help/satisfy people the most.
I bet: You'll use everything you know.
The End

Thank you!

Contact:

dcollins@kbcc.cuny.edu

deltasdnd.blogspot.com
Download