FIT CTU

Adam Vesecký

NI-APH
Summary

Course Summary

What are games

What is a computer game?

Computer Application

  • a computer program that helps the user to perform tasks

Computer game

  • a computer program where players interact with objects on a screen for the sake of entertainment

Gamification

  • application of game elements to other areas to encourage engagement with a product or sevice

Habitica

Gather.town

Computer games are not applications! Yet the line between them is blur.

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

  • a structure in a game that dictates what challenges players encounter

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

  • processes that change the game state
    as a reaction to some sort of input

Rules

  • statements that describe constraints and consequences

History 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
  • all games were based on raycasting until 1996
Hovertank 3-D
1991
Catacomb 3-D
1991
Wolfenstein 3-D
1992
Doom
1993

Quake Engine

  • ~id Tech 1
  • released by ID Software in 1996
  • initiated the rise of 3D gaming
  • true 3D real-time rendering, 3D light sources
  • Id Tech 1 Games:
    • Quake, Hexen 2, Silver Wings
  • Other IdTech-based games:
    • Call of Duty
    • Medal of Honor
    • Daikatana
    • Half-Life (forked Source Engine)
  • Successors:
    • id Tech 2, 3, 4, 5, 6, 7 (2020)

Quake source code: link

Game Engines Overview

Game Engine Primary Modules

  • Game Loop - the heartbeat of all games
  • Scene Manager - manages objects and structures them in a scene graph
  • Input Manager - handles inputs (keyboard, mouse, touch, joystick, gamepad,...)
  • Resource Manager - manages assets, controls a cache
  • 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,...

Game Loop

Game Loop

  • the most important part of the game engine
  • each turn advances the state of the game
  • usually coordinated with the event loop of the platform/virtual machine
  • optimal time step for rendering: 60 FPS = 16.6 ms per frame
  • audio, input, and physics 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

  • synchronization can be provided by delayed invocation
  • first used in Ultima VII (1994)

Update method

Fixed time step

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

Variable time step

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

Adaptive time step

  • switches between variable and fixed time step based on thresholds
  • 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 the update - one-frame-off lag
  • possible solutions: bucket update, script execution order (Unity), process priority (Godot)

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

Scene Graph

Scene Graph

Scene Graph

  • essential structure of every interactive application
  • a way of ordering data into a hierarchy
  • represented as an 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

  • uses scene graph as a logical hierarchy
  • responsibility: communication, binding to logical components
  • Unity Engine - game objects form a hierarchy
  • Unreal Engine - components form a hierarchy
  • Godot Engine - everyting forms a hierarchy

Scripting Engine

Game Engine Scripting API

  • the engine needs to communicate with the script - provided by bridging
  • 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)
  • applies to visual representations as well

Introduction to Assets

What are assets?

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

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

All media elements that are presented to the player and protected by copyright.

Resource Manager

Resource Manager

Provides access to all resources (~assets)

  • meshes, materials, shaders, animations, textures, clips, levels
  • 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

Data loading

Level loading

  • only one game chunk is loaded at a time
  • requires a loading screen or a pre-rendered cutscene
  • used in Tomb Raider, Doom, Quake,...

Air locks

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

World streams

  • the engine unloads regions too far away and loads new regions the player is heading to
  • 2D uses lazy loading
  • 3D uses LOD system - chunks are loaded at a variable granularity
  • used in GTA, WoW, Arma, and many 2D scrollers

Multimedia assets

Image Formats

JPEG

  • Joint Photographic Experts Group
  • used for digital photography
  • doesn't support alpha channel
  • don't use it for pixel-art

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
JPG image
PNG image
SVG image

Videos and Cutscenes

  • 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
  • 2020's - in-game cutscenes
Warcraft cutsceneHellblade cutscene

Textures

  • pieces of bitmaps 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
Color texture

Diffuse

Displacement texture

Displacement

Normal texture

Normal

Volumetric texture

Volumetric

Tileable texture

Tileable

Sprite animation

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
Prince of Persia sprites

Game Model

Game Model

Domain Model

  • contains rules, game mechanics and essential data to the gameplay

Presentation Model

  • a model represented to the player
  • e.g. played sounds, animated sprites

Physics model

  • all entities that take part in physical interaction

AI model

  • all data and structures that are processed by AI (navigation maps, behavioral trees,...)

Networking model

  • everything that has to be in sync with other models on other devices

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 (+ Attribute)
  • 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 the scene graph

Terms

Entity

  • a single entity/game object

Component

  • instantiable unit that defines functional behavior

System

  • a superior component responsible for a bigger scope (HealthSystem, GameManager, AudioSystem)

Attribute

  • data attached to an entity
  • some engines use data components instead of attributes
The naming varies. Some engines use Behaviours for Components, Components for Systems, Properties for Attributes etc.

Messaging

How components communicate

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
  • e.g. listening to ITEM_PICK event and playing a sound

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

Action Patterns

Processes and Actions

  • Process - something that requires more than one frame to finish
    • basically everything that involves animations and accumulated player input

Example: Pacman actions

Responsibility ownership

  • determines which component should be responsible for given scope/action/decision
  • there is no bulletproof recipe, yet it should be unified within the game
  • 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 to a parent element up the scene graph (e.g. the root object)
    • example: battle formation controller, duel controller (who wins, who loses)

Individual units

Battle formation

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

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 }

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 implement a simple state machine
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 }

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 }

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
Digital sound diagram

Sound synthesizing, recording and composing

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
Sound Waves

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
    Music features - looping
  • branching - conditional music
    Music features - branching
  • layering - some pieces can be muted on and off and re-combined
    Music features - layering
  • transitions - moving smoothly from one cue to another
    Music features - transitions

Sounds in games

3D Sound

Attenuation

  • the further the sound, the quieter it is
Attenuation in 3D sound

Occlusion

  • how sound is occluded by solid objects, losing its high frequency
Occlusion in 3D sound

Obstruction

  • when the direct path to a sound is muffled but not enclosed
  • creates delays
Obstruction in 3D sound

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

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)

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

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

  • Quad-tree - for 2D and semi-3D space
  • Oct-tree - for 3D space
  • Grid - a square grid
  • BSP (binary-space partitioning), portals, anti-portals,...

Oct-tree

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

Navigation

Pathfinding algorithms

Uniformed graph searches

  • ignores 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 to every other node in the graph
    • uses CSF (cost-so-far) metric
  • 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 F = G + H , where G  is the cost-so-far and H  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

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
  • |a| = \sqrt{a_x^2+a_y^2+a_z^2}

Magnitude of a vector

Normalization

  • a unit vector is a vector with a magnitude of one: \mathrm{u} = \frac{\mathrm{v}}{|\mathrm{v}|}

Normal vector

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

Dot product

  • \mathrm{a}\cdot\mathrm{b} = a_xb_x+a_yb_y+a_zb_z
  • \mathrm{a}\cdot\mathrm{b} = |\mathrm{a}|\cdot |\mathrm{b}|\cdot \cos{(\theta)}

Cross product

  • yields another vector that is perpendicular to two vectors
  • \mathrm{a}\times\mathrm{b} = [(a_yb_z - a_zb_y),(a_zb_x - a_xb_z),(a_xb_y - a_yb_x)]

Projection

  • projection of a vector onto another vector
  • \mathrm{v'} = \frac{\mathrm{w}\cdot\mathrm{v}}{|\mathrm{v}|^2}\cdot \mathrm{v}

Steering Behaviors

Steering Behaviors

  • a set of algorithms 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 environment, with the 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

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 Engine

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)

Collision Detection

Bounding Boxes

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

Bounding Boxes

OBB

  • oriented bounding box
  • defined by a position and orientation
  • commonly used

k-DOP

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

Compound shapes

  • more efficient alternative to a poly-soup
  • the system first tests bounding volumes of compound shapes

Convex hull

  • smallest convex volume containing the object

SAT

SAT (separating axis theorem)

  • 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
  • AABB in 2D: only 2 axis to 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 Raycasting or more advanced Swept Shapes technique
  • Raycasting - the object emits a ray which is tested for intersection
  • Swept Shapes - a new shape is formed by the motion of the original one

Space

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

Quaternions

  • axis + angle + extra algebra
  • q = [q_x, q_y, q_z, q_w]
  • alternative form: q = a + b\boldsymbol{i} + c\boldsymbol{j} + d\boldsymbol{k}
  • unit-length: (q_x^2 + q_y^2 + q_z^2 + q_w^2 = 1)
  • a unit quaternion can be visualised as a 3D vector + scalar
  • Q = (\cos{\frac{\varphi}{2}}, i \cdot \sin{\frac{\varphi}{2}}, j \cdot \sin{\frac{\varphi}{2}}, k \cdot \sin{\frac{\varphi}{2}})
  • 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

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

Animations

Interpolation

  • a 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

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 other
  • 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

ASCIIdent

Compute shader

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

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

Light baking

  • common technique to create pre-rendered static lights
  • is usually combined with dynamic lighting - when a scene has many lights, most of them can be baked
  • only works for static scenes

Textured scene

Baked lights

Result

AI introduction

Challenges for Game AI

Game AI features and limits

  • real-time
  • limited resources
  • incomplete knowledge
  • planning
  • learning
  • acceleration (strategies)

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

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 }
Doom scripting

Finite State Machine

  • the oldest and most commonly used formalism to model game AIs
  • useful for entities with a small set of distinct states
    Dungeon FSM
  • each entity can be in exactly one of a finite number of states at any time
  • Definition
    • quadruple: (Q, \Sigma, \delta, q_0)
    • Q  is a finite, non-empty set of states
    • \Sigma  is a finite set of inputs
    • \delta: Q \times \Sigma \rightarrow Q  is the state-transition function
    • q_0  is an initial state, q_0 \in Q
  • 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
HFSM doomguard
doomguard

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 example

Behavior Tree

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

Real AI in games

Terms

Planning

  • a formalized process of searching for sequence of actions to satisfy a goal

Supervised learning

  • works just like learning at schools
  • for certain input, there is a correct output the algorithm has to learn

Unsupervised learning

  • the output cannot be categorized as being either correct or false
  • the algorithm learns patterns instead

Reinforcement learning

  • an agent must learn the best possible output by using trial and error
  • for each action chosen the agent obtains a feedback

AI in strategies

Real-time strategy

  • Real-time strategy is a Bayesian, zero-sum game (Rubinstein, 1994)
    Total War
  • 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

  • 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

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

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 n  peers, each must have n-1  connections -> O(n^2)  in total
  • methods: single master, partial authority, full replication

Client-Server architecture

  • n  devices, n-1  connections
  • server must handle (n-1)\times  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

Transport

Message Types

Stream

  • doesn't need to be confirmed, contains a collection of continuous values (e.g., position change)

Snapshot

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

Event

  • 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

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 approach

  • 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