Top Banner
CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang
73

CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

CSCE 590E Spring 2007

Game Architecture and Math

By Jijun Tang

Page 2: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Announcements

We will meet in 2A21 on Wednesday Please bring laptops (with mouse) on

Wednesday Small game due Friday, March 9th,

5:00pm Presentations start on Monday, March

5th.

Page 3: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Game Design Presentation

Two presentations, on March 5th and March 7th.

March 5th: Project Gnosis, Cheeze Puffs!, Team Swampus

March 7th: Space Banditos, Group E, Psychosoft

Each has 20 minutes to present, 5 minutes to answer questions

Page 4: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Contents for the Presentation

Description, specification, goals, game play System requirement, audience, rating Interface, input/output, interactions, cameras Premise/limitations/choices/resources Content designs, audio Level designs, flexibility Use case/UML (rough) Engines to use Version control/testing strategy Brief timeline (demo date is May 2nd-9th)

Page 5: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Logos-so-far

Page 6: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Data Structures: Array

Elements are adjacent in memory (great cache consistency) Requires continuous memory space

They never grow or get reallocated Use dynamic incremental array concept GCC has a remalloc function

In C++ there's no check for going out of bounds Use vector if possible Keep in mind of checking boundaries

Inserting and deleting elements in the middle is expensive

Page 7: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Lists

Page 8: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Hash Table

Page 9: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Stack/Queue/Priority Queue

Page 10: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Bits

Page 11: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Inheritance

Models “is-a” relationship Extends behavior of existing classes by

making minor changes Do not overuse, if possible, use component

systerm UML diagram representing inheritance

E ne m y B o s s Supe rD upe rB o s s

Page 12: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Polymorphism

The ability to refer to an object through a reference (or pointer) of the type of a parent class

Key concept of object oriented design C++ implements it using virtual functions

Page 13: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Multiple Inheritance

Allows a class to have more than one base class

Derived class adopts characteristics of all parent classes

Huge potential for problems (clashes, casting, dreaded diamond, etc)

Multiple inheritance of abstract interfaces is much less error prone (virtual inheritance)

Java has no multiple inheritance

Page 14: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Component Systems

Component system organization

G am e E nti ty

N am e = s w o r d

R e nde rC o m p C o ll is io nC o m p D am age C o m p P ic kupC o m p W ie ldC o m p

Page 15: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Object Factory

Creates objects by name Pluggable factory allows for new object

types to be registered at runtime Extremely useful in game development

for passing messages, creating new objects, loading games, or instantiating new content after game ships

Page 16: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Singleton

Implements a single instance of a class with global point of creation and access

For example, GUI Don't overuse it!!!

Single to ns ta tic S in g le to n & G etI n s tan c e ( ) ;/ / R eg u lar m em b er f u n c tio n s . . .

s ta t ic S in g le to n u n iq u eI n s tan c e ;

Page 17: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Observer

Allows objects to be notified of specific events with minimal coupling to the source of the event

Two parts subject and observer

Page 18: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Composite

Allow a group of objects to be treated as a single object

Very useful for GUI elements, hierarchical objects, inventory systems, etc

Page 19: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

The Five StepDebugging Process

1. Reproduce the problem consistently

2. Collect clues

3. Pinpoint the error

4. Repair the problem

5. Test the solution

Page 20: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Expert Debugging Tips

Question assumptions Minimize interactions and interference Minimize randomness Break complex calculations into steps Check boundary conditions, use assertions Disrupt parallel computations Exploit tools in the debugger (VC is good) Check code that has recently changed Explain the bug to someone else Debug with a partner (A second pair of eyes) Take a break from the problem Get outside help (call people)

Page 21: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Game Architecture

Page 22: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Overall Architecture

The code for modern games is highly complex The Sims: 3 million lines of code Xbox HD DVD player: 4.7 million lines MS Train Simulator has 1GB installed, with only

10MB executable With code bases exceeding a million lines of

code, a well-defined architecture is essential

Page 23: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Overall Architecture

Main structure Game-specific code Game-engine code

Both types of code are often split into modules, which can be static libraries, DLLs, or just subdirectories

Page 24: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Overall Architecture

Architecture types Ad-hoc (everything accesses everything) Modular DAG (directed acyclic graph) Layered

Options for integrating tools into the architecture Separate code bases (if there's no need to share

functionality) Partial use of game-engine functionality Full integration

Page 25: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Ad-hoc

Page 26: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Modular

Page 27: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

DAG

Page 28: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Layered

Page 29: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Overview: Initialization/Shutdown

The initialization step prepares everything that is necessary to start a part of the game

The shutdown step undoes everything the initialization step did, but in reverse order

Page 30: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Initialization/Shutdown

Resource Acquisition Is Initialization A useful rule to minimalize mismatch errors in the

initialization and shutdown steps Means that creating an object acquires and

initializes all the necessary resources, and destroying it destroys and shuts down all those resources

Optimizations Fast shutdown Warm reboot

Page 31: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Overview:Main Game Loop

Games are driven by a game loop that performs a series of tasks every frame

Some games have separate loops for the front and and the game itself

Other games have a unified main loop

Page 32: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Tasks of Main Game Loop

Handling time Gathering player input Networking Simulation Collision detection and response Object updates Rendering Other miscellaneous tasks

Page 33: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Main Game Loop

Structure Hard-coded loops Multiple game loops

For each major game state Consider steps as tasks to be iterated through

Coupling Can decouple the rendering step from simulation

and update steps Results in higher frame rate, smoother animation,

and greater responsiveness Implementation is tricky and can be error-prone

Page 34: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Execution Order of Main Loop

Most of the time it doesn't matter In some situations, execution order is

important Can help keep player interaction

seamless Can maximize parallelism Exact ordering depends on hardware

Page 35: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.
Page 36: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Sample Game Loop

Page 37: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Game Entities

What are game entities? Basically anything in a game world that can be interacted

with More precisely, a self-contained piece of logical interactive

content Only things we will interact with should become game entities

Organization Simple list Multiple databases Logical tree Spatial database

Page 38: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Creation and Updating

Object creation Basic object factories Extensible object factories Using automatic registration Using explicit registration

Updating Updating each entity once per frame can be too expensive Can use a tree structure to impose a hierarchy for updating Can use a priority queue to decide which entities to update

every frame

Page 39: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Level Instantiation

Loading a level involves loading both assets and the game state

It is necessary to create the game entities and set the correct state for them

Using instance data vs. template data

Page 40: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Identification and Communication

Identification Strings Pointers Unique IDs or handles

Communication Simplest method is function calls Many games use a full messaging system Need to be careful about passing and allocating

messages

Page 41: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Math

Page 42: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Applied Trigonometry

Trigonometric functions Defined using right triangle

x

yh

Page 43: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Applied Trigonometry

Angles measured in radians

Full circle contains 2 radians

Page 44: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Applied Trigonometry

Sine and cosine used to decompose a point into horizontal and vertical components

r cos

r sin r

x

y

Page 45: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Trigonometry

Page 46: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Trigonometric identities

Page 47: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Inverse trigonometric functions

Return angle for which sin, cos, or tan function produces a particular value

If sin = z, then = sin-1 z

If cos = z, then = cos-1 z

If tan = z, then = tan-1 z

Page 48: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

arcs

Page 49: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Applied Trigonometry

Law of sines

Law of cosines

Reduces to Pythagorean theorem when = 90 degrees

b

a

c

Page 50: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Scalars represent quantities that can be described fully using one value Mass Time Distance

Vectors describe a magnitude and direction together using multiple values

Page 51: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Examples of vectors Difference between two points

Magnitude is the distance between the points Direction points from one point to the other

Velocity of a projectile Magnitude is the speed of the projectile Direction is the direction in which it’s traveling

A force is applied along a direction

Page 52: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Vectors can be visualized by an arrow The length represents the magnitude The arrowhead indicates the direction Multiplying a vector by a scalar changes

the arrow’s length

V

2V

–V

Page 53: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Two vectors V and W are added by placing the beginning of W at the end of V

Subtraction reverses the second vector

V

W

V + W

V

W

V

V – W–W

Page 54: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

An n-dimensional vector V is represented by n components

In three dimensions, the components are named x, y, and z

Individual components are expressed using the name as a subscript:

1 2 3x y zV V V

Page 55: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Vectors add and subtract componentwise

Page 56: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

The magnitude of an n-dimensional vector V is given by

In three dimensions, this is

Page 57: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

A vector having a magnitude of 1 is called a unit vector

Any vector V can be resized to unit length by dividing it by its magnitude:

This process is called normalization

Page 58: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

A matrix is a rectangular array of numbers arranged as rows and columns A matrix having n rows and m columns is

an n m matrix At the right, M is a

2 3 matrix If n = m, the matrix is a square matrix

Page 59: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

The entry of a matrix M in the i-th row and j-th column is denoted Mij

For example,

Page 60: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

The transpose of a matrix M is denoted MT and has its rows and columns exchanged:

Page 61: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

An n-dimensional vector V can be thought of as an n 1 column matrix:

Or a 1 n row matrix:

Page 62: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Product of two matrices A and B Number of columns of A must equal

number of rows of B Entries of the product are given by

If A is a n m matrix, and B is an m p matrix, then AB is an n p matrix

Page 63: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Example matrix product

Page 64: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

Matrices are used to transform vectors from one coordinate system to another

In three dimensions, the product of a matrix and a column vector looks like:

Page 65: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Identity Matrix In

For any n n matrix M,

the product with the

identity matrix is M itself InM = M

MIn = M

Page 66: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Invertible

An n n matrix M is invertible if there exists another matrix G such that

The inverse of M is denoted M-1

1 0 0

0 1 0

0 0 1

n

MG GM I

Page 67: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Properties of Inverse

Not every matrix has an inverse A noninvertible matrix is called singular Whether a matrix is invertible can be

determined by calculating a scalar quantity called the determinant

Page 68: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Determinant

The determinant of a square matrix M is denoted det M or |M|

A matrix is invertible if its determinant is not zero

For a 2 2 matrix,

deta b a b

ad bcc d c d

Page 69: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Determinant

The determinant of a 3 3 matrix is

Page 70: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Inverse

Explicit formulas exist for matrix inverses These are good for small matrices, but

other methods are generally used for larger matrices

In computer graphics, we are usually dealing with 2 2, 3 3, and a special form of 4 4 matrices

Page 71: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

The inverse of a 2 2 matrix M is

The inverse of a 3 3 matrix M is

Page 72: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

A special type of 4 4 matrix used in computer graphics looks like

R is a 3 3 rotation matrix, and T is a translation vector

11 12 13

21 22 23

31 32 33

0 0 0 1

x

y

z

R R R T

R R R T

R R R T

M

Page 73: CSCE 590E Spring 2007 Game Architecture and Math By Jijun Tang.

Vectors and Matrices

The inverse of this 4 4 matrix is

1 1 1 111 12 13

1 1 1 1 1 121 22 23

1

1 1 1 131 32 33

1 0 0 0 1

x

y

z

R R R

R R R

R R R

R T

R R T R TM

R T

0