Game and animation control flow CSE 3541 Matt Boggus
Overview Pseudocode for a game/animation loop Undo/redo example Tradeoff in saving cause vs. effect Unity transform and hierarchy computations as saving the effect
Game/Animation loop main(){ Initialize(); while(true){ DrawScene(); HandleEvents(); Update(); }
Draw() and HandleEvents() Draw() handled by Unity Types of events I/O Timers Scripted events Network message
Initialize frame time Scene/Hierarchy objects Initially 0, increases by 1 every time the scene is drawn time Initially 0, increases by dt (a.k.a. timestep, Δt) every update Units might be seconds, minutes, or days, etc. Unity Time and Framerate Management Scene/Hierarchy objects Geometric objects Lights Camera
Update() P’(time+dt) = P(time) + Displacement(dt) Displacement(dt) is the change in position over the amount of time, dt, passing Can have similar equation for change in orientation How is Displacement(dt) calculated? Real-time user input Physics Behavior Key-frame data / Interpolation (Motion capture – save positions at each frame instead of computing displacement)
Time control or frame selection Braid (2008) Autodesk MotionBuilder Image from http://www.mobygames.com/game/xbox360/braid/screenshots/ Image from http://www.autodesk.com/products/motionbuilder/overview
Two approaches P(time-dt) = P’(time) - Displacement(dt) Save the cause (of the position change): Displacement(dt) Save the effect or result (of the position change) P’(time)
Timeline control – undo/redo of drawing shapes Ex: see MSPaint Removes the last drawn shape Replace removed shapes, unless a new one is drawn
Undo/Redo – two options Save the cause (the data to draw each shape) Which shape? Starting mouse (x, y) Ending mouse (x, y) Save the effect (the image after drawing the new shape) Image width * image height pixels Each pixel has a red, green, and blue value
Undo/Redo – two options Save the cause (of each drawn shape) Algorithm: to undo, clear the background and redraw shapes Storage cost: 5 ints per shape Save the effect (image after each shape is drawn) Algorithm: to undo, revert to previous image Storage cost: x * y * 3 ints per shape
Tie-in with hierarchical scenes in Unity Storing the result of a series of geometric transformations in an object’s transform is comparable to “saving the effect” Image from https://www.packtpub.com/books/content/unity-3-0-enter-third-dimension
Additional slides
draw_scene() Input: 3D scene + camera Output: 2D image When to redraw? Camera change [1] Object in view moves [1] Every frame [2] Every xth frame [3] [1] if(flag_set){ draw(); flag_set = false; } [2] [3] counter = (counter + 1)%draw_rate; if(counter == 0)
Graphics pipeline