MI-APH Summary

Czech technical University in Prague
Faculty of Information Technology
Department of Software Engineering
© Adam Vesecký, MI-APH, 2019

Lecture 01: Introduction to the world of games and game engines

What is a computer game?

Habitica

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

Computer Program

  • a set of instructions that can be executed by a computer

Computer Application

  • 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

General consensus

Computer games are real-time interactive agent-based simulators.

List of 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

  • 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

Simulation

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

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

Game Mechanics

Space

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

Objects, attributes

  • entities that can be seen or manipulated

Actions

  • an object's turn to act

Rules

  • statements that describe constraints, consequences and goals

ID Tech

  • Family of game engines developed by ID software
  • Id Tech 0 - the very first engine
  • Every next game had more advanced technology
  • Still, memory constraints sabotaged attempts to create data-heavy design

WAD File

"Where's All the Data?"

  • Binary format of package files for Doom
    • Levels (walls, floors, monsters)
    • Sound effects, music
    • Color palettes, images
  • Designed by John D. Carmack

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 (Doom Eternal)

Lecture 02: Architecture of Game Engines

Application, Loop, System

Game Application

  1. Handle everything we need to set up the application
  2. Register each handler
  3. Initialize rendering window
  4. Start the game loop
  5. Quit the application

Game loop

  • Process inputs
  • Process game logic
  • Erase the screen and draw game objects
  • Repeat

Game Engine Modules

Main 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 - bridge between the engine and interpreted languages (JS, Lua, C#,...)
  • Multimedia Engine - plays music, clips, sounds, video
  • AI Engine - abstract engine for AI (path finding, states, behavioral trees, ...)
  • Networking Engine - handles multipeer communication

Other modules

  • GUI framework, Level Editor, Camera, Event System, World Streaming, Security, LOD, Profiler,...

Game Loop

  • Simple, yet the most important part of the game engine
  • Each turn advances the state of the game
  • Sometimes it's coordinated with the platform's event loop
  • Optimal time step for rendering: 60FPS = 16.6 ms per frame
  • Audio is usually separated as it requires more frequent updates (~200 FPS)
  • Certain systems don't need to be updated so frequently (AI)
In general, a program spends 90% of its time in 10% of the code. The game loop will be firmly in those 10%

Simple Game Loop

Multi-threaded game loop

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 (physics)

Adaptive time step

  • switches between variable and fixed time step
  • based on thresholds or a more sophisticated approach
  • better dealing with breakpoints

Update inconsistencies

Game objects are consistent before and after every update

  • yet they may be inconsistent during the update
  • major source of confusion and bugs
  • PixiJS updates all dependent transformations instantly

One-frame-off lag

  • the state of some objects lags one frame behind the states of the others
  • possible solutions: bucket update, script execution order (Unity)

Object 2 reads updated state of Object 1 but not updated state of Object 3

Scene Graph

Scene Graph

  • essential structure of every interactive application
  • a way of ordering the data into a hierarchy
  • N-Tree or a regular graph
  • parent nodes affect child nodes (translation, rotation, scale,...)
  • leaves usually represent atomic units (shapes, vertices, meshes)
  • implementation: arrays, oct-trees, quad-trees, bounding volume hierarchies,...

Scene Manager

  • manages objects in the scene
  • similar to HTML Document Object Model and Event Manager
  • responsibility: sending messages, searching for objects, applying transformation constraints,...
  • Unity Engine - game objects form a hierarchy
  • Unreal Engine - components form a hierarchy

Resource Manager

  • Provides access to all resources (assets)
    • meshes, materials, shaders, animations, textures, clips, levels
    • many assets are not used in their original format
    • engines usually encode their resource metadata in XML files
    • 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 of the file, guaranteed to be unique
  • Loads required resources and unloads those no longer needed
    • loading is simpler than unloading
  • Handles streaming

Audio Engine

Audio pipeline

  • for each 3D sound, a dry digital PCM signal must be synthesized
  • distance-based attenuation - provides a sense of distance
  • reverb - provides accoustics
  • dry signals - arrive via an unobstructed path
  • wet signals - echo (early reflections) + tail (late reverberations)
  • audio buffer must be fed periodically - dropping audio is worse than dropped frames

Audio Assets

Audio clips

  • digital sound asset (MP3, OGG, WAV)
  • module asset (MOD, S3M, IT, XM), not used anymore, yet it's fun to play around with them
  • MIDI - sequencer-related data, nowadays mainly for recording and preprocessing

Sound cues

  • collection of audio clips with metadata

Sound banks

  • package of sound clips and cues

Streaming sounds

  • small ring buffer for music and speech

Input Manager

Detects input events from devices

Atomic events

  • KEY_DOWN
  • KEY_UP
  • MOUSE_BUTTON_DOWN
  • MOUSE_BUTTON_UP
  • MOUSE_WHEEL
  • MOUSE_MOTION

Compound events

  • FLING
  • PINCH_TO_ZOOM
  • DOUBLE_TAP

Special events

  • cheat codes
  • fighting combos

Input Devices

Getting the state of the device

  • polling - compare against previous state
  • callbacks - handled by upper SW layer
  • via a protocol (wireless device)

Devices

  • keyboard, touch sensor
  • one-axis controller - single analog state
  • two-axis controller - mouse and joystick
  • three-axis controller - accelerometer
  • camera, VR lens, Azure Kinect

Dead zone

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

Normalization

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

Memory Manager

Main issue

  • the default memory manager that comes with default C-runtime libraries is not suitable for
    most game applications
  • game engines usually implement their own allocator

Custom allocators

  • stack-based
  • pool-based
  • heap-based
  • bucket allocators

Pool-based 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

Loading approaches

Level loading

  • used in Tomb Raider, Doom,...
  • requires a loading screen
  • only one game chunk is loaded at a time

Air locks

  • used in Half-Life 2, Portal, Inside, new Wolfenstein,...
  • larger block contains the whole scene
  • smaller block represents an air lock (usually a small room/hall)
  • when the player enters the are from which can neither see the previous area nor return to it, next scene is loaded

World streams

  • used in open-world games: GTA, WoW, ARMA, Read Dead Redemption, Witcher,...
  • the world is divided into regions
  • when the player enters region B and is far enough that chunk A can no longer be seen, the engine unloads chunk A and starts loading chunk C
  • LOD (level of detail) - chunks are loaded with variable granularity (only meshes)

Lecture 03: Component Architecture I

Various models for various purposes

View model

  • the one represented on screen as a result of processing all other models

Networking model

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

Core model

  • describes a state of a game at any point in time
  • tightly coupled with networking model (if there is one)

Physics model

  • all entities taking part in physical interaction

AI model

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

All-purpose object pattern

  • Used in 90's
  • One class determines behavior of every game object
  • Lots of switches and if-checks

Component

  • A unit of composition with specified interfaces and explicit context dependencies
  • components are basically plug-and-play objects
  • prefers composition over inheritance
  • it is not about how to organize your models but how to tie them up

Architectures

Monolithic

  • discussed in previous chapter
  • traditional OOP way
  • problems with deep and wide hierarchies

Object-centric

  • each game object is represented in runtime by a single class instance
  • Early games: Thief: The Dark Project (1998), Dungeon Siege (2002)

Property-centric

  • each game object is represented only by a unique id
  • cache-friendly (components are stored contiguously in memory)
  • identifiers: integer, string, hashed string,...
  • early game: Deus Ex 2 (2003)

ECS/ECSA pattern

  • ECS+A - Entity-component-system + attribute
  • game object is just a container for data and logic
  • can be easily integrated into scene graph

ECS/ECSA PATTERN

The overall behavior of a particular game object is fully determined
by the aggregation of its components (and attributes).

Terms

Entity (GameObject)

  • represents a single entity, usually a node in a scene graph

Attribute

  • member variable replacement, contains data

Component

  • instantiable entity that defines functional behavior

(Sub)system

  • superior component responsible for a system (Renderer, GameManager, AudioSystem)

Message

  • method call replacement, used to communicate among components

Event

  • type of a message; something that already happened (usually a broadcast)

Command

  • type of a message; something we want to happen (usually a multicast or a unicast)

Lecture 04: Component Architecture II

Component-oriented approach

  • scalable
  • data-oriented
  • components are easy to reuse
  • easy to make new object types
  • polymorphic operations for components
  • dynamic typing - everything is assembled at runtime
  • all dependencies have to be wired together
  • code must be written in an utterly generic way
  • refactoring may become very difficult
  • harder to debug

Communication practices

By modifying the container object's state

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

By direct calls

  • OP way
  • fast, but increases coupling
  • e.g.: group of components that are always bound together

By messaging systems

  • events and commands
  • each component can declare interest in relevant messages
  • slower than the direct call, but that cost is negligible in all but performance-critical code
  • difficult to debug, messages can fall into an infinite loop
  • can be implemented via polymorphism, arrow functions,...
  • e.g.: game-over event

Messaging system

  • Components should be notified of any state change that are relevant to them
  • Can be used for returning values (danger of feedback deadlock )
  • Blind event forwarding - an object can forward an event to another object
  • One handler - OnMessage() method, implemented in each component
  • Processing can be instant or delayed
  • Event Queue - pattern for batch message processing, can post events with a delay

Message types

Unicast

  • a component sends a message to another component
  • in most cases this can be handled by a direct call
  • example: kill an object

Multicast

  • a) component sends a message to specific subscribers
  • b) component sends a message to all objects that meet specific criteria
  • example: notify all nearby units that an enemy has entered the area
  • example: a unit was destroyed -> notify everyone interested

Broadcast

  • rarely used (observer pattern doesn't stick to it)
  • usually for System-Entities communication
  • example: level completed, game over, player died

Lecture 05: Game Programming Patterns

Two-stage initialization

  • Avoids passing everything through the constructor
  • Constructor creates an object, init method initializes it
  • Objects can be initialized several times
  • Objects can be allocated in-advance in a pool

class Brainbot extends Unit {

 

  private damage: number;

  private currentWeapon: WeaponType;

 

  constructor() {

    super(UnitType.BRAIN_BOT);

  }

 

  init(attributes: UnitAttribs) {

    // set default values

    this.damage = DEFAULT_DAMAGE_BRAINBOT;

    this.currentWeapon = WeaponType.LASERGUN;

    this.attributes = attributes;

  }

}

State

  • several meanings (state of the whole game, internal state of entities,...)
  • in this context, it is just a member of a set, determining what actions an object may execute
  • // stateless, the creature will jump each frame

    creatureUpdate(delta: number, absolute: number) {

      if(pressedKeys.has(KeyCode.UP)) {

        this.creature.jump();

      }

    }

     

    // a variable that makes certain actions possible only according to the current state

    creatureUpdate(delta: number, absolute: number) {

      if(pressedKeys.has(KeyCode.UP) && this.creature.state !== STATE_JUMPING) {

        this.creature.changeState(STATE_JUMPING);

        this.creature.jump();

      }

    }

Flags

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

Dirty Flag

  • marks changed objects
  • can be applied to various attributes (animation, physics, transformation)
  • you have to make sure to set the flag every time the state changes
  • you have to keep the previous derived data in memory

Cleaning

  • When the result is needed
    • Avoids doing recalculation if the result is never used
    • Game can freeze for expensive calculations
  • At well-defined checkpoints
    • less impact on user experience
    • you never know, when it happens
  • On the background
    • You can do more redundant work
    • race-condition may occur

Builder

  • slightly different from the builder defined by GoF
  • stores attributes needed to build a game object, can be used to build several objects
  • Aspect in Artemis framework, Prefab in Unity
  • class Builder {

      private _position: Vector;

      private _scale: Vector;

     

      position(pos: Vector) {

        this.position = pos;

        return this;

      }

     

      scale(scale: Vector) {

        this.scale = scale;

        return this;

      }

     

      build() {

        return new GameObject(this._positionthis._scale);

      }

    }

     

    new Builder().position(new Vector(1254)).scale(new Vector(21)).build();

Factory

  • Builder assembles an object, factory manages the assembling
  • Factory creates an object according to the parameters but with respect to the context
  • class UnitFactory {

     

      private pikemanBuilder: Builder// preconfigured to build pikemans

      private musketeerBuilder: Builder// preconfigured to build musketeers

      private archerBuilder: Builder// preconfigured to build archers

     

      public spawnPikeman(position: Vectorfaction: FactionType): GameObject {

        return this.pikeman.position(position).faction(faction).build();

      }

     

      public spawnMusketeer(position: Vectorfaction: FactionType): GameObject {

        return this.musketeerBuilder.position(position).faction(faction).build();

      }

     

      public spawnArcher(position: Vectorfaction: FactionType): GameObject {

        return this.archerBuilder.position(position).faction(faction).build();

      }

    }

Flyweight

  • an object holds shared data to support large number of fine-grained objects
  • example: instanced rendering, geometry hashing, particle systems

Replay

  • allows to reproduce any state of a game at any time
  • much more complex than the save mechanism
  • all game entities must have a reproducible behavior (similar to multiplayer facility)
  • two main impediments: random functions and nondeterministic operations
  • Solution a)
    • store the state of all objects in the game - either on frame basis or at a fixed frequency
    • reproduce them by modifying all objects at each frame
  • Solution b)
    • if the game is completely message-driven, we can store all game messages
    • during the replay, forbid all components to send messages on their own
    • send queued messages one by one and let them be processed

Lecture 06: Math and Dynamics

Random functions distribution

Uniform distribution

  • most common distribution of random generators
  • applications: noise, shuffling, one-of-many selection

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 current 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)
  • makes game events occur proportionally
  • can be defined as a rarity table, calculated via weighted sum

Random encounter

  • popular mechanics of RPG games (Final Fantasy, WoW, 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)

Perlin Noise

  • Perlin Noise - developed by Ken Perlin in 1983
  • Simplex Noise - Perlin's improved noise, fewer artifacts and lower computational overhead
  • both are gradient noises - we set a pseudo-random gradient at regularly spaced points in space and interpolate between them
  • the noise is constructed from octaves (contribution to the signal at a particular scale)
  • the signal is interpolated via a quartic function:

float PerlinNoise2D(int xint yfloat persistenceint octavesfloat zoom) {

    float total = 0.0f;

    // initial frequency and amplitude

    float frequency = zoom;

    float amplitude = 1.0f;

 

    for (int i = 0; i < octaves; i++) {

        // calculate noise

        total = total + InterpolatedNoise(x*frequency, y*frequency) * amplitude;

        // update frequency and amplitude

        frequency = frequency * 2;

        amplitude = amplitude * persistence;

    }

    return total; }

Spatial Partitioning

Oct-tree

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

Quad-tree

  • hierarchical partition
  • each inner node has 4 children
  • it is common to have the children of same size -> we won't need to save the position vector
  • overlapping objects are put it into all children they touch
  • only objects in the same leaf can be in collision
  • useful for outdoor scenes, where objects are placed on a landscape
  • good for a small amount of objects of various sizes

Quad-tree for geometric hashing

Quad-tree for bounding volumes

Steering behaviors

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

Seek

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

Flee and arrive

Flee

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

Arrive

  • seek is not good at stopping
  • arrive decelerates the agent onto the target position
  • additional parameter: slowing radius

Pursuit and Evade

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 pursuite
  • the evader flees from the estimated future position

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
  • smoothness of movement depends on three parameters:
    • circle radius, distance from the vehicle and jittering (randomness)
  • the greater the radius and the distance, the stronger the force

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,...)

Obstacle avoidance and offset pursuit

Obstacle avoidance

  • steers a vehicle to avoid obstacles (usually approximated by a circle)
  • vehicle has a detection box - rectangular area
  • two forces are calculated: lateral force and braking force

Offset pursuit

  • keeps a vehicle positioned at a specified offset from a leader vehicle
  • useful for battle formations

Flocking

  • emergent behavior, more agents/vehicles are taken into consideration
  • combination of three aspects:
    • separation - steers a vehicle away from its neighborhood
    • alignment - keeps the vehicle's direction aligned with its neighbors
    • cohesion - moves a vehicle toward the center of mass of its neighbors

Points and Vectors

Vector addition and subtraction

Magnitude of a vector

  • 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

Points and Vectors

  • Magnitude
    • scalar representing the length of the 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
  • Cross product
    • yields another vector that is perpendicular to two vectors

Integration methods

Implicit methods

  • make use of quantities from the next time step
  • decrease energy from the system

Semi-implicit methods

  • combination of explicit and implicit methods
  • very stable

Explicit methods

  • make use of known quantities at each time step

Runge-Kutta family

  • Euler methods, midpoint methods, RK4,...

Verlet family

  • Regular Verlet
  • Leapfrog Verlet
  • Velocity Verlet

Euler integration

  • Explicit method

    Improved method

    Implicit method

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

Lecture 07: Physics

Physics engine

  • System that simulates physical phenomena
  • computes motion of objects in virtual scene
  • simulation must be real-time (accuracy is not that important)
  • 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
  • takes significant computing resources
  • is based on heavy magic tricks that are difficult to comprehend

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

Soft Body

  • can be deformed

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

Primitives

Sphere

  • center point and radius (4 numbers)

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

Primitives

Poly Soup

  • used to model complex static geometry (terrain)
  • very expensive kind of collision test

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

Complex shape/volume

  • not necessarily convex
  • simplified mesh/sprite
  • needs preprocessing (BSP)

Comparison

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,...

Example: SAT

  • AABB in 2D: only 2 axes to check

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
  • a new shape is formed by the motion of the original one
  • rotating shapes may result in shapes that aren't convex

Collision queries

Queries:
  • Find the first target the bullet hits
  • Can a camera move without interpenetrating the wall?
  • Find all objects within a given radius

Ray casting

  • any game is going to need a good raycaster
  • the cast line segment is tested against the collidable objects in the collision world; if it intersects any of them, the contact point is returned
  • weapon systems, player mechanics, AI systems, vehicle systems, line-of-sight
  • used in 80's and 90's also for pseudo-3D rendering, nowadays it's being replaced by raytracing

Shape casting

  • how far a shape would be able to travel along a directed line segment before it hits something
  • sphere casts - e.g. to determine whether the camera is in a collision
  • capsule casts - e.g. character movement on uneven terrain

Particle systems

  • a collection of point masses that obeys certain physical laws
  • can model complex fuzzy shapes and dynamics
  • heavily used 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 attributes
  5. render meshes

Lecture 08: Graphics

Rotation in 3D space

Rotational representations

Euler angles

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

Axis + angle

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

Rotational representations

Quaternions

  • similar to axis + angle, however, this is an algebraic field
  • 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
Usually, Euler angles are used for fast rotation around one axis
and quaternions for complex yet slow 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 (e.g. a nose of an animal)

World space

  • fixed coordinate space, in which the positions, orientations and scales 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

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

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 the 2D surface on which the world will be projected

Perspective projection

Orthographic projection

Animations

Stop-motion animation

  • predefined set of images/sprites
  • simple and intuitive, but impossible to re-define without changing the assets

Keyframed animation

  • keyframes contain values (position, color, modifier,...) at given point of time
  • intermediate frames are interpolated

Sprite animation

Skeletal 2D

Skeletal 3D

Vertex

Interpolation

  • Method that calculates points within the range of given points
  • Applications
    • graphics - image resizing
    • animations - morph between two transformations
    • multiplayer - morph between two game states
    • video - keyframe interpolation
    • sound processing - sample interpolation
  • Main methods
    • Constant interpolation (none/hold)
    • Linear interpolation
    • Cosine interpolation
    • Cubic interpolation
    • Bézier interpolation
    • Hermite interpolation

Linear interpolation

Linear interpolation

  • for 1D values (time, sound)

Bilinear interpolation

  • on a rectilinear 2D grid
  • for 2D values (images, textures)
  • Q - known points (closest pixels)
  • P - desired point

Trilinear interpolation

  • on a regular 3D grid
  • for 3D values (mipmaps)

Terms

Z-Fighting

Vertex

  • primarily a point in 3D space with  coordinates
  • attributes: position vector, normal, color,  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
  • 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, even physics or 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

Compute shader

  • shader that run outside of the rendering pipeline
  • used for massively parallel GPGPU computing

Lecture 09: Game AI

Navigation graph

Waypoint-based

  • level designer places waypoints that are later linked up

Mesh-based

  • created from a polygonal representation of the environment's floor
  • describes walkable areas

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 environmental 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, first described 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 (penalty for direction change)
    • 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 costs
  • Dijkstra ignores position of the target
  • A* takes into account both of them

Scripting

  • IF-THIS-THEN-THAT approach
  • AI behavior is completely hardcoded
  • simple, easy to debug, easy to extend if programmed properly
  • human player should behave as the developers expect
  • good scripting behavior must cover a large amount of situations

// Doom 2: find player to chase

void A_Look (mobj_t* actor) {

    mobj_t* targ;

    actor->threshold = 0;   // any shot will wake up

    targ = actor->subsector->sector->soundtarget;

      if (actor->flags & MF_AMBUSH){

        if (P_CheckSight (actor, actor->target))

        goto seeyou;

    } else goto seeyou;

  

    if (!P_LookForPlayers (actor, false)) return;

    // go into chase state

seeyou:

    P_ChasePlayer();

}

Finite state machine

  • the oldest and most commonly used formalism to model game AIs
  • useful for an entity whose behavior changes based on an internal state that can be divided
    into small number of distinct options
  • each game 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 substate
  • groups of states share transitions
  • usually implemented as a stack
    • push a low-level state on the stack when enter
    • pop and move to the next state when finished

Behavior tree

  • tree of hierarchical nodes that control decision making process
  • originate from gaming industry around 2004 (Halo 2)
  • combine elements from both Scripting and HFSMs
  • there is no standardized formalization
  • inner nodes lead to appropriate leaves best suited to the situation
  • 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 depends...It depends...It depends...
ParallelIf N children succeedIf M-N children succeedIf all children are running
ActionWhen completedUpon an errorDuring completion
ConditionIf trueIf falseNever

Terms

Bot

  • an intelligent artificial player emulating a human player
  • used mainly to describe AI in FPS game

NPC

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

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

Occupancy map

  • a grid over the game environment
  • maintains probability for each grid cell
  • probability represents the likelihood of an opponent presence
  • when the opponent is not visible, the probabilities from the previous occupancy cells are propagated along the edges to neighboring cells

Threat map

  • used in RTS
  • the value at any coordinate is the damage that the enemy can inflict
  • updating is done through iterating the list of known enemies
  • can be easily integrated into pathfinding
  • Example: combat units bypass enemy defensive structure in order to attack their infrastructure

Influence map

  • more generalized than threat maps
  • helps the AI to make better decisions by providing useful information
    • situation summary (borders, enemy presence)
    • statistics summary (number of visits, number of assaults)
    • future predictions
  • Example: number of units killed during last strike

Goal-oriented action planning

  • centers on the idea of goals as desirable world states
  • each action has a set of conditions it can satisfy, as well as a set of preconditions that
    must be true in order to be satisfied
  • originally implemented for F.E.A.R (2005)
  • two stages: select a relevant goal and attempt to fulfill that goal

RTS features

Resource control

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

Tech tree

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

Build order (opening)

  • the timings 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
  • we can prioritize economy over technology or military production

Lecture 10: Scripting Languages

Scripts

Script

  • a piece of code that performs a specific task
  • originally, scripts were simple languages for punch-cards processing

Scripting language

  • common definition: a scripting language is a high-level language that can be interpreted by another program at runtime
  • it is more about an environment than the language itself - even C/C++ can be considered as a scripting language if loaded by an interpreter
  • C# is compiled into byte-code that is interpreted at runtime by .NET, yet most people don't consider it as a scripting language

Common characteristics

  • economy of expression
  • flexible dynamic typing
  • easy access to other programs

Game Engine script API

  • the engine needs to communicate with the scripting part - provided by bridges
    • JNI (Java - C++)
    • P/Invoke (.NET - C++)
    • LuaBridge (Lua - C++)
    • Dukbind (Duktape JS - C++)
  • bridge is a performance bottleneck, especially for per-frame calls
  • more scripting languages -> more bridges to maintain
    • crossing the boundary between C++ and the script is slow while marshalling a large amount of data
  • Marshalling
    • transforming the memory representation of an object between two domains (different programming languages)
  • Semantic gap
    • descriptive difference of an object in various representations (relational database, object-oriented structure)

Lecture 11: Multiplayer

Multiplayer categories

  • single-screen multiplayer
  • split-screen multiplayer
  • networked multiplayer
  • MMOG

Issues

  • the main objective: how to synchronize several universes
  • all clients have to achieve a certain degree of synchrony
  • there is no (known) real-world picture for this type of problem
  • impact on the game design - game model, animation engine, sound engine, vehicle model, AI engine, physics,...
  • implement multiplayer features into a single-player game is a painful task
  • converting a multiplayer game into a single-player game is trivial
  • Naive approach: transfer a complete game state repeatedly to all clients
  • Most common approach: transfer a minimal subset of a game state that is required to reconstruct the complete information
  • Topologies: peer-to-peer, client-server

Peer-to-peer architecture

  • each device exchanges data with each other in a fully connected graph
  • used in Doom, early Command & Conquer, Age of Empires, Starcraft
  • given  peers, each peer 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)
  • it is quite clear which code runs on a server machine
  • Dedicated server - only runs the game state and communicates
  • Listen server - server is an active participant in the game itself

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 usually consists of a type of an object and all parameters required to construct this object on all machines

 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);

    ...

  }

Latency handling summary

Interpolation/extrapolation

  • smoothens out incoming values by interpolating 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 12: Indie Games Development

Goodbye quote

You are not prepared!Illidan Stormrage