Player-Computer Interaction 

GAME ENGINE
ARCHITECTURE

UNIT 12:

Prof. Dr. Eike Langbehn

Department of Media Technology

Faculty of Design, Media and Information

Hamburg University of Applied Sciences

SECTION NAME

EXAMPLES

UNIT 2:

AGENDA

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

1. INTRO

2. REQUIREMENT
ANALYSIS

3. GAME
DESIGN

0. ORGANIZATION

4. GETTING STARTED
WITH GODOT

5. USER
STUDIES

6. ANALYSIS OF
HUMAN FACTORS

7. INTERACTION
DESIGN

8. ADVANCED PROGRAMMING
WITH GODOT

9. EVALUATION
MODELS

10. MARKET
ANALYSIS

11. NARRATIVE
DESIGN

12. GAME ENGINE
ARCHITECTURE

LEARNING outcomes

  • Understanding of software architecture in games 
  • Knowledge of most common structures and principles in game programming

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

software architecture

  • What is software architecture?
    • Organization of code
    • Often by abstraction and decoupling
  • Why is it different in games/interactive systems?
    • Interactivity and real-time
    • Time and sequencing
    • Rapidly build and iterate
    • Interactions between systems and objects
    • Performance
  • What is the goal of software architecture?
    • Minimize the amount of knowledge you need to have in-cranium before you can make progress
      • Loosely coupled modules/systems
      • Clean written code
    • The measure of an architecture is how easily it accommodates changes
      • Maintainable
      • Flexible
      • Easy to extend

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Get problem

Learn code

Code solution

Clean up

software architecture

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

  • What is decoupling?
    • A change to one piece of code doesn’t necessitate a change to another
  • What are design pattern?
    • Solution to common problems
  • Drawbacks of Software Architecture?
    • Performance
    • (Short-term) Development time

Runtime Performance (Game’s Execution Speed)

(Short-term) Development Speed

(Long-term) Development Speed

game engine architecture

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

"A game engine is a software development environment designed for people to build games."

  • Engine consists of systems...
    • Rendering
    • Physics
    • Networking
    • Audio
    • Input
    • ...
  • ... and tools
    • Level Editor
    • Profiler
    • Asset Importer
    • ...
  • How to separate engine and game?
  • How to connect the subsystems of an engine?
  • How to integrate ressources/assets into the engine/game?

high level architecture

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Application Layer

Game Logic

Game View

Application Layer

  • Abstraction Layer for
    • Hardware
    • Operating System
    • Driver
    • Graphics API (DirectX/OpenGL)

Game Logic

  • Holds Game State
    • All entities (objects) in the game world with all of their attributes and abilities
      • Players, Enemies, Vehicles, Buildings, Plants, Items, ...
    • Informations about all these objects
      • Hitpoints, Damage, Health, ...
  • Changes Game State
    • Rules, Behaviours of those objects
      • Move, Collide, Attack, Explode, ...

Game View

  • Presents/displays the game state
    • Graphics
    • Sounds
    • etc.
  • Offers controls & input methods
  • Can be different for human, AI, remote, ...

high level architecture

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Application Layer

Game Logic

Game View

  • This approach is similar to MVC - Model View Controller
  • Model: Game Logic / Game State
  • View: Game View
  • Controller: Game Logic / Behaviour

high level architecture

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Application Layer

Game Logic

Game View

Communication

example: communication

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Game Logic

Game View

Player_hit_brake

GameState_updated

ButtonA_pressed

Plays Sound Effect 

Plays Visual Effect 

Exactly the same for Human, AI, Remote, ... 

overview

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

Game Logic

Game View

Application Layer

Hardware

  • Input
  • Memory 
  • Files

OS

  • Threads
  • Network
  • Driver

Game Lifetime

  • Init/Shutdown
  • Main Loop
  • Event System
  • Game State
  • Behaviour
  • Physics
  • Rendering
  • Audio
  • Input

Game View - AI Agent

  • Stimulus Interpreter
  • Decision System

main loop / game loop

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

  • Main Loop or Game Loop or Update Loop is the central construct of every game or interactive real-time system
  • Why a loop?
    • Static scene = Picture
    • Change is necessary for real-time interactivity
    • Non-interactive program starts, does what it has to do and ends
    • Interactive program starts, blocks and waits for user input
  • Engine keeps the program running
    • Engine updates the world
    • One loop cycle = frame / tick
    •  > 24 updates per second
    •  (Ideally 60+ per eye)
    • Updates per second = framerate
    • FPS dependent on
      • computer hardware
      • amount of work to do
public static void main()
{ //Start
Console.Write("I did something!");
} //end
public static void main()
{
 while (running)
 {
 Event* event = waitForEvent();
 dispatchEvent(event);
 }
}
public static void main()
{
 while(running)
 {
 processInput();
 updateGameState();
 output();
 }
}

Initialization

Process Player Input

Perform Game Logic
(AI, Physics, World Simulation, etc.)

Render Scene, Play SFX, etc.

Shutdown

exit

Main Loop

example: pong

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

public static void main() {
 while
(running
)
 {
 processInput();
 updateGameState();
 output();
 }
void main() {
	initGame();
    while (true) { // game loop
		readHumanInterfaceDevices();
 		if (quitButtonPressed())
        	break; // exit the game loop
            movePaddles();
            moveBall();
            collideAndBounceBall();
            if (ballImpactedSide(LEFT_PLAYER)) {
            	incremenentScore(RIGHT_PLAYER);
                resetBall();
			} else if (ballImpactedSide(RIGHT_PLAYER)) {
              incrementScore(LEFT_PLAYER);
              resetBall();
		}
        renderPlayfield();
     } 
}




input

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

public static void main() 
{
	while(running)
 	{
       processInput();
       updateGameState();
       output();
 	} 
}
  • First, process user input
    • Mouse/Keyboard/Gamepad
    • Camera-Based (Kinect)
    • Rotational / Translational Tracking
    • Touch-based
    • Speech-based
    • ...

update

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

public static void main() 
{
	while(running)
 	{
       processInput();
       updateGameState();
       output();
 	} 
}
  • Framerate problem: Object moves 2cm each update loop cycle
    • 30 FPS: 60cm moved in one second
    • 60 FPS: 120cm moved in one second
  • Solutions:
    • Fixed frame rate (e.g. 30 FPS)
    • Sleep to not run too fast
    • Time-dependent movement (variable or fluid time step)
    • Fixed time step
      • Variable time step is not deterministic cause of floating point numbers rounding errors
      • Important for physics, networking and AI
public static void main() 
{
	while(running)
 	{
      processInput();
      updateGameState();
      output();
 	  sleep(start + millisecondsPerFrame - getCurrentTime());
 	} 
 }
public static void main() 
{
 	double lastTime = getCurrentTime();
 	while (running)
 	{
       double current = getCurrentTime();
       double deltaTime = current - lastTime;
       processInput();
       updateGameState(deltaTime);
       output();
       lastTime = current;
	}
}
public static void main() 
{
   double previous = getCurrentTime();
   double lag = 0.0;
   while (true)
   {
     double current = getCurrentTime();
     double deltaTime = current - previous;
     previous = current;
     lag += deltaTime;
     
 	 processInput();
 
 	 while (lag >= millisecondsPerFrame)
   {
     updateGameState();
     lag -= millisecondsPerFrame;
   }
   
   output();
   } 
 }

quake ii main loop

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	MSG msg;
 	int time, oldtime, newtime;
 	char* cddir;
 	ParseCommandLine (lpCmdLine);
 	Qcommon_Init (argc, argv);
 	oldtime = Sys_Milliseconds ();
 	while (1) { // main window message loop
    	// Windows message pump.
		while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
        	if (!GetMessage (&msg, NULL, 0, 0)) Com_Quit ();
 			sys_msg_time = msg.time;
 			TranslateMessage (&msg);
 			DispatchMessage (&msg);
            	}
				// Measure real delta time in milliseconds.
 				do {
            newtime = Sys_Milliseconds ();
 			time = newtime - oldtime;
 		} while (time < 1);
        // Run a frame of the game.
        Qcommon_Frame (time);
        oldtime = newtime;
        43
 	}
 	return TRUE; // never gets here
}

void Qcommon_Frame (int msec) {
   char *s;
   int time_before, time_between, time_after;
   // [some details omitted...]
   // Handle fixed-time mode and time scaling.
   if (fixedtime->value) msec = fixedtime->value;
   else if (timescale->value) {
  	 msec *= timescale->value;
	 if (msec < 1) msec = 1;    
   }
   // Service the in-game console.
   do {
   		s = Sys_ConsoleInput ();
 		if (s) Cbuf_AddText (va("%s\n",s));
   } while (s);
   Cbuf_Execute ();
   // Run a server frame.
   SV_Frame (msec);
   // Run a client frame.
   CL_Frame (msec);
   // [some details omitted...]
}

output

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

public static void main() 
{
 while(running)
 {
   processInput();
   updateGameState();
   output();
 } 
}
public void output() 
{
   // processes other output
   otherOutput();
   // draws the updated state
   draw(); 
}
public void output() 
{
   // processes other output
   otherOutput();
   // draws the updated state
   draw();
   // change to back buffer
   swapBuffers();
}
  • When GameState updated, render GameView
    • draw on screen
    • play audio
    • tactile feedback (vibration)
    • ...
  • Rendering usually is done using a double buffer

Double Buffering

1. Draw

graphics

Image

Screen

Back Buffer

Primary Surface

2. Blt

(copy)

Image

Screen

Back Buffer

Primary Surface

lag

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

  • Time between user input and reaction on screen
  • Lag control is essential:
    • Lag distorts causality
    • Motion sickness
    • Tracking difficult
  • High variance of lag even worse
  • Reducing Lag?
    • Pick a goal frame rate
    • Faster algorithms, hardwar
    • Budget resources
    • Asynchronous update

Time

Event arrives

Lag

process input
update state
render
process input
update state
render
process input
update state
render

 }

{

{

{

Frame time

Frame time

Frame time

.
.
.

.
.
.

game loop in godot

PLAYER-COMPUTER INTERACTION

UNIT 12: GAME ENGINE ARCHITECTURE

func _ready():
		# Do something...
 	 pass

func _process(delta):
		# Do something...
 	 pass

func _physics_process(delta):
 		# Do something...
	 pass

Player-Computer Interaction 

The contents of this Open Educational Resource are licensed under the Creative-Commons Attribution 4.0 International license (CC BY 4.0)
Attribution: Eike Langbehn, Anh Sang Tran, Peter Wood

Unit 12 - V4

By sangow

Unit 12 - V4

  • 83