Download presentation
Presentation is loading. Please wait.
Published bySandra Green Modified over 8 years ago
1
CSCE 552 Spring 2009 Game Architecture and Math By Jijun Tang
2
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
3
Component Systems Component system organization
4
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
5
Singleton Implements a single instance of a class with global point of creation and access For example, GUI Don't overuse it!!!
6
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
7
Observer Allows objects to be notified of specific events with minimal coupling to the source of the event Two parts subject and observer
8
Composite Allow a group of objects to be treated as a single object Very useful for GUI elements, hierarchical objects, inventory systems, etc
9
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
10
Ad-hoc
11
Modular
12
DAG
13
Layered
14
Sample Game Loop
15
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
16
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
17
Automatic Registration (h file) class MSG { public: virtual ~MSG() {} static void Register(const string&, MSG* (*)(istream&)); static MSG* Build(const string& s, istream& i); protected: typedef map MsgMap; static MsgMap keys; };
18
Automatic Registration (C++) MSG::MsgMap MSG::keys; void MSG::Register(const string& name, MSG* (*bldfunc)(istream &)) { keys[name] = bldfunc; } MSG* MSG::Build(const string & name, istream & is) { MsgMap::iterator i = keys.find(name); if (i == keys.end()) throw CodingError("No such message type"); return (*i->second)(is); // Go build an instance and return it }
19
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
20
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
21
Math
22
Applied Trigonometry Trigonometric functions Defined using right triangle x y h
23
Applied Trigonometry Angles measured in radians Full circle contains 2 radians
24
Applied Trigonometry Sine and cosine used to decompose a point into horizontal and vertical components r cos r sin r x y
25
Trigonometry
26
Trigonometric identities
27
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
28
arcs
29
Applied Trigonometry Law of sines Law of cosines Reduces to Pythagorean theorem when = 90 degrees b a c
30
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
31
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
32
Visualize Vectors The length represents the magnitude The arrowhead indicates the direction Multiplying a vector by a scalar changes the arrow ’ s length V 2V2V –V–V
33
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 W V + W V W V V – WV – W –W–W
34
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:
35
Add/Subtract Componentwise
36
Vector Magnitude The magnitude of an n-dimensional vector V is given by In three dimensions, this is
37
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
38
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
39
Matrix Representation The entry of a matrix M in the i-th row and j-th column is denoted M ij For example,
40
Matrix Transpose The transpose of a matrix M is denoted M T and has its rows and columns exchanged:
41
Vectors and Matrices An n-dimensional vector V can be thought of as an n 1 column matrix: Or a 1 n row matrix:
42
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
43
Vectors and Matrices Example matrix product
44
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:
45
Identity Matrix I n For any n n matrix M, the product with the identity matrix is M itself I n M = M MI n = M
46
Invertible An n n matrix M is invertible if there exists another matrix G such that The inverse of M is denoted M -1
47
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
48
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,
49
Determinant The determinant of a 3 3 matrix is
50
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
51
Inverse of 2x2 and 3x3 The inverse of a 2 2 matrix M is The inverse of a 3 3 matrix M is
52
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
53
Inverse of 4x4
54
General Matrix Inverse (AB) -1 = B -1 A -1 A general nxn matrix can be inverted using methods such as Gauss-Jordan elimination Gaussian elimination LU decomposition
55
Gauss-Jordan Elimination Gaussian Elimination
56
The Dot Product The dot product is a product between two vectors that produces a scalar The dot product between two n-dimensional vectors V and W is given by In three dimensions,
57
Dot Product Usage The dot product can be used to project one vector onto another V W
58
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
59
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
60
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)
61
Illustration The cross product satisfies the trigonometric relationship This is the area of the parallelogram formed by V and W V W ||V|| sin
62
Area and Cross Product The area A of a triangle with vertices P 1, P 2, and P 3 is thus given by
63
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
64
Rules in Figure
65
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
66
Alternative The cross product can also be expressed as the matrix-vector product The perpendicularity property means
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.