class: center, middle # Creación de Videojuegos ### Physics --- # Review Given a plane going through the point (1,2,3) with normal (0.71,0,-0.71), determine where a ray starting at (0,0,3) in direction (1,0,0) intersects that plane. Recall: $$ t = \frac{(\vec{p} - \vec{o}) \cdot \vec{n}}{\vec{d} \cdot \vec{n}} $$ --- class: center, middle # Bounding Volumes --- class: small # Bounding Volumes * Computing intersections between all pairs of arbitrary objects is expensive * Idea: Put a bigger object around each object for which intersections are easy to compute * Different bounding volumes: - Spheres: Easy to compute intersections, but may be too big - Boxes: Harder to compute intersections, but can be fitted pretty well - Axis-Aligned Boxes: Easy to compute intersections, better fit than spheres --- # Axis-Aligned Bounding Boxes
To test intersections: Check if x-projections *and* y-projections (and z-projections, in 3D) overlap. Sort-And-Sweep Algorithm: Sort start- and end-values of each interval, and iterate over them in sorted order. If there are two or more consecutive starts of intervals, the projections overlap. --- class: small # Hierarchical Representations * The idea of using larger bounding volumes to quickly eliminate unnecessary tests can be extended * For example, objects in one room will never collide with objects in a completely different room * Idea: Put multiple objects into a single bounding volume. If two such bounding volumes don't intersect, no object from one of them intersects with any object in the other. * Start with two objects, and put them into a combined bounding volume, then combine this larger bounding volume with another, etc. * We get a tree hierarchy of bounding volumes --- # Bounding Volume Tree
--- # Quadtrees/Octrees * Another idea is to split the space into fixed size parts, say cut each axis in half * This gives us 4 (in 2D), or 8 (in 3D) total parts * For any part in which there are objects, we can repeat this process * When any box only contains one object (and is sufficiently small), we can stop splitting * We can now test if something may intersect any object by checking the cell in which it falls in the tree --- # Octree
--- class: center, middle # Let there be light --- # The Phong Reflection Model * The Phong Reflection Model is an empirical model for how lights are reflected off of surfaces * Consists of ambient, diffuse and specular components
--- # The Phong Reflection Model
R
V
N
L
$$ I = k_a \cdot i_a + \sum_m \left(k_d (L_m \cdot N) i_d + k_s (R_m \cdot V)^\alpha i_s \right) $$ --- class: small # How do we use this information? * Flat shading: Calculate one value for the center of each triangle, and color the entire triangle that way * Gouraud shading: Calculate one value for each vertex of a triangle and interpolate the color in between * Phong shading: Interpolate normals between the vertices of a triangle, and calculate the color for each pixel
--- # Note * Phong reflection *model*: Equation to determine how light affects the color of a point * Phong shading/Gouraud shading/Flat shading: Methods to *use* this equation * Shader: The *implementation*/*program* that performs shading --- # Alternatives: Non-Photorealistic Shading * Toon/Cel shaders!
--- class: center, middle # Interpolation --- # Where do we use Interpolation * Whenever we have two (or more) values where we want intermediate values, we use interpolation * Values can be numbers, positions, rotations, colors, etc. * For example: A platform that moves between two end points * The simplest case: Linear interpolation * However, we can generalize! --- # The problem We have two values: a and b, and we want a function $$ f: [0,1] \mapsto [a,b] $$ such that $$ f(0) = a $$ $$ f(1) = b $$ (And values between 0 and 1 are between a and b) --- # Linear interpolation One such function, which we have called Linear Interpolation (Lerp) is: $$ f(t) = (1-t)\cdot a + t \cdot b $$ But this results in constant speed over time, meaning we start and stop abruptly. -- Unless ... -- What if we start and stop slowly? --- # Composing functions If we had a function $$ g : [0,1] \mapsto [0,1] $$ with $$ g(0) = 0, g(1) = 1 $$ we can compose it with f to get another interpolation function: $$ f(g(t)) $$ --- class: small # Useful functions How about a cosine?
Sine_one_period.svg - a nice plot of the sine function
Sine(sin)-function from Wikimedia Commons plot-range: 0 to 2pi plotted with three different cubic bezier-curves the bezier-controll-points are calculated to give a very accurate result. symbols in "Computer Modern" (TeX) font embedded created with a plain text editor using GNU/Linux about: http://commons.wikimedia.org/wiki/Image:Sine_one_period.svg source: http://commons.wikimedia.org/ rights: GNU Free Documentation license, Creative Commons Attribution ShareAlike license
But that's not mapping from [0,1] to [0,1]?! -- We need to do two things: * Map the input of the cosine from [0,1] to something else * Map the output of the cosine to [0,1] --- class: small # Useful functions Which area do we want?
Sine_one_period.svg - a nice plot of the sine function
Sine(sin)-function from Wikimedia Commons plot-range: 0 to 2pi plotted with three different cubic bezier-curves the bezier-controll-points are calculated to give a very accurate result. symbols in "Computer Modern" (TeX) font embedded created with a plain text editor using GNU/Linux about: http://commons.wikimedia.org/wiki/Image:Sine_one_period.svg source: http://commons.wikimedia.org/ rights: GNU Free Documentation license, Creative Commons Attribution ShareAlike license
-- Map to the domain: $$ c(t) = \cos(\pi + t*\pi) $$ --- class: small # Useful functions Which area do we want?
Sine_one_period.svg - a nice plot of the sine function
Sine(sin)-function from Wikimedia Commons plot-range: 0 to 2pi plotted with three different cubic bezier-curves the bezier-controll-points are calculated to give a very accurate result. symbols in "Computer Modern" (TeX) font embedded created with a plain text editor using GNU/Linux about: http://commons.wikimedia.org/wiki/Image:Sine_one_period.svg source: http://commons.wikimedia.org/ rights: GNU Free Documentation license, Creative Commons Attribution ShareAlike license
But we also need to fix the result: $$ g(t) = \cos(\pi + t*\pi)/2 + 0.5 $$ --- # Useful functions Here's another useful one: A sigmoid function
Graph of Logistics Curve
Originally Produced by GNUPLOT 4.2 patchlevel 2, hand compressed
0
0.5
1
−6
−4
−2
0
2
4
6
Corrected: $$ g(t) = \frac{1}{2\cdot(1+e^{-(12\cdot t - 6)})} + 0.5 $$ --- # Comparison
Unity gives you: `Mathf.SmoothStep` --- class: small # References * [Unity Documentation on SmoothStep](https://docs.unity3d.com/ScriptReference/Mathf.SmoothStep.html) * [How to Lerp like a Pro](https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/)