class: center, middle # Creación de Videojuegos ### 2D Graphics --- class: center, middle # Lines --- # Lines * A *vertex* (plural: vertices) is a point in (2D or 3D) space * A *line* connects two vertices * Lines are the basic building blocks of 2D vector graphics * You can assemble lines into polygons * Later, we will also see how to use predrawn images --- class: small # Rasterization of Lines * How do we draw a line on the screen? * Pixels! * Rasterization: Take a line and map it to pixels * But pixels are arranged in a rectangular grid * How do we draw diagonal lines? * Idea: Draw ideal line, and for each column of pixels make the pixel black that is closest to the line! * Bresenham's algorithm! --- # Bresenham's algorithm
--- # Bresenham's algorithm: result
--- # Aliasing
--- class: small # Anti-Aliasing * What if we use other pixels that are touched by the line, too? * Maybe not to show them in black, but some sort of gray? * How do we determine "how gray" a pixel should be? * Supersampling! * Calculate more pixels than necessary, and take the average value --- # Supersampling
--- # Supersampling: Result
--- class: small # Anti-Aliasing
--- class: small # Polygons
image/svg+xml
* Can be convex or not * Can self-overlap * *Simple* polygons: No self-intersections --- class: small # (Simple) Polygons: Flood Fill
image/svg+xml
* Start at any pixel inside the polygon * If current pixel already has the target color, or the border color: return * Otherwise, color current pixel with target color and recursively call Flood Fill for all four neighboring pixels --- # Corner cutting
image/svg+xml
* What if we want a round corner? * Idea: Take parts of the lines near the corners and start cutting --- # Corner cutting
image/svg+xml
* Now cut each of these two line segments in half, and connect the midpoints --- # Corner cutting
image/svg+xml
* Now we have three line segments * Cut each in half again, and connect the end points --- # Corner cutting
image/svg+xml
--- # Corner cutting
image/svg+xml
--- # Corner cutting
image/svg+xml
--- # Corner cutting
image/svg+xml
--- # Corner cutting * We don't need to cut each line segment in half * We can use cuts of 1/3 to 2/3, or any other fraction * This allows us to control the rounding
image/svg+xml
--- # Corner cutting with ration 1/3 to 2/3
image/svg+xml
--- # Corner cutting with ration 1/3 to 2/3
image/svg+xml
--- # Corner cutting with ration 1/3 to 2/3
image/svg+xml
--- # Subdivision Surfaces * You can basically do the same thing in 3D!
--- # Curves * Sometimes we want to draw arbitrary curves * One approach: Perform corner cutting on a polygon * Drawback: Smoothness * Other approach: Splines! --- # Splines
--- # Splines
--- # Bezier Curves
--- class: center, middle # Sprites --- # Sprites * Sometimes we want pre-drawn art: *Sprites* * A sprite is a 2D image that is drawn into the scene * Old consoles and arcade machine had hardware support for sprites * Today they are very cheap to display * Sprites can also be animated! --- # Sprite Sheets
--- # Sprite Animations
--- # Sprite Animations
```JavaScript context.drawImage(image, frameIndex * width / numberOfFrames, clip*height, width / numberOfFrames, height, 0, 0, width / numberOfFrames, height); ``` --- # Goomba
--- # Goomba, animated
--- # Sprites in Unity
--- class: center, middle # Vectors --- # Vectors * How do we actually represent the location of a vertex/sprite and its movement in our game? 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 --- class: small # Vector Operations * A vector describes a *change* in position * A *vertex* is basically the change from the origin * For two vertices a and b, b - a gives you the change required to go **from a to b** * `\(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. * To turn a vector by 90 degrees: Exchange the coordinates and switch the sign of one, i.e. (x,y) becomes (-y,x) --- # Vector Operations: The Dot Product $$ \vec{A} \cdot \vec{B} = A_x \cdot B_x + A_y \cdot B_y\\\\ \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) --- # Dot Product: Projection
image/svg+xml
If B is a unit vector (with length 1): $$ \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 \)`) --- class: small # Dot Product: Example 2 * A car in a racing game is at `\(\begin{pmatrix} 3 \\\\ 3\end{pmatrix} \)` moving in direction `\(\begin{pmatrix} -4 \\\\ 3\end{pmatrix} \)` * There is a circular obstacle with radius 0.5 at `\(\begin{pmatrix} 0 \\\\ 4\end{pmatrix} \)`, and the car is 1 unit wide * If it keeps its present course, will the car collide with the obstacle? --- # Asodev Breakfast Tomorrow
--- class: center, middle # Next Time: 3D Graphics