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

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

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

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.

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

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)

Logos-so-far

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

Lists

Hash Table

Stack/Queue/Priority Queue

Bits

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

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

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

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

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

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 ;

Observer

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

Two parts subject and observer

Composite

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

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

The Five StepDebugging Process

1. Reproduce the problem consistently

2. Collect clues

3. Pinpoint the error

4. Repair the problem

5. Test the solution

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)

Game Architecture

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

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

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

Ad-hoc

Modular

DAG

Layered

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

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

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

Tasks of Main Game Loop

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

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

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

Sample Game Loop

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

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

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

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

Math

Applied Trigonometry

Trigonometric functions Defined using right triangle

x

yh

Applied Trigonometry

Angles measured in radians

Full circle contains 2 radians

Applied Trigonometry

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

r cos

r sin r

x

y

Trigonometry

Trigonometric identities

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

arcs

Applied Trigonometry

Law of sines

Law of cosines

Reduces to Pythagorean theorem when = 90 degrees

b

a

c

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

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

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

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

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

Vectors and Matrices

Vectors add and subtract componentwise

Vectors and Matrices

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

In three dimensions, this is

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

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

Vectors and Matrices

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

For example,

Vectors and Matrices

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

Vectors and Matrices

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

Or a 1 n row matrix:

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

Vectors and Matrices

Example matrix product

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:

Identity Matrix In

For any n n matrix M,

the product with the

identity matrix is M itself InM = M

MIn = M

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

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

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

Determinant

The determinant of a 3 3 matrix is

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

Vectors and Matrices

The inverse of a 2 2 matrix M is

The inverse of a 3 3 matrix M is

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

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