FIT CTU

Adam Vesecký

vesecky.adam@gmail.com

Course Summary

Architecture of Computer Games

Adam Vesecký

Games: What are games

What is a computer game?

Computer Application

  • a computer program that helps a user to perform a task

Computer game

  • a computer-controller game where players interact with objects displayed on a screen for the sake of entertainment.
Computer games are not applications! Yet the line between them is blurry.

Games: Game Mechanics

Terms

Emergence

  • refers to the fact that the behavior is the result of a complex and dynamic system of rules

Progression

  • refers to the structures in games where a designer outlined the possible game states beforehand, usually through level design.

Gameplay

  • an emergent property of the game as defined by its rules

Mechanics

  • a set of rules governing the behavior of a single game element.

System

  • interacting group of game elements forming a unified whole

Level

  • structure in a game that dictates what challenges players encounter

Terms

Simulation

  • a representation of a source system via a less complex system that correlates with the user's understanding of the source system

Bot

  • an intelligent artificial player emulating a human player

Agent

  • an intelligent autonomous entity, having the ability to manipulate its environment

Mob

  • a generic monster/enemy or a group of monsters

NPC

  • non-playable character - doesn't play the game but rather interacts with the player

Game Basic Elements

Mechanics

  • a set of rules governing the behavior of a single game element

Story

  • a sequence of events that unfolds in the game

Aesthetics

  • how the game looks, sounds, tastes, feels,...

Technology

  • any materials and interactions that make the game possible

Goals

  • define what players try to achieve

Game Mechanic Entities

Space

  • various spaces that can exist in a game and how they are related
  • physical and conceptual relationships

Objects

  • entities that can be seen or manipulated

Actions

  • an object's turn to act

Rules

  • statements that describe constraints, consequences and goals

Games: Rise of game engines

ID Tech

  • Family of game engines developed by ID Software
  • Id Tech 0 - the very first game engine
  • Every next game had more advanced technology
  • Still, memory constraints sabotaged attempts to create data-heavy design
Hovertank 3-D
1991
Catacomb 3-D
1991
Wolfenstein 3-D
1992
Shadowcaster
1993

Quake Engine

  • ~id Tech 1
  • Released by ID Software in 1996
  • True 3D real-time rendering
  • Binary space partitioning
  • 3D light sources, Gouraud shading
  • Games released: Quake, Hexen 2, Silver Wings
  • Source code released in 1999
  • Successors:
    • id Tech 2: 1997
    • id Tech 3: 1999
    • id Tech 4: 2004
    • id Tech 5: 2011
    • id Tech 6: 2016
    • id Tech 7: 2018

Lecture Summary

  • I'm able to define somehow what a game is
  • I know the difference between games and applications
  • I know the difference between emergence and progression
  • I know terms like gameplay, mechanics, system, level, simulation, bot, agent, mob, NPC
  • I know game basic elements
  • I know elements/entities of game mechanics
  • I know a thing or two about IDTech

Engines: Game Engines Overview

Game Engine Primary Modules

  • Game Loop - heartbeat of all games
  • Scene Manager - manages objects and structures them in a scene graph
  • Resource Manager - manages assets, controls a cache
  • Input Manager - handles inputs (keyboard, mouse, touch, joystick, gamepad,...)
  • Memory Manager - memory allocator and deallocator
  • Rigidbody Engine - event-based collision detection
  • Physics Engine - handles behavior of objects based on forces and impulses
  • Rendering Engine - renders the game, takes care of the rendering pipeline
  • Animation Engine - handles animations
  • Scripting Engine - a bridge between the engine and interpreted languages (JS, C#,...)
  • Audio Engine - plays music, clips, sounds, calculates 3D sound
  • AI Engine - abstract engine for AI (state machines, behavioral trees,...)
  • Networking Engine - handles multipeer communication
  • Other modules - GUI framework, Level Editor, Camera, Event System, LOD system,...

Engines: Game Loop

Application and Loop

Initialization process

  1. Initialize engine object
  2. Register all systems
  3. Initialize rendering window
  4. Run game loop
  5. Terminate

Game Loop process

  1. Process inputs
  2. Update game state
  3. Render game objects
  4. Repeat

Game Loop

  • simple, yet the most important part of the game engine
  • each turn advances the state of the game
  • the loop is usually coordinated with the event loop of the platform/virtual machine
  • optimal time step for rendering: 60 FPS = 16.6 ms per frame
  • audio and input processing are usually separated as they require more frequent updates
In general, a program spends 90% of its time in 10% of its code. The game loop will be firmly in those 10%.

Simple Game Loop

Game loop with separated rendering

Cooperative game loop

  • implemented by small, relatively independent jobs
  • firstly used in Ultima VII (1994)

Update method

Fixed time step

  • each update advances game time by a certain amount of time
  • precise and stable
  • the game may slow down

Variable time step

  • each update advances game time based on how much real time passed since the last frame
  • natural
  • non-deterministic and unstable

Adaptive time step

  • switches between variable and fixed time step
  • based either on thresholds or a more sophisticated approach
  • deals with long breaks better than the other two
Some old games were tied with the system clock, hence their CPU-sensitivity.

Update Inconsistencies

  • game objects are consistent before and after every update
  • they may get to an inconsistent state during update - one-frame-off lag
  • possible solutions: bucket update, script execution order (Unity)

Object A reads previous state of Object B and Object B reads previous state of Object C

Engines: Scene Graph

Scene Graph

Scene Graph

  • essential structure of every interactive application
  • a way of ordering the data into a hierarchy
  • represented as N-Tree or a regular graph
  • implemented as an array, oct-tree, quad-tree, bounding volume hierarchy,...
  • parent nodes affect child nodes (translation, rotation, scale,...)
  • leaves represent atomic units (shapes, vertices, imported meshes)

Scene Manager

  • manages objects in the scene
  • similar to HTML DOM along with Event Manager
  • responsibility: sending events, searching for objects, applying transformations,...
  • Unity Engine - game objects form a hierarchy
  • Unreal Engine - components form a hierarchy
  • Godot Engine - everyting forms a hierarchy

Engines: Input

Input Manager

Detects input events from devices

Atomic events

  • KEY_DOWN
  • KEY_UP
  • MOUSE_WHEEL
  • JOYPAD_A

Compound events

  • FLING
  • PINCH_TO_ZOOM
  • DOUBLE_TAP

Special events

  • cheat codes
  • fighting combos

Input Devices

Receiving the state of the device

  • polling - compare against previous state
  • callbacks - handled by upper SW layer

Devices

  • keyboard, touch sensor, camera, Oculus Touch,...
  • one-axis controller - single analog state
  • two-axis controller - mouse and joystick
  • three-axis controller - accelerometer

Dead zone

  • area of a control interface that has no input effect (analog joystick)

Normalization

  • analog axis are mapped to a Cartesian space, not a circular one
  • input must be normalized

Normalized input

Engines: Memory

Memory Manager

  • default managers are not suitable for games (i.e. malloc function)
  • game engines usually have their own allocators

Custom allocators

  • stack allocator
  • pool allocator
  • heap allocator
  • bucket allocator

Pool allocator

  • allocates lots of small blocks of memory, each of the same size
  • doesn't suffer from memory fragmentation
  • entities have to be of the same size

Other allocators

Bucket allocator

  • several pool allocators for objects of various sizes
  • great solution for demanding games
  • difficult to manage

Heap allocator

  • more or less the same as heaps in virtual machines
  • very flexible
  • requires garbage collector

Data loading

Level loading

  • used in Tomb Raider, Doom, Quake,...
  • requires a loading screen
  • only one game chunk is loaded at a time
  • appropriate for games with levels, separated scenes or star topology

Air locks

  • used in Half-Life, Inside, Portal (partially)
  • air lock is a small scene (room, hall)
  • when the player enters the area from which can't see the previous one, next scene is loaded

World streams

  • used in GTA, WoW, Arma, Spiderman
  • the world is divided into regions
  • the engine unloads chunks too far away and loads new chunks the player is heading to
  • uses LOD system - chunks are loaded at variable granularity

Engines: Scripting Engine

Scripting architectures

Scripted callbacks

  • certain functions are customizable via scripts
  • a game object can respond to some relevant occurrence within the game world (in-game scripts)

Scripted components

  • new components/objects/properties can be constructed entirely in a script
  • firstly used in Dungeon Siege

Script-driven game

  • script is running the game and the core systems are written in the engine
  • used in Unreal, Unity, Godot, GameMaker,...

Script-driven engine

  • script drives the entire engine (PixiJS, ThreeJS, p5.js, BabylonJS)

Game Engine Scripting API

  • the engine needs to communicate with the script - provided by bridging
    • JNI (Java and C++)
    • P/Invoke (.NET and C++)
    • Dukbind (Duktape JS and C++)
  • bridge is a performance bottleneck, especially for per-frame calls
  • more scripting languages = more bridges to maintain
  • Marshalling
    • transforming memory representation of an object between two domains (different programming languages)
  • Semantic gap
    • descriptive difference of an object in various representations (e.g. relational database vs object-oriented structure)

Lecture Summary

  • I know primary modules of game engines
  • I know how game loop works
  • I know the options of game update and inconsistencies that may occur
  • I know what scene graph is and what elements it contains
  • I know categories of input events and input device
  • I know what dead-zone of input is
  • I know the purpose of a memory manager
  • I know the purpose of pool allocator
  • I know the options of data loading in games
  • I know the types of scripting architectures
  • I know what marshalling is

Assets: Introduction to Assets

What are assets?

Everything that is specific to a game (and not to an engine).

Any piece of data which is in a format that can be used by the game engine.

All visual elements that are presented to the user and protected by copyright.

Assets: Assets management

Resource Manager

Provides access to all resources (~assets)
  • meshes, materials, shaders, animations, textures, clips, levels
  • many assets are not used in their original format
  • Resource Cache - used for faster access
Manages lifetime of each resource
  • most managers maintain some kind of registry
Ensures that only one copy of each resource exists in memory
  • resource GUID - usually path to the file, guaranteed to be unique
Loads required resources and unloads those no longer needed
  • loading is simpler than unloading
Handles streaming
  • in case of open worlds

Assets: Multimedia assets

Image Formats

JPEG

  • Joint Photographic Experts Group
  • used for storing digital photography
  • doesn't support alpha channel

PNG

  • Portable Network Graphics
  • good results for simple images (not real pictures)

TGA

  • Truevision TGA
  • favorite format for textures

SVG

  • Scalable Vector Graphics
  • format for vector images

Videos and Cutscenes

  • Cutscene
    • one of the most progressive properties of a game
    • a) fully progressive - video, in-game cutscene
    • b) partially progressive - i.e. custom skin of the avatar in the scene
    • c) partially emergent - the player can walk around but can't control the scene
    • d) fully emergent - generated cutscene
  • 1980's - in-game cutscenes
  • 1990's - pre-rendered video cutscenes
  • 2000's - in-game cutscenes
  • 2010's - pre-rendered video cutscenes ¯\_(ツ)_/¯

Textures

  • pieces of bitmap that are applied to an object
  • textures are ultimately converted to formats that are specific to platform and graphic API

Types

  • diffuse texture
  • displacement texture
  • normal texture
  • volumetric texture
  • tileable texture
  • sprite atlas

Formats

  • Desktop: BC6H, BC7, DXT1, DXT5
  • iOS: ATSC, PVRTC
  • Android: ASTC

Diffuse

Displacement

Normal

Volumetric

Tileable

Sprite atlas

Sprites

Sprite

  • a single graphic image that is incorporated into a scene
  • allows to create large scenes as you can manipulate each sprite separately

Spritesheet

  • a set of corresponding sprites

Sprite atlas

  • a single texture image containing a list of spritesheets

Lecture Summary

  • I know what game assets are
  • I know the purpose of a Resource manager
  • I know the difference between the use-cases of WAV and MP3/OGG formats
  • I know the difference between PNG and JPEG formats
  • I know what a cutscene is
  • I know what a sprite is
  • I know the types of textures
  • I know what a tilemap is

Components: Game Model

What is a Game Model

Game model is a model of a domain in which the simulated world takes place.

Components: Component-oriented architecture

Component

  • a unit of composition with specified interfaces and explicit context dependencies
  • components are plug-and-play objects
  • prefers composition over inheritance
  • behavior of an entity is determined by the aggregation of its components
  • Dungeon Siege (2002) - one of the first games featuring component-based systems

ECS Pattern

  • Entity-Component-System
  • common pattern for component-oriented libraries and engines
  • game object is just a container for data and logic
  • components are attached to their game objects
  • can be easily integrated into scene graph

Terms

Entity

  • a single entity/game object, usually incorporated into a scene graph

Attribute

  • data unit attached to an entity

Property

  • data unit attached to a component

Component

  • instantiable unit that defines simple functional behavior

System

  • a superior component responsible for a bigger scope (HealthSystem, GameManager, AudioSystem)
The naming varies. Some engines use behaviours for components, components for systems, property for attributes etc.

Components: Messaging

Messaging possibilities

By modifying the container object's state

  • e.g. shared state machine
  • indirect communication
  • difficult to debug

By direct calls

  • OOP way
  • fast, but increases coupling
  • we need to know what to call
  • e.g. calling a global component that is always present

By using a message broker

  • events and commands
  • each component can declare interest in relevant messages
  • slower than the direct call
  • e.g. listening to GAME_OVER event and stopping the game

Message Broker

  • components should be notified of any state change that is relevant
  • can be used for returning values (danger of feedback deadlock)
  • a handler can be implemented inside components - OnMessage() function
  • processing can be instant or delayed/scheduled
  • Event - a message informing that something happened
  • Command - a message instructing that something should happen

Message Types

Unicast

  • a component sends a message to another component
  • in most cases, this can be handled by a direct call
  • example: pause the game

Multicast

  • a) component sends a message to subscribers
  • b) component sends a message to all objects that meet specific criteria
  • example: notify all nearby units that the player has entered the area
  • example: collision trigger - notify all subscribers

Broadcast

  • rarely used, usually for global messages
  • example: level completed, game over

Lecture Summary

  • I know what a game model is and what is it made of
  • I know the pitfalls of object-oriented architecture in games
  • I know what a component is
  • I know the ECS pattern
  • I know messaging practices for component architecture
  • I know the purpose of a message broker in games

Patterns: Action Patterns

Chain

  • Process - something that requires more than one frame to finish
    • basically anything that involves animations, mini cut-scenes, delayed actions, sounds
  • Chain - a set of commands, events and processes that need to be evaluated in a given order
  • implementation

    • callbacks - basically in every language, very bad robustness
    • listener chaining - any language with closures (Java, JavaScript, C#,..)
    • iterator blocks - C#
    • promises and generators - JavaScript
    • coroutines - Kotlin, Ruby, Lua,...

Responsibility ownership

  • determines which component should be responsible for given scope/action/decision
  • there is no bulletproof recipe, yet it should be unified
  • if the scope affects only one entity, it should be a component attached to that entity
    • example: a worker that goes to the forest for some wood
  • if the scope affects more entities, it's often a component/system attached either to an abstract entity up the scene graph (e.g. the root object)
    • example: battle formation controller, duel controller (who wins, who loses)

Individual units

Battle formation

Patterns: Optimizing Patterns

Flyweight

  • an object keeps shared data to support large number of fine-grained objects
  • e.g. instanced rendering, geometry hashing, particle systems
  • here we move a position and a tile index (Sprite) into an array

Patterns: Structural Patterns

Selector

  • a function that returns a value
  • centralizes the knowledge of how to find an entity/attribute/component
  • can be used by components to access dynamic data
  • can form a hierarchy from other selectors
1 const getPlayer(scene: Scene) => scene.findObjectByName('player');
2
3 const getAllUnits(scene: Scene) => scene.findObjectsByTag('unit_basic');
4
5 const getAllUnitsWithinRadius(scene: Scene, pos: Vector, radius: number) => {
6 return getAllUnits(scene).filter(unit => unit.pos.distance(pos) <= radius);
7 }
8
9 const getAllExits(scene: Scene) => {
10 const doors = scene.findObjectsByTag('door');
11 return doors.filter(door => !door.locked);
12 }

Patterns: State Patterns

Flag

  • bit array that stores binary properties of game objects
  • may be used for queries (e.g. find all MOVABLE objects)
  • similar to a state machine but the use-case is different
  • if we maintain all flags within one single structure, we can search very fast

Numeric state

  • the most basic state of an entity
  • allows us to set conditions upon which a transition to other states is possible
1 // stateless, the creature will jump each frame
2 updateCreature() {
3 if(eventSystem.isPressed(KeyCode.UP)) {
4 this.creature.jump();
5 }
6 }
7
8 // introduction of a state
9 updateCreature() {
10 if(eventSystem.isPressed(KeyCode.UP) && this.creature.state !== STATE_JUMPING) {
11 this.creature.changeState(STATE_JUMPING);
12 this.creature.jump();
13 }
14 }

Patterns: Creational Patterns

Builder

  • a template that keeps attributes from which it can build new objects
  • each method returns back the builder itself, so it can be chained
1 class Builder {
2 private _position: Vector;
3 private _scale: Vector;
4
5 position(pos: Vector) {
6 this.position = pos;
7 return this;
8 }
9
10 scale(scale: Vector) {
11 this.scale = scale;
12 return this;
13 }
14
15 build() {
16 return new GameObject(this._position, this._scale);
17 }
18 }
19
20 new Builder().position(new Vector(12, 54)).scale(new Vector(2, 1)).build();

Prototype

  • Builder builds new objects from scratch, Prototype creates new objects by copying their attributes
  • in some implementations, the prototype is linked to its objects - if we change the prototype, it will affect all derived entities
  • e.g. templates in Godot, linked prefabs in Unity and Unreal engine

Prefabs in Unity

Factory

  • Builder assembles an object, factory manages the assembling
  • Factory creates an object according to the parameters but with respect to the context
1 class UnitFactory {
2
3 private pikemanBuilder: Builder; // preconfigured to build pikemans
4 private musketeerBuilder: Builder; // preconfigured to build musketeers
5 private archerBuilder: Builder; // preconfigured to build archers
6
7 public spawnPikeman(position: Vector, faction: FactionType): GameObject {
8 return this.pikeman.position(position).faction(faction).build();
9 }
10
11 public spawnMusketeer(position: Vector, faction: FactionType): GameObject {
12 return this.musketeerBuilder.position(position).faction(faction).build();
13 }
14
15 public spawnArcher(position: Vector, faction: FactionType): GameObject {
16 return this.archerBuilder.position(position).faction(faction).build();
17 }
18 }

Lecture Summary

  • I know how chain works
  • I know something about responsibility ownership in component architecture
  • I know flyweight pattern
  • I know selector pattern
  • I know flag pattern
  • I know numeric state pattern
  • I know builder pattern
  • I know prototype pattern
  • I know factory pattern

Audio: Digital Sound

PCM

  • pulse-code modulation, a method used to digitally represent sampled analog signals
  • sample - fundamental unit, representing the amplitude of an audio signal in time
  • bit depth - each bit of resolution is equal to 6dB of dynamic range
  • sample rate - number of samples per second: 8 kHz, 22.05 kHz, 44.1 kHz, 48 kHz
  • frequency - a measure of the number of pulses in a given space of time
  • frame - collection of samples, one for each output (e.g. 2 for stereo)
  • buffer - collection of frames in memory, typically 64-4096 samples per buffer
    • the greater the buffer, the greater the latency, but less strain being placed on the cpu

Audio: Sound synthesizing

Sound generators

  • generator - oscillator that can make a tone, either independently or by pairing with another generator
    • synth music is a combination of generated tones, effects, filters, and recorded sound samples
  • main synthesis types: additive, subtractive, FM, wavetable

Audio: Music in games

Music in games

Assets

  • sound cues - collection of audio clips with metadata
  • sound banks - package of sound clips and cues (e.g. all voices of one person)

Asset categories

  • diegetic - visible (character voices, sounds of objects, footsteps)
  • non-diegetic sound - invisible (sountrack, narrator)
  • background music - ambient music (e.g. river)
  • score - soundtrack, is clearly recognizable
  • interface music - button press
  • custom music (e.g. GTA radio)
  • alert - music triggered by an event

Dynamics

  • linear audio - only reflects major changes (e.g. finished level)
  • dynamic audio - changes in response to changes in the environment or gameplay
  • adaptive audio - responses to the gameplay, adapts itself based on game attributes

Dynamic music

Features

  • looping - going around and around
  • branching - conditional music
  • layering - some pieces can be muted on and off and re-combined
  • transitions - moving smoothly from one cue to another

Audio: Sounds in games

3D Sound

Attenuation

  • the further the sound, the quieter it is

Occlusion

  • how sound is occluded by solid objects, losing its high frequency

Obstruction

  • when the direct path to a sound is muffled but not enclosed
  • creates delays

Lecture Summary

  • I know what PCM is and how buffer size affects the gameplay
  • I know 4 basic types of synthesis
  • I know basic waveforms of sound generators
  • I know categories of audio assets in games
  • I know features of dynamic music (looping, branching, layering, transitions)
  • I know what attenuation, occlusion and obstruction in 3D sound are

Audio: Randomness

Random Functions Distribution

Uniform distribution

  • most common distribution of random generators
  • applications: noise, shuffling, dice

Gaussian (normal) distribution

  • more common in games - every characteristic has some kind of average, with individuals varying with a normal distribution
  • can be calculated from a uniform generator via transformation (Box-muller algorithm)
  • applications: height of trees, aiming for projectiles, average speed, physical reaction time, reload rate, refresh healing rate, critical hit

Uniform distribution

Gaussian distribution

Terms

Seed

  • a hash that initializes random generators
  • a good source of entropy is user input or time

Loot

  • items obtained over the gameplay (money, spells, equipment, weapons,...)

Spinning

  • calling the random function on a time-frame basis without using the result
  • advances the game to a difficult-to-predict place

Rarity Slotting

  • a method of standardization to determine rates (common, rare, epic, legendary)
  • can be defined as a rarity table, calculated via weighted sum

Random encounter

  • popular mechanics of RPG games (Final Fantasy, Pokémon, Golden Sun)
  • the game suddenly shifts to battle mode, forcing the player to fight
  • after winning the battle, the player receives a reward (skill upgrade, items, money)

Space: Procedural generation

Noise

  • Randomness is used to vary characteristics, noise is used to vary them over time or in space
  • Noise functions
    • Lattice-based
      • Perlin noise, Simplex noise, Wavelet noise, Value noise
    • Point-based
      • Worley noise (Voronoi/Cellular)

Perlin Noise

Simplex Noise

Worley Noise

Space: Geometry

Points and Vectors

  • Vector - a quantity that has both a magnitude and a direction
  • vector can be used to represent a point, provided that we fix the tail of the vector to the origin of the coordinate system

Addition and subtraction

  • vector + vector = vector
  • vector - vector = vector
  • point + vector = point
  • point - point = vector
  • point + point = undefined

Vector addition and subtraction

Points and Vectors

Magnitude

  • scalar representing the length of the vector

Magnitude of a vector

Normalization

  • a unit vector is a vector with a magnitude of one:

Normal vector

  • vector is normal to a surface if it is perpendicular to it

Dot product

Dot Product

Cross product

  • yields another vector that is perpendicular to two vectors

Rotation in 3D space

Rotational representations

Euler angles

  • Pitch, Yaw, Roll
  • simple, small size (3 floats), intuitive
  • the order in which the rotations are performed matters
  • gimbal lock issue - when a 90-degree rotation causes one axis to collapse onto another

Axis + angle

  • axis of rotation plus a scalar for the angle of rotation
  • intuitive and compact
  • rotations cannot be easily interpolated
  • rotations cannot be applied to vectors directly

Rotational representations

Quaternions

  • similar to axis + angle, but with an algebraic twist
  • alternative form:
  • unit-length:
  • a unit quaternion can be visualised as a 3D vector + scalar
  • permits rotations to be concatenated and applied directly
  • permits rotations to be easily interpolated
  • can perform only one full rotation between keyframes
Use Euler angles for fast rotation around one axis and quaternions for complex rotations around all axes

Space: Geometric hashing

Spatial partitioning

Bounding volume

  • groups objects or their parts together based on their positions and sizes
  • if the object moves, so will the hierarchy
  • used for physics, shape analysis, precise collision detection

Spatial data structure

  • a structure that stores objects by their position
  • is locked to the world
  • used for range queries, neighborhood searching, rough collision detection
  • the more objects we have, the more benefits we get

Implementations

  • BSP - binary-space partitioning
  • Quad-tree - for 2D and semi-3D space
  • Oct-tree - for 3D space
  • Grid - a square grid

Oct-tree

Binary Space Partitioning

  • algorithm that decomposes a polygon-soup into a tree that contains convex sets
  • first used in Doom to solve difficult rendering of circles around pillars in level 2
  • very good for rendering, ray-casting and collision detection in complex indoor environments
  • works only in static environments and requires a complex preprocessing stage

Quad-tree

  • hierarchical partition
  • each inner node has 4 children
  • overlapping solid objects are put into all children they touch
  • only objects in the same leaf can be in collision
  • useful for outdoor scenes
  • good for object sparsely spread that do not move too fast

Quad-tree and geometric hashing

Grid

  • implemented as an 1D/2D/3D array or a hash-table
  • each cell has a list of units that are inside
  • if a unit crosses the boundary of the cell, we need to move it to the other list
  • good for a large amount of fast objects that are uniformly distributed
  • very fast to locate an object - in sharp contrast with recursing down a quad-tree
  • takes up more memory, granularity is static

Space: Navigation

Navigation graph

Grid-based

  • created by superimposing a grid over a game environment
  • traversability flag indicates whether the cell is traversable or not
  • connection geometries: tile, octile, hex
  • reflecting renvironmental changes = recalculation of the traversability flag

Pathfinding algorithms

Uniformed graph searches

  • searches a graph without regard to any associated edge cost
  • DFS (depth-first search)
    • searches by moving as deep into the graph as possible
    • doesn't guarantee to find the best path
  • BFS (breadth-first-search)
    • fans out from the source node, always finds the best path

Cost-based graph searches

  • Dijkstra's Algorithm
    • explores every node in the graph and finds the shortest path from the start node to every other node in the graph
    • uses CSF (cost-so-far) metric
    • explores many unnecessary nodes
  • A* (Dijkstra with a Twist)
    • extension of Dijkstra, invented in 1968
    • main difference: augmentation of the CSF value with a heuristic value

A*

  • improved Dijkstra by an estimate of the cost to the target from each node
  • Cost , where  is the cost-so-far and  is the heuristic estimate
  • Heuristics: Euclidean, Manhattan, adaptive, dynamic,...
    • Manhattan distance will work if almost no obstacles appear

Improvements

  • preprocess the map, calculate universal paths
  • mark tiles which cannot lead anywhere as dead-ends
  • limit the search space

Pathfinding algorithms: Comparison

  • breadth-first search ignores the cost
  • Dijkstra ignores the topology of the graph
  • A* considers both

Lecture Summary

  • I know what purpose can serve uniform and gaussian distributions
  • I know what is a loot, spinning, rarity slotting and random encounter in games
  • I know something about noise functions
  • I know basic vector operations: addition, subtraction, magnitude, normalization, and dot product
  • I know what quaternions and Euler angles are, and what use-cases they are good for
  • I know basic structures for spatial partitioning: grid, quad-tree, oct-tree and BSP
  • I know basic types of navigation graphs
  • I know something about pathfinding algorithms, such as BFS, Dijkstra and A*

Physics: Dynamics

Motion

Projectile motion

Slope motion (no friction)

Euler Integration

Explicit method

Improved method

Implicit method

  • cheap and easy to implement
  • high error and poor stability, depending
    directly on the time step

Physics: Steering Behaviors

Steering Behaviors

  • set of algorithms and principles that help autonomous agents move in a realistic manner by using simple forces
  • designed by Crag Reynolds in the early 90's
  • Agent - a system situated within an envirnoment, with an ability to sense that environment
  • Motion layers
    • action selection - choosing goals, strategy
    • steering - trajectory calculation
    • locomotion - way of moving, animation, articulation

Steering Behaviors

Seek

  • the simplest steering behavior
  • a force that directs an agent toward a target position

Steering Behaviors

Flee

  • opposite of seek
  • creates a force that steers the agent away

Arrive

  • seek + stopping movement
  • decelerates the agent onto the target position, based on given slowing radius

Steering Behaviors

Pursuit

  • agent intercepts a moving target
  • predicts where the target is going to be in the future
  • calls for a good prediction function

Evade

  • opposite of pursuit
  • the evader flees from the estimated future position

Steering Behaviors

Wander

  • produces a force that will give an impression of a random walking
  • small random displacement is applied to the velocity vector every frame
  • a circle is projected in front of the vehicle
  • the vehicle is steered toward a target that moves along the perimeter
  • parameters: circle radius, distance from the vehicle and jittering (randomness)

Steering Behaviors

Path follow

  • moves a vehicle along a set of waypoints
  • the last waypoint can be reached using arrive, the others via seek
  • smooth movement can be achieved using a tolerance radius or Bézier curve approximation
  • very sensitive to configuration (max force, max velocity, radius,...)

Physics: Physics Engine

Physics engine

  • system that approximates physical phenomena in real-time
  • computes dynamics of objects in virtual scene
  • you have to understand your game before you decide how to add a physical simulation to it
  • can improve immersion
  • can support new gameplay events
  • can broke the game story
  • can take up significant computing resources

Object types

Body

  • fundamental object in the physics scene

Rigid Body

  • idealized, infinitely hard, non-deformable solid object
  • physics-driven bodies - driven entirely by the simulation
  • game-driven bodies - moved in a non-physical way (animations)
  • fixed bodies - collision-only bodies (e.g. triggers)

Soft body

  • deformable

Shape

  • region of space described by a boundary, with a definite inside and outside (curved line, polygon, curved surface, polyhedron)

Fixture

  • used to describe size, shape and material properties

Object Types

Constraint

  • connects bodies together in order to simulate interaction (ropes, wheels, vehicles, chains)

Sensor/Phantom

  • entity that provides a feedback when certain objects overlap
  • participates on collision detection but doesn't affect the scene

Rag doll

  • displays human-like figures with a realistic motion

Destructible object

  • breakable object, can be implemented by using rigid body dynamics, dividing the model into a number of breakable pieces

Constraints

rope

revolute

prismatic

cone-twist

Particle Systems

  • a collection of point masses that obeys certain physical laws
  • can model complex fuzzy shapes and dynamics
  • uses Flyweight pattern (array of positions, velocities, group lists)
  • particles are not only moving points! Even a tree may become a particle

Applications

  • fluids
  • visual effects
  • flocks
  • rendered trails (plants)
  • soft bodies (flag, cloth)

Basic model

  1. generate new particles
  2. assign individual attributes
  3. extinguish dead particles
  4. move and transform particles according to their dynamic values
  5. render meshes

Physics: Collision Detection

Primitives

Sphere

  • center point and radius (4 numbers for 3D)

Capsule

  • 2D: rectangle and two circles
  • 3D: cylinder and two hemispherical end-caps
  • representation: two points and radius

AABB

  • axis-aligned bounding box
  • rectangular volume (cuboid) whose faces are parallel to the axes of the coordinate system
  • very efficient test for penetration
  • AABB must be recalculated whenever the object rotates

Primitives

OBB

  • oriented bounding box
  • defined by a position, half-extents and orientation
  • commonly used

k-DOP

  • discrete oriented polytope
  • more general case of AABB and OBB
  • approximates the shape of an object

Convex volume

  • more general shape
  • must be convex
  • expensive for intersection test

SAT

SAT (separating axis theorem)

  • based on collection of intersection tests
  • if an axis can be found along which the projection of two convex shapes do not overlap, then the two shapes do not intersect
  • for 2D: AABB 2 axes, OBB 4 axes
  • for 3D: AABB 3 axes, OBB 15 axes

Other methods

  • GJK, Line Sweep, Sphere test

Tunneling problem

Stepped world

  • time steps vary based on occurring situation
  • collision time is calculated by doing binary search in time, moving object back and forth by 1/2 steps (5 iterations is usually enough)

Continuous Collision Detection (CCD)

  • uses Swept Shapes technique
  • a new shape is formed by the motion of the original one
  • rotating shapes may result in shapes that aren't convex

Lecture Summary

  • I know basic motion equations
  • I know basic Euler integration methods
  • I know basic types of steering behaviors
  • I know object types for physics engines
  • I know basic constraints, such as rope, revolute, prismatic, and cone-twist
  • I know basic collision primitives
  • I know SAT theorem
  • I know what CCD is
  • I know what particle systems are

Graphics: Space

Space

Model Space

  • origin is usually placed at a central location (center of mass)
  • axes are aligned to natural direction of the model

World Space

  • fixed coordinate space, in which the transformations of all objects in the game world are expressed

View/Camera Space

  • coordinate frame fixed to the camera
  • space origin is placed at the focal point of the camera
  • OpenGL: camera faces toward negative z

Clip Space

  • a rectangular prism extending from -1 to 1 (OpenGL)

View/Screen Space

  • a region of the screen used to display a portion of the final image

World-Model-View

Clip Space

View Volume

  • View volume - region of space the camera can see
  • Frustum - the shape of view volume for perspective projection
  • Rectangular prism - the shape of view volume for orthographic projection
  • Field of View (FOV) - the angle between the top and bottom of a 2D surface of the projected world

Perspective projection

Orthographic projection

Graphics: Animations

Interpolation

  • method that calculates semi-points within the range of given points

Applications

  • graphics - image resizing
  • animations - transformation morphing
  • multiplayer - game state morphing
  • audio/video - sample/keyframe interpolation

Main methods

  • Constant interpolation (none/hold)
  • Linear interpolation
  • Cosine interpolation
  • Cubic interpolation
  • Bézier interpolation
  • Hermite interpolation
  • SLERP (spherical linear interpolation)
SLERP is widely used in animation blending

Graphics: GPU processing

Terms

Vertex

  • primarily a point in 3D space with x, y, z coordinates
  • attributes: position vector, normal, color, uv coordinates, skinning weights,...

Fragment

  • a sample-sized segment of a rasterized primitive
  • its size depends on sampling method

Texture

  • a piece of bitmap that is applied to a model

Occlusion

  • rendering two triangles that overlap each others
  • Z-fighting issue

    Z-Fighting

  • solution: more precise depth buffer

Culling

  • process of removing triangles that aren't facing the camera
  • frustum culling, portals, anti-portals,...

Shaders

  • programs that run on the video card in order to perform a variety of specialized functions (lighting, effects, post-processing, physics, AI)

Vertex shader

  • input is vertex, output is transformed vertex

Geometry shader (optional)

  • input is n-vertex primitive, output is zero or more primitives

Tessellation shader (optional)

  • input is primitive, output is subdivided primitive

Pixel (fragment) shader

  • input is fragment, output is color, depth value, stencil value
  • widely used for visual effects

Compute shader

  • shader that runs outside of the rendering pipeline (e.g. CUDA)

Graphics: Rendering concepts

Other techniques

LOD

  • level of detail, boosts draw distance
  • pioneered with Spyro the Dragon Series

Texture mapping

  • mapping of 2D surface (texture map) onto a 3D object
  • early games have issues with perspective correctness
  • common use - UV mapping

Baking

  • a.k.a rendering to texture
  • generating texture maps that describe different properties of the surface of a 3D model
  • effects are pre-calculated in order to save computational time and circumvent hardware limits

Lecture Summary

  • I know the difference between model space, world space, view space, clip space and screen space
  • I know what view volume and field of view is
  • I know what interpolation is
  • I know terms like Vertex, Fragment, Texture, Occlusion, and Culling
  • I know what purpose serve vertex and fragment shader
  • I know what LOD, texture mapping, texture filtering, and baking is

Game AI: AI introduction

Challenges for Game AI

Game AI features and limits

  • real-time
  • limited resources
  • incomplete knowledge
  • planning
  • learning

Game AI properties

  • predictability and unpredictability (surprise elements)
  • support - communication between NPC and the player
  • surprise - harassment, ambush, team support,...
  • winning well and losing well
  • cheating - acceptable as long as it doesn't get detected by the player
The AI must be fun to play against, not beat the player easily

Game AI: AI for mobs

Scripting

  • IF-THIS-THEN-THAT
  • AI behavior is completely hardcoded
  • simple, easy to debug, easy to extend
  • human player should behave as the developers expected
  • good scripting behavior must cover a large amount of situations
1 // Doom 2: find player to chase
2 void A_Look (mobj_t* actor) {
3 mobj_t* targ;
4 actor->threshold = 0; // any shot will wake up
5 targ = actor->subsector->sector->soundtarget;
6 if (actor->flags & MF_AMBUSH){
7 if (P_CheckSight (actor, actor->target))
8 goto seeyou;
9 } else goto seeyou;
10
11 if (!P_LookForPlayers (actor, false)) return;
12 // go into chase state
13 seeyou:
14 P_ChasePlayer();
15 }

Finite State Machine

  • the oldest and most commonly used formalism to model game AIs
  • useful for entities with a small set of distinct states
  • each entity can be in exactly one of a finite number of states at any time
  • Definition
    • quadruple:
    •  is a finite, non-empty set of states
    •  is a finite set of inputs
    •  is the state-transition function
    •  is an initial state,
  • can be implemented via polymorphism or a state transition table
  • unmanageable for large complex systems, leading to transition explosion

Hierarchical state machine

  • also known as statecharts
  • each state can have a superstate or a set of substates
  • groups of states share transitions
  • usually implemented as a stack
    • push a low-level state on the stack when entered
    • pop and move to the next state when finished

Behavior Tree

  • tree of hierarchical nodes that control decision making process
  • originated from gaming industry since Halo 2 (2004)
  • combines elements from both Scripting and HFSMs
  • there is no standardized formalization
  • depth-first traversal, starting with the root node
  • each executed behavior passes back and returns a status
    • SUCCESS, FAILURE, RUNNING, (SUSPENDED)

Behavior Tree

Node TypeSuccessFailureRunning
SelectorIf one child succeedsIf all children failIf one child is running
SequenceIf all children succeedIf one child failsIf one child is running
DecoratorIt dependsIt dependsIt depends
ParallelIf N children succeedIf M-N children succeedIf all children are running
ActionWhen completedUpon an errorDuring completion
ConditionIf trueIf falseNever

Game AI: AI in strategies

Real-time strategy

  • Real-time strategy is a Bayesian, zero-sum game (Rubinstein, 1994)
  • a game where the player is in control of certain, usually military, assets, with which the player can manipulate in order to achieve victory
  • goal: build up a base, gather resources, produce army, destroy the enemy
  • methods: layer-based AI, rule-based AI

Main elements

  • map, mini-map
  • resources
  • units and their attributes
  • buildings

Other features

  • real-time aspect (no turns)
  • fog of war
  • tech tree

RTS Features

Resource Control

  • minerals, gas, oil, trees,...
  • controlling more resources increases the players' constsruction capabilities

Tech tree

  • a directed acyclic graph that contains the whole technological development of a faction

Build order (opening)

  • the timing at which the first buildings are constructed .scope.mt-5.fragment

Fog of war

  • fog that covers the parts of the map the player has not yet explored
  • requires to scout unexplored areas to find enemy sources

Micromanagement

  • way of controlling units in detail while they are in combat

Tactics

  • a set of specific actions used when applying a strategy

Strategy

  • making decisions knowing what we saw from the opponent

Lecture Summary

  • I know what challenges in terms of game AI the developers face
  • I know basic AI techniques, such as scripting, FSM, and HFSM
  • I know something about behavior trees
  • I know the definition of supervised, unsupervised, and reinforcement learning
  • I know the main element and features of RTS

Multiplayer: Networking Architecture

Peer-to-peer architecture

  • each device exchanges data with each other in a fully connected graph
  • used in Doom, early Command & Conquer series, Age of Empires, Starcraft
  • given  peers, each must have  connections ->  in total
  • methods: single master, partial authority, full replication

Client-Server architecture

  •  devices,  connections
  • server must handle  more messages per second
  • server quickly becomes the bottleneck (lack of power and bandwidth)
  • Dedicated server - only runs the game state and communicates
  • Listen server - server is an active participant in the game itself

Multiplayer: Transport

Message Types

Stream

  • doesn't need to be confirmed, contains a collection of continuous values
  • e.g. dynamic objects and their attributes (transformation)

Snapshot

  • complete information of the game state, sent either on demand or at given intervals

Command

  • messages that have an impact on the game state, have to be confirmed
  • e.g.: UNIT_CREATED, UNIT_DESTROYED, BUILDING_COMPLETED

Action

  • high-priority messages (player's inputs, fire button,...)

Procedure Call

  • a generic message that allows to call any function (play sound, load assets, reset animation)

Connection messages

  • messages for handshake, ID assignment, disconnect, etc.

Beacon

  • regular messages to inform the server that the connection is still on

Replication

  • the act of transmitting a state of an object from one device to another
  • each object must be uniquely identified (network ID)
  • the network message contains a type of an object and all parameters required to construct it

 switch(actionType) {

    case OBJECT_CREATED:

      int objectType = reader->ReadDWord();

      auto factory = creatorManager->FindObjectFactory(objectType);

      auto newInstance = factory->CreateInstance(reader); // parse parameters

      objects.push_back(newInstance);

    break;

    case OBJECT_DELETED:

      int objectId = reader->ReadDWord();

      sceneManager->removeObjectById(objectId);

    ...

  }

Reliability

  • packets may get lost
  • server keeps sending messages that have an impact on the game state until the client accepts them

Ordering

  • packets may arrive in a different order
  • the client shouldn't apply a command message to its state before it applies all previous messages

Multiplayer: Synchronization

Server-side rewind

  • dealing with instant actions that affect the gameplay (e.g. instant hit in FPS)
  • occurs due to the inaccuracies of dead reckoning and time dilation
  • server may change a state that has already been confirmed

Source engine's solution

  • rewinds state on the server to exactly the state in which the player fired
  • server stores the poses of every relevant object for X last frames and looks up the two frames between which the client was interpolating

Wrong

Correct

Latency handling summary

Time dilation

  • delays the values by a few frames and interpolates to them

Deterministic prediction

  • runs simulated code, masks latency and keeps the client's state in sync

Dead reckoning

  • non-deterministic prediction
  • client uses the last known state of an object to extrapolate future state

Server-side rewind

  • the server buffers object positions for several frames to match the client's view when processing instant events
It is better to be wrong on time than right but late

Lecture Summary

  • I know the difference between P2P and client-server architecture
  • I know what issues networking architecture may face
  • I know what types of messages can be used in multiplayer games
  • I know what replication is
  • I know how reliability in multiplayer works
  • I know how interpolation works
  • I know what server-side rewind is
  • I know how games can deal with latency