72
CSCE 552 Spring 2011 Math By Jijun Tang

CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

CSCE 552 Spring 2011

Math

By Jijun Tang

Page 2: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Languages

C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Page 3: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 4: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Lists

Page 5: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Hash Table

Page 6: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Stack/Queue/Priority Queue

Page 7: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Bits

Page 8: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 9: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 10: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 11: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 tic S in g le to n u n iq u eI n s tan c e;

Page 12: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Adapter

Convert the interface of a class into another interface clients expect.

Adapter lets classes work together that couldn't otherwise because of incompatible interfaces

Real interface

Page 13: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Observer

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

Two parts subject and observer

Page 14: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Ad-hoc

Page 15: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Modular

Page 16: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

DAG

Page 17: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Layered

Page 18: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 19: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 20: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 the game itself

Other games have a unified main loop Must finish a loop within 0.017 second

Page 21: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Tasks of Main Game Loop

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

Page 22: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox
Page 23: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Sample Game Loop

Page 24: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 25: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 26: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Example (Open Source Rail Simulator)

Simulator = new Simulator(settings, args[0]);

if (args.Length == 1)Simulator.SetActivity(args[0]);

elseSimulator.SetExplore(args[0], args[1], args[2], args[3], args[4]);

Simulator.Start();

Viewer = new Viewer3D(Simulator);

Viewer.Initialize();

Viewer.Run(); //Frame Updates Here

Simulator.Stop(); //Check if exit condition met

Page 27: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Math

Page 28: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Applied Trigonometry

Trigonometric functions Defined using right triangle

x

yh

Page 29: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Applied Trigonometry

Angles measured in radians

Full circle contains 2 radians

Page 30: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Applied Trigonometry

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

r cos

r sin r

x

y

Page 31: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Trigonometry

Page 32: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Trigonometric identities

Page 33: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 34: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

arcs

Page 35: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Applied Trigonometry

Law of sines

Law of cosines

Reduces to Pythagorean theorem when = 90 degrees

b

a

c

Page 36: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Vectors and Scalars

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 37: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 38: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Visualize Vectors

The length represents the magnitude The arrowhead indicates the direction Multiplying a vector by a scalar

changes the arrow’s length

V2V

–V

Page 39: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Vectors Add and Subtraction

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

Subtraction reverses the second vector

V

WV + W

V

W

V

V – W–W

Page 40: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

High-Dimension Vectors

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 41: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Add/Subtract Componentwise

Page 42: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Vector Magnitude

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

In three dimensions, this is

Page 43: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Vector Normalization

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 44: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 45: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Matrix Representation

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

For example,

Page 46: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Matrix Transpose

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

Page 47: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 48: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 49: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Vectors and Matrices

Example matrix product

Page 50: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 51: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Identity Matrix In

For any n n matrix M,the product with theidentity matrix is M itself

InM = M MIn = M

Page 52: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 53: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 54: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 55: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Determinant

The determinant of a 3 3 matrix is

Page 56: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

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 57: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Inverse of 2x2 and 3x3

The inverse of a 2 2 matrix M is

The inverse of a 3 3 matrix M is

Page 58: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

4x4

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 59: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Inverse of 4x4

1 1 1 111 12 13

1 1 1 1 1 121 22 23

11 1 1 1

31 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

Page 60: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

General Matrix Inverse

(AB) -1 = B-1A-1

A general nxn matrix can be inverted using methods such as Gauss-Jordan elimination Gaussian elimination LU decomposition

Page 61: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Gauss-Jordan Elimination

Gaussian Elimination

Page 62: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

The Dot Product

The dot product is a product between two vectors that produces a scalar

The dot product between twon-dimensional vectors V and W is given by

In three dimensions,

Page 63: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Dot Product Usage

The dot product can be used to project one vector onto another

V

W

Page 64: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Dot Product Properties

The dot product satisfies the formula

is the angle between the two vectors Dot product is always 0 between

perpendicular vectors If V and W are unit vectors, the dot

product is 1 for parallel vectors pointing in the same direction, -1 for opposite

Page 65: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Self Dot Product

The dot product of a vector with itself produces the squared magnitude

Often, the notation V 2 is used as shorthand for V V

Page 66: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

The Cross Product

The cross product is a product between two vectors the produces a vector

The cross product only applies in three dimensions The cross product is perpendicular to both

vectors being multiplied together The cross product between two parallel

vectors is the zero vector (0, 0, 0)

Page 67: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Illustration

The cross product satisfies the trigonometric relationship

This is the area ofthe parallelogramformed byV and W

V

W

||V|| sin

Page 68: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Area and Cross Product

The area A of a triangle with vertices P1, P2, and P3 is thus given by

Page 69: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Rules

Cross products obey the right hand rule If first vector points along right thumb, and

second vector points along right fingers, Then cross product points out of right palm

Reversing order of vectors negates the cross product:

Cross product is anticommutative

Page 70: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Rules in Figure

Page 71: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Formular

The cross product between V and W is

A helpful tool for remembering this formula is the pseudodeterminant (x, y, z) are unit vectors

Page 72: CSCE 552 Spring 2011 Math By Jijun Tang. Languages C/C++ Java Script: Flash, Python, LISP, etc. C# XNA for PC and Xbox

Alternative

The cross product can also be expressed as the matrix-vector product

The perpendicularity property means