* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download Math for Programmers
Rotation matrix wikipedia , lookup
Jordan normal form wikipedia , lookup
Matrix (mathematics) wikipedia , lookup
Determinant wikipedia , lookup
Perron–Frobenius theorem wikipedia , lookup
Cayley–Hamilton theorem wikipedia , lookup
Non-negative matrix factorization wikipedia , lookup
Eigenvalues and eigenvectors wikipedia , lookup
System of linear equations wikipedia , lookup
Orthogonal matrix wikipedia , lookup
Exterior algebra wikipedia , lookup
Singular-value decomposition wikipedia , lookup
Gaussian elimination wikipedia , lookup
Cross product wikipedia , lookup
Laplace–Runge–Lenz vector wikipedia , lookup
Vector space wikipedia , lookup
Euclidean vector wikipedia , lookup
Matrix multiplication wikipedia , lookup
Covariance and contravariance of vectors wikipedia , lookup
Vectors & Matrices
Marq Singer (marq@essentialmath.com)
What Is a Vector?
• Geometric object with two properties
– direction
– length (if length is 1, is unit vector)
• Graphically represented by
Algebraic Vectors
• Any entity that meets certain rules (lies in
vector space) can be called ‘vector’
• Ex: Matrices, quaternions, fixed length
polynomials
• Mostly mean geometric vectors, however
Vector Space
• Set of vectors related by +,·
• Meet rules
–
–
–
–
–
–
–
v+w=w+v
(v + w) + u = v + (w + u)
v+0=v
v + (-v) = 0
() v = (v)
(+)v = v + v
(v + w) = v + w
(commutative +)
(associative +)
(identity +)
(inverse +)
(associative ·)
(distributive ·)
(distributive ·)
Real Vector Spaces
• Usually only work in these
• Rn is an n-dimensional system of real numbers
– Represented as ordered list of real numbers
(a1,…,an)
• R3 is the 3D world, R2 is the 2D world
Linear Combination
• Combine set of n vectors using addition and
scalar multiplication
– v = 1v1 + 2v2 + … + nvn
• Collection of all possible linear combinations
for given v1… vn is called a span
• Linear combination of 2 perpendicular vectors
span a plane
Linear Dependence
• A system of vectors v1, … ,vn is called linearly
dependant if for at least one vi
– vi = 1v1 +…+ i-1vi-1 + i+1vi+1 +…+ nvn
• Otherwise, linearly independent
• Two linearly dependant vectors are said to be
collinear
– I.e. w = .v
– I.e. they point the “same” direction
Vector Prerequisites
Linear Dependence
• Example
• Center vector can be constructed from outer
vectors
Vector Basis
• Ordered set of n lin. ind. vectors
– = { v1, v2, …, vn}
• Span n-dimensional space
• Represent any vector as linear combo
– v = 1v1 + 2v2 + … + nvn
• Or just components
– v = (1, 2, …, n)
Vector Representation
• 3D vector v represented by (x, y, z)
Use standard basis { i, j, k }
Unit length, perpendicular (orthonormal)
– v = xi + yj + zk
• Number of units in each axis direction
v3
v1
v2
Vector Operations
•
•
•
•
Addition: +,Scale: ·
Length: ||v||
Normalize: v̂
Addition
• Add a to b
a b a1 b1 ,a 2 b2 ,a3 b3
b
a
a+b
Scalar Multiplication
• change length of vector v by
α v (α v1 ,α v 2 ,α v3 )
Length
• Length
– ||v|| gives length (or Euclidean norm) of v
v v1 v 2 v3
2
2
2
– if ||v|| is 1, v is called unit vector
– usually compare length squared
• Normalize
– v scaled by 1/||v|| gives unit vector v̂
Vector Operations
• Games tend to use most of the common
vector operations
– Addition, Subtraction
– Scalar multiplication
• Two others are extremely common:
– Dot product
– Cross product
Dot product
• Also called inner product, scalar
product
a b a1 b1 a2 b2 a3 b3
a b a b cos(θ)
a
b
Dot Product: Uses
• a • a equals ||a||2
• can test for collinear vectors
– if a and b collinear & unit length, |a • b| ~ 1
– Problems w/floating point, though
• can test angle/visibility
– a • b > 0 if angle < 90°
– a • b = 0 if angle = 90° (orthogonal)
– a • b < 0 if angle > 90°
Dot Product: Example
• Suppose have view vector v and vector t to
object in scene (t = o - e)
• If v • t < 0, object behind us, don’t draw
e
o
t
v
Dot Product: Uses
• Projection of a onto b is
ab
projb a
b
bb
a
projb a
b
Dot Product: Uses
• Example: break a into components collinear
and perpendicular to b
a projb a
a
projb a
b
Cross Product
• Cross product: definition
a b (a2b3 a3b2 , a3b1 a1b3 , a1b2 a2b1 )
– returns vector perpendicular to a and b
– right hand rule
– length = area of parallelogram
c
b
a
Cross Product: Uses
• gives a vector perpendicular to the other two!
• ||a b|| = ||a|| ||b|| sin()
• can test collinearity
– ||a b|| = 0 if a and b are collinear
– Better than dot – don’t have to be normalized
Other Operations
• Several other vector operations used in games
may be new to you:
– Scalar Triple Product
– Vector Triple Product
• These are often used directly or indirectly in
game code, as we’ll see
Scalar Triple Product
• Dot product/cross product combo
u (v w)
• Volume of parallelpiped
u
v
w
• Test rotation direction
– Check sign
Triple Scalar Product: Example
• Current velocity v, desired direction d
on xy plane
• Take z ( v d)
• If > 0, turn left, if < 0, turn right
v
vd
d
v
d
vd
Vector Triple Product
• Two cross products
u (v w)
• Useful for building orthonormal basis
– Compute and normalize:
u
v u
u ( v u)
Points
• Points are positions in space — anchored to
origin of coordinate system
• Vectors just direction and length — freefloating in space
• Can’t do all vector operations on points
• But generally use one class in library
Point-Vector Relations
• Two points related by a vector
– (Q - P) = v
–P+v=Q
Q
v
P
Affine Space
• Vector, point related by origin
– (P - O) = v
–O+v=P
P
e3
v
O
e1
e2
• Vector space, origin, relation between them
make an affine space
Cartesian Frame
• Basis vectors {i, j, k}, origin (0,0,0)
• 3D point P represented by (px, py, pz)
• Number of units in each axis direction relative
to origin
pz
o
px
py
Affine Combination
• Like linear combination, but with points
– P = a1P1 + a2P2 + … + anPn
– a1,…,an barycentric coord., add to 1
• Same as point + linear combination
– P = P1 + a2 (P2-P1) + … + an (Pn-P1)
• If vectors (P2-P1), …, (Pn-P1) are linearly
independent, {P1, …, Pn} called a simplex
(think of as affine basis)
Convex Combination
• Affine combination with a1,…,an between 0
and 1
• Spans smallest convex shape surrounding
points – convex hull
• Example: triangle
Points, Vectors in Games
• Points used for models, position
– vertices of a triangle
• Vectors used for velocity, acceleration
– indicate difference between points, vectors
Parameterized Lines
• Can represent line with point and vector
– P + tv
Q
P
v
• Can also represent an interpolation from P to
Q
– P + t(Q-P)
– Also written as (1-t)P + tQ
Planes
• 2 non-collinear vectors span a plane
• Cross product is normal n to plane
n
Planes
• Defined by
– normal n = (A, B, C)
– point on plane P0
• Plane equation
– Ax+By+Cz+D = 0
– D=-(A·P0x + B·P0y + C·P0z)
Planes
• Can use plane equation to test locality of point
n
Ax+By+Cz+D > 0
Ax+By+Cz+D = 0
Ax+By+Cz+D < 0
• If n is normalized, gives distance to plane
Transformation
• Have some geometric data
• How to apply functions to it?
• Also desired: combine multiple steps into
single operation
• For vectors: linear transformations
Transformations
• A transformation T:VW is a function that maps
elements from vector space V to W
• The function
f(x, y) = x2 + 2y
is a transformation because it maps R2 into R
Linear Transformation
• Two basic properties:
– T(x + y) = T(x) + T(y)
– T(ax) = aT(x)
• Follows that
– T(0) = 0
– T(ax+y) = aT(x) + T(y)
Linear Transformations
• Basis vectors span vector space
• Know where basis goes, know where rest goes
• So we can do the following:
– Transform basis
– Store as columns in a matrix
– Use matrix to perform linear transforms
Linear Transforms
• Example:
T ( x, y ) ( x 2 y , 2 x y )
• (1,0) maps to (1,2)
• (0,1) maps to (2,1)
• Matrix is
1 2
2 1
What is a Matrix?
• Rectangular m x n array of numbers
• M rows by n columns
1.3 2.4 45.3
98
2.1 0
12 69 20
• If n=m, matrix is square
Matrix Concepts
• Number at row i and column j of matrix A is
element Aij
• Elements in row i make row vector
• Elems in column j make column vector
• If at least one Aii (diagonal from upper left to
lower right) are non-zero and all others are
zero, is diagonal matrix
Transpose
• Represented by AT
• Swap rows and columns along diagonal
T
1 2 3 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9
• ATij = Aji
• Diagonal is invariant
Transpose
• Transpose swaps transformed basis vectors
from columns to rows
• Useful identity
( AB )T BT AT
Transforming Vectors
•
•
•
•
Represent vector as matrix with one column
# of components = columns in matrix
Take dot product of vector w/each row
Store results in new vector
Ax b
Transforming Vectors
• Example: 2D vector
b11 b12 a1 a1b11 a2b12
b21 b22 a2 a1b21 a2b22
• Example: 3D vector to 2D vector
b11 b12
b21 b22
a1
b13 a1b11 a 2 b12 a3b13
a 2
b23 a1b21 a 2 b22 a3b23
a3
Row Vectors
•
•
•
•
Can also use row vectors
Transformed basis stored as rows
Dot product with columns
Pre-multiply instead of post-multiply
a1
b11 b12 a1b11 a2b21
a2
b21 b22 a1b12 a2b22
• If column default, represent row vector by vT
Row vs. Column
• Using column vectors, others use row vectors
– Keep your order straight!
M 3 M 2 M1 v
Column vector order (us, OpenGL)
v T M 1 M 2 M 3 Row vector order (DirectX)
• Transpose to convert from row to column (and
vice versa)
Matrix Product
• Want to combine transforms
(T S )( x) T ( S (x))
• What matrix represents(T S )?
• Idea:
– Columns of matrix for S are xformed basis
– Transform again by T
Matrix Product
a11 a12 b11 b12 a11 b11 a12 b21 a11 b12 a12 b22
a21 a22 b21 b22 a21 b11 a22 b21 a21 b12 a22 b22
a1T
T b1
a
2
or
a1 b1
b 2
a 2 b1
a1 b 2
a2 b2
• In general, element ABij is dot product of row i
from A and column j from B
Matrix product (cont’d)
• Number of rows in A must equal number of
columns in B
• Generally not commutative
AB BA
• Is associative
ABC AB C
Block Matrices
• Can represent matrix with submatrices
A B
C D
• Product of block matrix contains sums of
products of submatrices
A B E F AE BG AF BH
C D G H CE DG CF DH
Identity
• Identity matrix I is square matrix with main
diagonal of all 1s
1 0 0
0 1 0
0 0 1
• Multiplying by I has no effect
–AI=A
Inverse
• A-1 is inverse of matrix A such that
1
AA I
• A-1 reverses what A does
• A is orthogonal if AT = A-1
– Component vectors are at right angles and
unit length
– I.e. orthonormal basis
Computing Inverse
•
•
•
•
Only square matrices have inverse
Inverse doesn’t always exist
Zero row, column means no inverse
Use Gaussian elimination or Cramer’s rule (see
references)
Computing Inverses
• Most interactive apps avoid ever computing a
general inverse
• Properties of the matrices used in most apps
can simplify inverse
• If you know the underlying structure of the
matrix, you can use the following:
Computing Inverse
• If orthogonal, A-1 =AT
• Inverse of diagonal matrix is diagonal matrix
with A-1ii = 1/Aii
• If know underlying structure can use
1
1
(AB) B A
1
• We’ll use this to avoid explicit inverses
Storage Format
• Row major
– Stored in order of rows
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
– Used by DirectX
Storage Format (cont’d)
• Column Major Order
– Stored in order of columns
0
1
2
3
4 8 12
5 9 13
6 10 14
7 11 15
– Used by OpenGL, and us
Storage Format (cont’d)
• Note: storage format not the same as
multiplying by row vector
• Same memory footprint:
– Matrix for multiplying column vectors in column
major format
– Matrix for multiplying row vectors in row major
format
• I.e. two transposes return same matrix
System of Linear Equations
• Define system of m linear equations with n
unknowns
b1 = a11x1 + a12 x2 + … + a1n xn
b2 = a21x1 + a22 x2 + … + a2n xn
…
bm = am1x1 + am2 x2+ … + amn xn
References
• Anton, Howard and Chris Rorres, Elementary
Linear Algebra, 7th Ed, Wiley & Sons, 1994.
• Axler, Sheldon, Linear Algebra Done Right,
Springer Verlag, 1997.
• Blinn, Jim, Notation, Notation, Notation, Morgan
Kaufmann, 2002.
• Van Verth, James M. and Lars M. Bishop, Essential
Mathematics For Games & Interactive
Applications, Morgan Kaufmann, San Francisco,
2004.