Presentation is loading. Please wait.

Presentation is loading. Please wait.

Game Programming Algorithms and Techniques

Similar presentations


Presentation on theme: "Game Programming Algorithms and Techniques"— Presentation transcript:

1 Game Programming Algorithms and Techniques
Chapter 3 Linear Algebra for Games

2 Chapter 3 Objectives Vectors Learn the fundamentals of vectors
Learn the two products (dot and cross) and their uses Apply them to sample problems Learn about different coordinate systems Matrices Learn how to multiply matrices Transforming vectors by matrices

3 Vector Definition Represents a magnitude and direction in n-dimensional space, with one real number per dimension: For games, usually 2D or 3D vectors, though sometimes we use 4D in 3D games. Expressed as: No concept of position!

4 Vector field where all the vectors are equivalent
Vectors: No Position Where we draw a vector does not change its magnitude or direction, so it doesn't change the vector. These vectors all have the same value: Vector field where all the vectors are equivalent

5 Vector Terms Tail: where the vector starts.
Head: what the arrow is "pointing at." Although where a vector is drawn doesn't change its value, it's helpful to draw with the tail starting at the origin.

6 Vector Drawn w/ Tail at Origin
The head "points at" the point that happens to correspond to the x and y component of the vector. The 2D vector 〈1, 2〉 drawn with its tail at the origin and its head “pointing at” (1, 2)

7 Vector addition and the parallelogram rule
Is commutative.

8 Subtraction Vector subtraction From A to B is B – A.

9 Subtraction: From Point A to B
Represent points A and B as vectors drawn with their tails at the origin: Useful for a waypoint arrow, for example, which points from the player to the target. Representing points as vectors from the origin

10 Length and Length Squared
To calculate the length (or magnitude) of a vector: If we just need a comparison between the length of two vectors, avoid the square root by using length squared:

11 Unit Vectors and Normalization
Unit vectors have a length of 1. Drawn with a hat, like To convert a non-unit vector to a unit one, normalize the vector: Normalizing loses magnitude: If you care about magnitude, don't normalize. If you only care about direction, do normalize.

12 Scalar Multiplication

13 Angle between unit vectors
Dot Product Allows you to figure out the angle between two vectors: Angle between unit vectors

14 Scalar Projection Works with a normalized and an un-normalized vector

15 Dot Product Special Cases (For Normalized Vectors)
0 = Two vectors are perpendicular. 1 = Two vectors are parallel and facing in the same direction. -1 = Two vectors are parallel and facing in the opposite direction.

16 Dot Product Properties
Commutative, distributed over addition, and associative: Length squared is the same thing as dot product with self:

17 Problem: Vector Reflection
Steps to solve vector reflection

18 Vector Reflection Solution

19 Cross Product Only works with 3D vectors
Finds the vector perpendicular to the plane that contains a and b Cross product

20 "xyzzy" Mnemonic Tells the subscripts of the first row:
Then rotate components in the order x -> y -> z -> x to get the next two rows:

21 Cross Product Properties
Is anti-commutative: If the result of the cross product has 0 for every component, it means the two vectors are parallel.

22 Normal to a Triangle Vector that is perpendicular to the triangle:
The normal going into the page for triangle ABC (in a right-handed coordinate system)

23 Problem: Rotating a 2D Character
How do you know if you turn clockwise or counter-clockwise? The player needs to rotate to face the explosion.

24 Lerp from a to b with different f values
Linear Interpolation Lerp from a to b with different f values

25 Sample right-handed (a) and left-handed (b) coordinate systems
Which coordinate system you choose is arbitrary, but must be consistent! Sample right-handed (a) and left-handed (b) coordinate systems

26 Matrices A matrix is a grid of real numbers with m rows and n columns.
For games, 3x3 or 4x4 matrices are the most common.

27 Matrix Multiplication
Matrices A and B can be multiplied if the number of columns in A are equal to the number of rows in B:

28 Identity Matrix Multiplying a matrix by the identity matrix does not change its value: Number of rows/columns in I is equal to the number of columns in A: If a matrix is multiplied by its inverse, the result is identity:

29 Transposing a Matrix Rows become columns:

30 Representing a Vector as a Matrix
You can use either row-major or column-major. It doesn't matter which you select, but be consistent (most engines use row-major). Row-major: Column-major:

31 Multiply a Vector by a Matrix
If you change from row-major to column-major (or vice versa), you must transpose the matrix:


Download ppt "Game Programming Algorithms and Techniques"

Similar presentations


Ads by Google