GAMES101-03 Transformation

Why Transformation

Modeling: translation, rotation, scaling viewing: (3D to 2D) projection

20220524013228

What is Transforms

Linear Transforms

Linear Transforms = Matrices(of the same dimension)

include Scale,Reflection,Shear,Rotate

\[ \begin{aligned} x^{\prime} &=a x+b y \\ y^{\prime} &=c x+d y \\ {\left[\begin{array}{c} x^{\prime} \\ y^{\prime} \end{array}\right] } &= \left[\begin{array}{ll} a & b \\ c & d \end{array}\right] \left[\begin{array}{l} x \\ y \end{array}\right] \\ \mathbf{x}^{\prime} &=\mathbf{M} \mathbf{x} \end{aligned} \]

Translation Calculate

cannot be represented in matrix form

\[ \left[\begin{array}{l} x^{\prime} \\ y^{\prime} \end{array}\right]=\left[\begin{array}{ll} a & b \\ c & d \end{array}\right]\left[\begin{array}{l} x \\ y \end{array}\right]+\left[\begin{array}{l} t_{x} \\ t_{y} \end{array}\right] \]

So, translation is NOT linear transform

Homogenous Coordinates

Add a third coordinate (w-coordinate)

  • 2D point = \((x, y, 1)^T\)
  • 2D vector = \((x, y, 0)^T\)

Matrix representation of translations

\[ \left(\begin{array}{c} x^{\prime} \\ y^{\prime} \\ w^{\prime} \end{array}\right)=\left(\begin{array}{ccc} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \\ 0 & 0 & 1 \end{array}\right) \cdot\left(\begin{array}{l} x \\ y \\ 1 \end{array}\right)=\left(\begin{array}{c} x+t_{x} \\ y+t_{y} \\ 1 \end{array}\right) \]

Valid operation if w-coordinate of result is 1 or 0

  • vector + vector = vector
  • point – point = vector
  • point + vector = point
  • point + point = ??

In homogeneous coordinates

\[ \left(\begin{array}{c} x \\ y \\ w \end{array}\right) \text { is the 2D point }\left(\begin{array}{c} x / w \\ y / w \\ 1 \end{array}\right), w \neq 0 \]

so, point1 + point2, is also the center of the two point

Affine Transformations

Affine map = linear map + translation

\[ \left(\begin{array}{l} x^{\prime} \\ y^{\prime} \end{array}\right)=\left(\begin{array}{ll} a & b \\ c & d \end{array}\right) \cdot\left(\begin{array}{l} x \\ y \end{array}\right)+\left(\begin{array}{l} t_{x} \\ t_{y} \end{array}\right) \]

\[ \left(\begin{array}{l} x^{\prime} \\ y^{\prime} \\ 1 \end{array}\right)=\left(\begin{array}{ccc} a & b & t_{x} \\ c & d & t_{y} \\ 0 & 0 & 1 \end{array}\right) \cdot\left(\begin{array}{l} x \\ y \\ 1 \end{array}\right) \]

2D Transforms

2D cale

\[ \mathbf{S}\left(s_{x}, s_{y}\right)=\left(\begin{array}{ccc} s_{x} & 0 & 0 \\ 0 & s_{y} & 0 \\ 0 & 0 & 1 \end{array}\right) \]

2D Rotation

\[ \mathbf{R}(\alpha)=\left(\begin{array}{ccc} \cos \alpha & -\sin \alpha & 0 \\ \sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 1 \end{array}\right) \]

2D Translation

\[ \mathbf{T}\left(t_{x}, t_{y}\right)=\left(\begin{array}{ccc} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \\ 0 & 0 & 1 \end{array}\right) \]

Composing Transforms

Sequence of affine transforms A1, A2, A3, ...

  • Compose by matrix multiplication
  • Very important for performance

\[ \begin{aligned} A_{n}\left(\ldots A_{2}\left(A_{1}(\mathbf{x})\right)\right)=& \mathbf{A}_{n} \cdots \mathbf{A}_{2} \cdot \mathbf{A}_{1} \cdot\left(\begin{array}{l} x \\ y \\ 1 \end{array}\right) \end{aligned} \]

Pre-multiply n matrices to obtain a single matrix representing combined transform

How to rotate around a given point c?

20220524021338

the right matrix is first!then the left!\(\mathbf{T}(\mathbf{c}) \cdot \mathbf{R}(\alpha) \cdot \mathbf{T}(-\mathbf{c})\)