GAMES101 Lecture 03
GAMES101 Lecture 03
Lecture 3:
Transformation
http://www.cs.ucsb.edu/~lingqi/teaching/games101.html
Announcements
• Assignment 0 will be released soon
• Dot Product
- Forward / backward (dot product positive / negative)
• Cross Product
- Left / right (cross product outward / inward)
• Matrices
• Today
- Why study transformation
- Homogeneous coordinates
- Composing transforms
- 3D transformations
- Modeling
- Viewing
• 2D transformations
• Homogeneous coordinates
Modeling: 6translation
GAMES101 Lingqi Yan, UC Santa Barbara
Why Transformation?
Modeling: rotation
GAMES101 7 Lingqi Yan, UC Santa Barbara
Why Transformation?
Modeling: scaling
GAMES101 8 Lingqi Yan, UC Santa Barbara
Why Transformation?
• 2D transformations
• Homogeneous coordinates
S0.5
CS184/284A Ren Ng
GAMES101 11 Lingqi Yan, UC Santa Barbara
Scale Transform
S0.5
0
x = sx
0
y = sy
CS184/284A Ren Ng
GAMES101 12 Lingqi Yan, UC Santa Barbara
Scale Matrix
S0.5
0
x s 0 x
0 =
y 0 s y
CS184/284A Ren Ng
GAMES101 13 Lingqi Yan, UC Santa Barbara
Scale (Non-Uniform)
S0.5,1.0
x0 sx 0 x
0 =
<latexit sha1_base64="1HluD7eaew2ZbEeE5TqBR+iyFhc=">AAACW3icbVFdS8MwFE07dXNOnYpPvgSHzqfRiqAvwtAXHye4D1hHSbO7LZimJUllpexP+qQP/hUx24q4zQshh3PP/chJEHOmtON8WnZha3unWNot71X2Dw6rR8cdFSWSQptGPJK9gCjgTEBbM82hF0sgYcChG7w+zvPdN5CKReJFpzEMQjIWbMQo0Ybyq9ILYMxEFoRESzadTeueh9O6B2L4y92vaZQ/xZfYMULH3MpPV9TrDef9VgR+teY0nEXgTeDmoIbyaPnVd28Y0SQEoSknSvVdJ9aDjEjNKIdZ2UsUxIS+kjH0DRQkBDXIFt7M8IVhhngUSXOExgv2b0VGQqXSMDBKs99Erefm5H+5fqJHd4OMiTjRIOhy0CjhWEd4bjQeMglU89QAQiUzu2I6IZJQbb6jbExw15+8CTrXDdfg55ta8yG3o4TO0Dm6Qi66RU30hFqojSj6QN9W0SpZX3bBLtuVpdS28poTtBL26Q8Oh7dR</latexit>
y 0 sy y
CS184/284A Ren Ng
GAMES101 14 Lingqi Yan, UC Santa Barbara
Reflection Matrix
Horizontal reflection: 0
0
x =??x 0
1 0 x
x = x 0 0 =
y =??y 0 1 y
<latexit sha1_base64="Aph81PCX6TVEP4rZzaxM2EuX/K0=">AAAB7HicbZDNSgMxFIVv6l+tf1WXboJFdGOZEUE3QtGNywpOW2iHkkkzbWgmMyQZaRn6DG5cKOLWB3Ln25i2s9DWA4GPc+8l954gEVwbx/lGhZXVtfWN4mZpa3tnd6+8f9DQcaoo82gsYtUKiGaCS+YZbgRrJYqRKBCsGQzvpvXmE1Oax/LRjBPmR6QvecgpMdbyRqc356NuueJUnZnwMrg5VCBXvVv+6vRimkZMGiqI1m3XSYyfEWU4FWxS6qSaJYQOSZ+1LUoSMe1ns2Un+MQ6PRzGyj5p8Mz9PZGRSOtxFNjOiJiBXqxNzf9q7dSE137GZZIaJun8ozAV2MR4ejnuccWoEWMLhCpud8V0QBShxuZTsiG4iycvQ+Oi6lp+uKzUbvM4inAEx3AGLlxBDe6hDh5Q4PAMr/CGJHpB7+hj3lpA+cwh/BH6/AEOpI4t</latexit>
0
y =y <latexit sha1_base64="xpWqfMGp4qSykFNTx9VbZxi6dgo=">AAAB63icbZDLSgMxFIbP1Futt6pLN8EiuiozUtCNUHTjsoK9QDuUTJppQ5PMkGSEYegruHGhiFtfyJ1vY6adhbb+EPj4zznknD+IOdPGdb+d0tr6xuZWebuys7u3f1A9POroKFGEtknEI9ULsKacSdo2zHDaixXFIuC0G0zv8nr3iSrNIvlo0pj6Ao8lCxnBJrfS85t0WK25dXcutApeATUo1BpWvwajiCSCSkM41rrvubHxM6wMI5zOKoNE0xiTKR7TvkWJBdV+Nt91hs6sM0JhpOyTBs3d3xMZFlqnIrCdApuJXq7l5n+1fmLCaz9jMk4MlWTxUZhwZCKUH45GTFFieGoBE8XsrohMsMLE2HgqNgRv+eRV6FzWPcsPjVrztoijDCdwChfgwRU04R5a0AYCE3iGV3hzhPPivDsfi9aSU8wcwx85nz+neY34</latexit>
<latexit sha1_base64="oFzHmF6CJilJ4bjZITeKNEAOwmU=">AAACWHicbVHLSgMxFM2MrX34qnXpJli0biwzIuhGKLpxWcE+oFNKJnOnDc1khiQjLUN/UnChv+LG9IHY6oWQw7nnPnLiJ5wp7Tgflr2Ty+8WiqXy3v7B4VHluNpRcSoptGnMY9nziQLOBLQ10xx6iQQS+Ry6/uRxke++glQsFi96lsAgIiPBQkaJNtSwEns+jJjI/Ihoyabzad3z8KzugQh+uPstzZWLL7BjdI653Q3pdrdFsw3BsFJzGs4y8F/grkENraM1rLx5QUzTCISmnCjVd51EDzIiNaMc5mUvVZAQOiEj6BsoSARqkC2NmeNzwwQ4jKU5QuMl+7siI5FSs8g3SrPfWG3nFuR/uX6qw7tBxkSSahB0NShMOdYxXriMAyaBaj4zgFDJzK6YjokkVJu/KBsT3O0n/wWd64Zr8PNNrfmwtqOITtEZukQuukVN9IRaqI0oekdfVs7KW582sgt2aSW1rXXNCdoIu/oN3QK1LQ==</latexit>
CS184/284A Ren Ng
GAMES101 15 Lingqi Yan, UC Santa Barbara
Shear Matrix
Hints:
0
Horizontal shift is 0 at y=0
x =?? x0 1 a x
0 0 =
Horizontal shift is a at y=1
y =?? y 0 1
<latexit sha1_base64="X7xTdjIFiturU9Y0o8RuD4rXvdI=">AAACV3icbVHLTgIxFO0MiIgv0KWbRqK4IjPGRDcmRDcuMZFHwkxIp1ygodOZtB3DZMJPGjf8ihstjxgBb9L05NxzHz0NYs6Udpy5Zefye4X94kHp8Oj45LRcOWurKJEUWjTikewGRAFnAlqaaQ7dWAIJAw6dYPK8yHfeQSoWiTedxuCHZCTYkFGiDdUvCy+AERNZEBIt2XQ2rXkeTmseiMEv97ilcfE1JkbmmNvdUG43W/TaEPTLVafuLAPvAncNqmgdzX75wxtENAlBaMqJUj3XibWfEakZ5TAreYmCmNAJGUHPQEFCUH629GWGrwwzwMNImiM0XrJ/KzISKpWGgVGa/cZqO7cg/8v1Ej188DMm4kSDoKtBw4RjHeGFyXjAJFDNUwMIlczsiumYSEK1+YqSMcHdfvIuaN/WXYNf76qNp7UdRXSBLtENctE9aqAX1EQtRNEn+rJyVt6aW992wS6upLa1rjlHG2FXfgDNkLYm</latexit>
y
Vertical shift is always 0
CS184/284A Ren Ng
GAMES101 16 Lingqi Yan, UC Santa Barbara
Rotate (about the origin (0, 0), CCW by default)
R45
CS184/284A Ren Ng
GAMES101 17 Lingqi Yan, UC Santa Barbara
Rotation Matrix
x0 x1 x2 x3 y
x Lecture xy Lecture 3 Math
+ y ✓ 3xxMath
Lecture 3 Math
cos ✓ sin ✓ Lecture 3 Math
x0 x1 f (x)
x2 x3 x0 y x1 x2 x3
x + yx0 ✓ xx1x x0x xx2y x
+ yx x✓ yx
1 3 2 x x
3
xyy
f (x + y)
cos=✓f (x)
sin+✓ fx(y)
+ y cos✓ ✓ xxsin ✓xy
x+y ✓
f (x) cos ✓ f (x)
sin ✓
x0 ) R✓ (x1 ) R✓ (x2 ) R✓ (x3 ) R✓ (ax) aR✓ (x) R✓ (y)
f
(ax) = af (x) f (x)
cos ✓ sin ✓f (x)
R✓ f=
(x + y) = f (x) + ff(x + y) = f (x) + f (y
(y)
sin ✓ cos ✓
f (x) = af (x)f (x + y) = f (x) + f (y)
CS184/284A Ren Ng
GAMES101
f (x + y) = f (x) + f (y)
18 Lingqi Yan, UC Santa Barbara
Linear Transforms = Matrices
(of the same dimension)
0
x = ax + by
0
y = cx + dy
0
x a b x
0 =
y c d y
0
x =Mx
CS184/284A Ren Ng
GAMES101 19 Lingqi Yan, UC Santa Barbara
Questions?
Today
• Why study transformation
• 2D transformations
• Homogeneous coordinates
- Why homogeneous coordinates
- Affine transformation
Ttx,ty
T 1,1
CS184/284A Ren Ng
GAMES101 22 Lingqi Yan, UC Santa Barbara
Translation??
Ttx,ty
T 1,1
0
x = x + tx
0
y = y + ty
CS184/284A Ren Ng
GAMES101 23 Lingqi Yan, UC Santa Barbara
Why Homogeneous Coordinates
• Translation cannot be represented in matrix form
0
x a b x tx
0 = +
<latexit sha1_base64="N69qeTDb59xHCbfPH0Qfs7gRUbs=">AAACfXicbZFdS8MwFIbT+j2/pl56Exx+y2hF0BtB9MZLBafCOkqans2wNC3JqTjK/oW/zDv/ijeaziGueiDk5T1PkpNzokwKg5737rhT0zOzc/MLtcWl5ZXV+tr6vUlzzaHFU5nqx4gZkEJBCwVKeMw0sCSS8BD1r8r8wzNoI1J1h4MMOgnrKdEVnKG1wvprEEFPqCJKGGrxMnzZDQI62A1AxT/eeYVhdIdGFuN2jyfI6mXlXRPAYYXAsGQwnKTCesNreqOgf4U/Fg0yjpuw/hbEKc8TUMglM6btexl2CqZRcAnDWpAbyBjvsx60rVQsAdMpRt0b0m3rxLSbarsU0pH7+0TBEmMGSWRJW9+TqeZK879cO8fuWacQKssRFP9+qJtLiiktR0FjoYGjHFjBuBa2VsqfmGYc7cBqtgl+9ct/xf1x07f69qRxcTluxzzZJFtkj/jklFyQa3JDWoSTD4c6+86B8+luu0du8xt1nfGZDTIR7ukX89XFpw==</latexit>
y c d y ty
(So, translation is NOT linear transform!)
In homogeneous coordinates,
0 1 0 1
x x/w
@ y A is the 2D point @y/w A , w 6= 0
w 1
CS184/284A Ren Ng
GAMES101 26 Lingqi Yan, UC Santa Barbara
Affine Transformations
CS184/284A Ren Ng
GAMES101 27 Lingqi Yan, UC Santa Barbara
2D Transformations
Scale ⇥
sx 0 0
S(sx , sy ) = ⇤ 0 sy 0⌅
0 0 1
Rotation ⇥
cos sin 0
R( ) = ⇤ sin cos 0⌅
0 0 1
Translation
⇥
1 0 tx
T(tx , ty ) = ⇤0 1 ty ⌅
0 0 1
CS184/284A Ren Ng
GAMES101 28 Lingqi Yan, UC Santa Barbara
Inverse Transform
1
M
1 1
M is the inverse of transform M in both a matrix
and geometric sense
1
M
CS184/284A Ren Ng
GAMES101 29 Lingqi Yan, UC Santa Barbara
Composing Transforms
CS184/284A Ren Ng
GAMES101 31 Lingqi Yan, UC Santa Barbara
Translate Then Rotate?
CS184/284A Ren Ng
GAMES101 32 Lingqi Yan, UC Santa Barbara
Rotate Then Translate
CS184/284A Ren Ng
GAMES101 33 Lingqi Yan, UC Santa Barbara
Transform Ordering Matters!
Transform
M = ROrdering M≠=Matters!
45 · T(1,0) T(1,0) · R45
Recall the matrix math represented by these symbols:
Matrix multiplication is not commutative
2 32 3 2 32 3
cos 45 sin 45 0 1 0 1 1 0 1 cos 45 sin 45 0
4 sin 45 M
cos = 05 ·40T(1,0)
45 R45 1 M05 6== ≠ 1 05·4R
40T(1,0) sin45
45 cos 45 05
0 0 1 0 0 1 0 0 1 0 0 1
Recall the matrix math represented by these symbols:
Note
2
that matrices
32
are applied
3 2
right
32
to left: 3
cos 45 sin 45 2x03 121 0 1 3 2 1 0 1 cos 45 3 2
0 1 cos 45 sin
sin 45 0 x
345 0
4 sin 45 cos 45 05 404 1 055 6 40 1 05 4 sin 45 5cos 05
4 5
T(1,0) · R45 y = 0 1 0 = 4 sin 45 cos 45 0 4y455
0 0 11 0 0 0 01 1 0 00 1 0 0 1 10 1
CS184/284A Ren Ng
Note that matrices are applied right to left:
2 3 2 32 32 3
x 1 0 1 cos 45 sin 45 0 x
T(1,0) · R45 4y 5 = 40 1 05 4 sin 45 cos 45 05 4y 5
1 0 0 1 0 0 1 1
CS184/284A
GAMES101 35 Lingqi Yan, UCRen
SantaNg
Barbara
Composing Transforms
CS184/284A Ren Ng
GAMES101 36 Lingqi Yan, UC Santa Barbara
Decomposing Complex Transforms
T( c) R( ) T(c)
Matrix representation?
T(c) · R( ) · T( c)
CS184/284A Ren Ng
GAMES101 37 Lingqi Yan, UC Santa Barbara
3D Transforms
GAMES101
CS184/284A 39 Lingqi Yan, UC Santa
RenBarbara
Ng
Use homogeneous coordinates again:
• 3D point = (x, y, z, 1)T
3D Transformations
• 3D vector = (x, y, z, 0)T
Use homogeneous coordinates again:
Use 4×4 matrices for affine transformations
• 3D point = (x, y, z, 1)T
⇥ ⇥ ⇥
• 3D vector ⇧ x
= (x,
y ⌃
y, z, a
0) T b c t x
⇧d e f ty ⌃ ⇧y ⌃
x
⇧ ⌃ = ⇧ ⌃·⇧ ⌃
⇤z ⌅ ⇤ g h i tz ⌅ ⇤ z ⌅
Use 4×4 matrices
1 for affine
0 0transformations
0 1 1
⇥ ⇥ ⇥
x a b c tx x
What’s the order?
⇧ ⌃ ⇧ ⌃ ⇧ ⌃
y d e f t y
Linear Transform
CS184/284A ⇧ ⌃
⇤z ⌅first
= or
⇧Translation first?
y ⌃·⇧ ⌃
⇤ g h i tz ⌅ ⇤ z ⌅
Ren Ng
1 0 0 0 1 1