class: center, middle # Creación de Videojuegos ### Vectors --- # Math Cady: *I like math.* Damian: *Eww. Why?* Cady: *Because it's the same in every country.* -- *Mean Girls (2004)* --- # Math * In video games we need a lot of math * In particular, we need a lot of vector math * Generally, game objects will be located somewhere, move, collide, etc. * Today we are going to revisit how vectors work --- class: center, middle # Vectors --- # Vectors * How do we represent positions, movements, etc.? Vectors! * A vector has 2 (or 3) components, called x and y (and z) * Addition and subtraction work component-wise * You can also multiply a vector with a real number --- # Vectors $$ \begin{pmatrix} x \\\\ y \end{pmatrix} + \begin{pmatrix} a \\\\ b \end{pmatrix} = \begin{pmatrix} x+a \\\\ y+b \end{pmatrix} $$ $$ a \cdot \begin{pmatrix} x \\\\ y \end{pmatrix} = \begin{pmatrix} a \cdot x \\\\ a \cdot y \end{pmatrix} $$ $$ \begin{pmatrix} x \\\\ y \end{pmatrix} - \begin{pmatrix} a \\\\ b \end{pmatrix} = \begin{pmatrix} x-a \\\\ y-b \end{pmatrix} $$ --- class: small # Vector Operations * A vector describes a *change* in position * As such, a vector does not itself have a position * That means, the vector that moves a point `a` by 3 units in x direction and 2 units in y direction is the same as the vector that moves a point `b` by 3 units in x direction and 2 units in y direction $$ \begin{pmatrix} 3 \\\\ 2 \end{pmatrix} $$ --- class: small # Vector Operations * For two points a and b, b - a gives you a vector: the change required to go **from a to b** * `\(t \cdot (b-a)\)` gives you a "part" of that change * `\(a + t \cdot (b-a) \)` gives you a point on the line from a to b. t determines where on the line that point is: - 0: The point is a - 0.5: The point is halfway between a and b - 1: The point is b - greater than 1: The point is on the opposite side of b than a - etc. * You can interpret `t` as a "percentage" --- # Example * The player is currently at `\((7,10)\)` * The enemy is at `\((1,2)\)` * Which direction does the enemy have to walk in, to get to the player? * What point is a quarter (0.25) of the way from the enemy to the player? --- # Velocity? * Now we know the "direction" the enemy has to walk in * But usually we want our game objects to move with constant velocity * How do we ensure that? -- * Unit vectors! --- # The length of a Vector $$ \left| \begin{pmatrix} x \\\\ y \end{pmatrix} \right| = \sqrt{x^2 + y^2} $$ $$ \left| \begin{pmatrix} x \\\\ y \\\\ z \end{pmatrix} \right| = \sqrt{x^2 + y^2 + z^2} $$ A **unit vector** is a vector with length 1. --- # Normalizing a Vector * A unit vector is useful to give us a general direction * We can later multiply it with the desired distance * To turn a vector into a unit vector, we divide it by its length $$ \vec{v}_n = \frac{\vec{v}}{|\vec{v}|} $$ --- # Example * The player is currently at `\((7,10)\)` * The enemy is at `\((1,2)\)` * The enemy is moving with 2 units/s towards the player * Where is the enemy after 2 seconds? * When will the enemy reach the point that is 80% of the way to the player? --- # Vector Rotation * To turn a vector by 90 degrees: Exchange the coordinates and switch the sign of one, i.e. (x,y) becomes (-y,x) * How could we return by other angles? * We'll talk about that next week * But maybe we could start by calculating the angle between two vectors? --- # Vector Operations: The Dot Product $$ \vec{A} \cdot \vec{B} = \begin{pmatrix} x_1 \\\\ y_1 \end{pmatrix} \cdot \begin{pmatrix} x_2 \\\\ y_2 \end{pmatrix} = x_1 \cdot x_2 + y_1 \cdot y_2\\\\ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| \cdot |\vec{B}| $$ * Very useful for a variety of applications! * Simplest: Calculate the angle between two vectors. * Calculate the projection of a point onto a vector (e.g. distance from a line) --- # Sin and Cos
--- # Projection $$ \vec{A} \cdot \vec{B} = \begin{pmatrix} x_1 \\\\ y_1 \end{pmatrix} \cdot \begin{pmatrix} x_2 \\\\ y_2 \end{pmatrix} = x_1 \cdot x_2 + y_1 \cdot y_2\\\\ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| \cdot |\vec{B}| $$ What if B is a unit vector? $$ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| $$ --- # Dot Product: Projection
image/svg+xml
If B is a unit vector: $$ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| $$ --- class: small # Dot Product: Example 1 * An AI controlled enemy is at `\(\begin{pmatrix} 1 \\\\ 1\end{pmatrix} \)` * They are currently looking in *direction* `\(\begin{pmatrix} 2 \\\\ 0\end{pmatrix} \)` * The player is at `\(\begin{pmatrix} 5 \\\\ 4\end{pmatrix} \)` * The enemy has a field of view of 45 degrees in each direction. Can it see the player? (`\(\cos(45) = \frac{\sqrt{2}}{2} \approx 0.7 \)`) --- # Dot Product in 3 Dimensions $$ \vec{A} \cdot \vec{B} = \begin{pmatrix} x_1 \\\\ y_1 \\\\ z_1 \end{pmatrix} \cdot \begin{pmatrix} x_2 \\\\ y_2 \\\\ z_2 \end{pmatrix} = x_1 \cdot x_2 + y_1 \cdot y_2 + z_1 \cdot z_2\\\\ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| \cdot |\vec{B}| $$ --- class: center, middle # Next Time: 3D Graphics
--- # References * [Linear Algebra Video Series](https://www.youtube.com/watch?v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab) * [Understanding the Dot Product](https://betterexplained.com/articles/vector-calculus-understanding-the-dot-product/) * [Matrix-Vector Multiplication](https://mathinsight.org/matrix_vector_multiplication)