52
Game Architecture 2/2/16: Linear Algebra II: Matrices

Game Architecture - Cognitive Science Departmentdestem/gamearch/3.pdf · Homework 2 The program should allow for an interactive prompt to call these methods. Upon initialization,

Embed Size (px)

Citation preview

Game Architecture2/2/16: Linear Algebra II: Matrices

θcosθ

sinθr = 1

sinθ=oppositehypotenuse

cosθ=adjacenthypotenuse

tanθ=oppositeadjacent

θ

x0 = xcosθ� ysinθy0 = xsinθ+ ycosθ

Dot Producta ·b= axbx+ayby+azbz = d

a ·b= |a||b|cos(θ)

a ·b= b ·a

a · (b+ c) = a ·b+a · c

sa ·b= a · sb= s(a ·b)

a ·a= |a|2

Enter the Matrix

M =

������

M11 M12 M13

M21 M22 M23

M31 M32 M33

������

⌘����x

0

y

0

���� =

����cos ✓ � sin ✓

sin ✓ cos ✓

����

����x

y

����y

0= x sin ✓ + y cos ✓

x

0= x cos ✓ � y sin ✓

����x

0

y

0

���� =

����cos ✓ � sin ✓

sin ✓ cos ✓

����

����x

y

����

or? ?

The Burning Question of Vector Representation

v =��a1 a2 a3

��v =

������

a1

a2

a3

������

v0 = (C(B(Av))) v0 = (((vA)B)C)

v=

������

a1a2a3

������

v0 =Mv v0 = vM

AB= C

a ·b= aTb= c

The Shape of Matrix Operations

c12 = a11b12+a12b22+a13b32

= 6=

Ab= c

����a11 a12 a13

a21 a22 a23

����

������

b11 b12 b13 b14

b21 b22 b23 b24

b31 b32 b33 b34

������

����c11 c12 c13 c14

c21 c22 c23 c24

����

������

c11

c21

c31

������

������

b11

b21

b31

������������

a11 a12 a13

a21 a22 a23

a31 a32 a33

������

������

b11

b21

b31

��������c11

����a11 a12 a13

��

������

x

0

y

0

z

0

������=

������

cos ✓ � sin ✓ 0

sin ✓ cos ✓ 0

0 0 1

������

������

x

y

z

������

������

x

y

z

������������

cos ✓ � sin ✓ 0

sin ✓ cos ✓ 0

0 0 1

������

������

(x cos ✓ � y sin ✓ + 0)

(x sin ✓ + cos ✓ + 0)

0 + 0 + z

������

Identity Matrix

AI⌘ IA⌘ A

I3⇥3 =

������

1 0 00 1 00 0 1

������

The inverse of a sequence of concatenated matrices can be written as the reverse concatenation of

the individual matrices’ inverses

(ABC)�1 = C�1B�1A�1

Transposition• The transpose of an orthonormal (pure

rotation) matrix is equal to it’s inverse (and much cheaper computationally!)

(ABC)T = CTBTAT

������

a b cd e fg h i

������

T

=

������

a d gb e hc f i

������

translation

rotation

scaleshear

Transformation Matrix

Affine Matrix

Transformation Matrix

+

Translations?• The result of translating a point p by a

translation t requires adding the components of t to the components of p individually:

p+ t= [(px+ tx),(py+ ty),(pz+ tz)]

p + t =

��������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

p

x

p

y

p

z

1

��������

��������

p

x

p

y

p

z

1

����������������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

(px

+ t

x

)(p

y

+ t

y

)(p

z

+ t

z

)1

��������

Points vs. Vectors (again)• What does it mean to rotate a point?

• What does it mean to rotate a vector?

• What does it mean to scale a point?

• What does it mean to scale a vector?

• What does it mean to translate a point?

• What does it mean to translate a vector?

Which Means• When we transform a vector by multiplying

it by a matrix, we want to ignore the translational effects of the matrix

Note!

��������

p

x

p

y

p

z

1

����������������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

(px

+ t

x

)(p

y

+ t

y

)(p

z

+ t

z

)1

��������

��������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

p

x

p

y

p

z

0

����������������

p

x

p

y

p

z

0

��������

Atomic Components

• Any affine transformation matrix can be created by concatenating a sequence of 4x4 matrices representing pure translation, pure rotations, and/or pure scale operations

Translation

• To invert a pure translation matrix, simply negate t. (i.e., -tx, -ty, -tz)

p + t =

��������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

p

x

p

y

p

z

1

��������

Rotation

• Within the upper-left 3x3, the 1 always appears on the axis we’re rotating about

• The inverse of a pure rotation is its transpose – inverting a rotation is equivalent to rotating by the negative angle

rotate

x

(r, �) =

��������

1 0 0 0

0 cos � � sin � 0

0 sin � cos � 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

rotate

y

(r, �) =

��������

cos � 0 sin � 0

0 1 0 0

� sin � 0 cos � 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

rotate

z

(r, ✓) =

��������

cos ✓ � sin ✓ 0 0

sin ✓ cos ✓ 0 0

0 0 1 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

Scale

• To invert a scaling matrix, substitute sx, sy, and sz with their reciprocals

• When the scale factor along all three axes is the same (sx = sy = sz), we call it uniform scale. To keep the mathematics of bounding sphere checks simple and fast, many engines will only allow uniform scale on renderable geometry or collision primitives

Sr =

��������

s

x

0 0 00 s

y

0 00 0 s

z

00 0 0 1

��������

��������

r

x

r

y

r

z

1

��������=

��������

s

x

r

x

s

y

r

y

s

z

r

z

1

��������

Notice Anything?

Sr =

��������

s

x

0 0 00 s

y

0 00 0 s

z

00 0 0 1

��������

��������

r

x

r

y

r

z

1

��������=

��������

s

x

r

x

s

y

r

y

s

z

r

z

1

��������

p + t =

��������

1 0 0 t

x

0 1 0 t

y

0 0 1 t

z

0 0 0 1

��������

��������

p

x

p

y

p

z

1

��������

rotate

x

(r, �) =

��������

1 0 0 0

0 cos � � sin � 0

0 sin � cos � 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

rotate

y

(r, �) =

��������

cos � 0 sin � 0

0 1 0 0

� sin � 0 cos � 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

rotate

z

(r, ✓) =

��������

cos ✓ � sin ✓ 0 0

sin ✓ cos ✓ 0 0

0 0 1 0

0 0 0 1

��������

��������

r

x

r

y

r

z

1

��������

Coordinate Spaces

• We can extend the idea of applying transforms to points and vectors to transforming rigid bodies – apply the transform matrix to all vertices of the mesh

Coordinate Spaces

• A point is a vector whose tail is fixed to the origin

• (origin of what?)

• A point is always expressed relative to a set of coordinate axes

Frames of Reference

yayb

xb

xa

PA = (2,4)

PB = (1,5)

Coordinate Spaces

• World Space

• Model Space

• View Space

• Perspective

From Python to JavaScript

Easy Translations…

Syntax is Easy

Literals are Easy

Data Structures… Less So

Objects are Both Fun and Annoying

JS Objects/Dicts

Functions

Variable Scope

Function Arguments

Undefined

More On Objects

Remember new!

More on Prototypes

Without Prototypes

Prototypes

this is Kinda Insane

Some Other WTFs

Arrays

Homework 2Write a C++ program/library that instantiates a 4x4 matrix class that contains (at least) the following methods:

translateX(float)

translateY(float)

translateZ(float)

rotateH(float)

rotateP(float)

rotateR(float)

scale(float) (Assume uniform scale)

translateXYZ(float, float, float)

translateXYZ(Vector3)

rotateHPR(float, float, float)

transformXYZHPRS(float, float, float, float, float, float, float)

reset()

Homework 2The program should allow for an interactive prompt to call these methods.

Upon initialization, the program will operate upon a cube, side length 2, lines parallel to the orthogonal axes, centered in world space at (0,0,0). Calling each method will perform the appropriate transformation and print both the cube’s new transformation matrix and the coordinates of each of the cube’s 8 vertices.

The reset() method returns the cube to it’s original transformation, with vertices at: (1, 1, 1) (1, 1, -1) (1, -1, 1) (1, -1, -1) (-1, 1, 1) (-1, 1, -1) (-1, -1, 1) (-1, -1, -1)