3 d game programming
Sponsored Links
This presentation is the property of its rightful owner.
1 / 447

3 D Game Programming PowerPoint PPT Presentation


  • 284 Views
  • Uploaded on
  • Presentation posted in: General

3 D Game Programming. 王銓彰 kevin.cwang@msa.hinet.net 2005. 課程大綱. Introduction to Game Development (3hr) Game System Analysis (3hr) The Game Main Loop (3hr) 3D Game Engine Training (Using TheFly3D) (6hr) Game Mathematics & Geometry (3hr) Terrain (3hr) Character Motion (3hr)

Download Presentation

3 D Game Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


3D Game Programming

王銓彰

kevin.cwang@msa.hinet.net

2005


課程大綱

  • Introduction to Game Development (3hr)

  • Game System Analysis (3hr)

  • The Game Main Loop (3hr)

  • 3D Game Engine Training (Using TheFly3D) (6hr)

  • Game Mathematics & Geometry (3hr)

  • Terrain (3hr)

  • Character Motion (3hr)

  • Game Control System (3hr)

  • Advanced Scene Management System (3hr)

  • Game AI (6hr)

  • Game Physics (3hr)

  • Game FX (3hr)

  • Network Gaming (3hr)

  • MMOG (3hr)

  • Summary

    • Wang’s Method for Real-time 3D Game Development


課程要求

  • One term project

    • The students are in teams.

    • Use TheFly3D 3D engine to code a Real-time 3D Game.

      • Action RPG

    • The teacher will provide graphics materials and setup the game design.

  • Final examination

  • Homework will be closely coupled with the term project.


王銓彰 (1/3)

  • 目前

    • 數位內容學院 專任講師 / 顧問

    • 宇峻奧汀 顧問

    • 鈊象電子 3D技術顧問

  • 學歷

    • 台灣大學土木工程學系畢業

  • 資歷

    • 04-04 資策會網路多媒體研究所 專案顧問

    • 97-04 昱泉國際股份有限公司 技術長

    • 96-96 虛擬實境電腦動畫股份有限公司 研發經理

    • 93-96 西基電腦動畫股份有限公司 研發經理

    • 90-93 國家高速電腦中心 助理研究員

    • 89-90 台灣大學土木工程學系 CAE Lab 研究助理


王銓彰 (2/3)

  • Game作品

    • Current

      • TheFly3D 3D Engine

    • 昱泉國際

      • DragonFly 3D Game Engine

        • M2神甲奇兵, VRLobby, 天劍記

      • Lizard 3D Game Engine

        • 幻影特攻、笑傲江湖 I & II、神鵰俠侶 I & II、風雲、小李飛刀、笑傲江湖網路版、怪獸總動員、聖劍大陸、笑傲外傳

    • 西基電腦動畫

      • Ultimate Fighter

        • 1st real-time 3D fighting game in Taiwan


王銓彰 (3/3)

  • 專長 (Expertise)

    • 3D Computer Graphics

    • Geometric Modeling

    • Numerical Methods

    • Character Animation

    • Photo-realistic Rendering

    • Real-time Shading

    • Volume Rendering

  • 應用領域 (Applications)

    • 即時3D遊戲開發 (Real-time 3D Game Development)

    • 電腦動畫 (Computer Animation)

    • 虛擬實境 (Virtual Reality)

    • 電腦輔助設計 (Computer-aided Design, CAD)

    • 科學視算 (Scientific Visualization)


1st

Introduction to

Game Development


Introduction to Game Development

  • Game platform

  • Game types

  • Game team

  • Game development pipeline

  • Game software system

  • Tools


Game Platform

  • PC

    • Single player

    • Match Makings

    • MMOG (Massive Multi-player Online Game)

    • Web-based Games

  • Console

    • Sony PS2

    • MS Xbox

    • Nintendo GameCube

  • Arcade

  • Mobile

    • Nintendo GBA

    • Nintendo DS

    • Sony PSP

    • Hand-held


Game Development on PC

  • PC is designed for general office application.

  • Not for entertainment purpose

  • A virtual memory system

    • Unlimited system memory

  • But video memory is limited.

    • For frame buffers, z buffers, textures, vertices, …

  • PCI / AGP might be a problem for performance.

  • Open architecture

    • Hardware driver version issue

    • Different capabilities

    • Different performance

  • Compatibility test is very important.

  • Development is easy to setup.

    • Visual C/C++ with DirectX


Game Development for Consoles

  • Specific hardware designed for games

  • Single user OS

  • Single process OS

  • No hard disk drive (?)

  • Closed system

  • Native coding environment

    • Proprietary SDK

    • Hardware related features

    • C language with assembly

  • Limited resources

    • Memory for everything

      • 32M for PS2

      • 64M for Xbox

  • One console runs, the others do !

  • Use gamepad and no keyboard


Game Types

  • RPG (Role playing games)

  • AVG (Adventure games)

  • RTS (Real-time strategy games)

  • FPS (First-person shooting games)

  • RSLG (戰棋)

  • STG

  • Sports

  • Action

  • Puzzle games

  • Table games

  • MMORPG

    • Massive Multiple Player Online Role Playing Games


Game Team Members

  • 開發團隊

    • 製作人

    • 執行製作人

    • 企劃團隊

    • 程式團隊

    • 美術團隊

  • 行銷業務團隊

    • 產品經理(PM)

  • 測試團隊

  • 遊戲審議委員會

    • Game project approval

  • 遊戲經營團隊

    • 線上遊戲 game master (GM)

    • Customer services

    • MIS


Game Producer 遊戲製作人

  • Team leader (usually)

  • 資源管理 (Resource management)

  • 行政管理 (Administration)

  • 專案管理 (Project management)

  • 向上負責 (Upward management)

  • 團隊的決策

  • 風險管理


遊戲執行製作人

  • 專案管理執行

  • Daily 運作

    • House keeping

    • Meeting coordinator

    • Schedule checking

    • Cross-domain communication

  • Usually not a full-time job position

    • A position for training and becoming a producer


遊戲企劃 (1/2)

  • 故事設計 (Story telling)

  • 腳本設計 (Scripting)

  • 玩法設計 (Game play design)

  • 角色設計(Character design)

  • 動作設計(Animation design)

  • 關卡設計 (Level design)

  • 特效設計(Effect design)

  • 物件設計

  • 介面設計(User Interface design)

  • 遊戲調適 (Game tuning)

  • 數值設定 (Numerical setup)

  • AI 設計 (Game AI design)

  • 音效設定 (Sound FX setup)


遊戲企劃 (2/2)

  • 場景設定 (Scene setup)

  • Game document writing

  • Game quality checking


遊戲美術

  • Visual setup for game design

    • 2D setup

    • 3D setup

  • Graphics design and production

    • 場景 (Terrain)

    • 人物 (Character)

    • 建模 (Models)

    • 材質 (Textures)

    • 動作 (Motion / Animation)

    • 特效 (FX)

    • User Interface

    • 行銷支援 (封面.海報..等)


遊戲程式

  • 遊戲程式 (Game Program) 撰寫

  • 遊戲開發工具 (Game Tools) 開發

    • Level editor

    • Scene editor

    • FX editor

    • Script editor

    • Game editor

  • 遊戲Data exporters from 3D animation Software

    • 3dsMax / Maya / Softimage

  • Game engine development

  • Game technique research

  • Online game server development


遊戲開發流程

Basic Procedures for Game Development

Idea

Proposal

Production

Integration

Testing

Debug

Tuning

  • 發想 (Idea)

  • 提案 (Proposal)

  • 製作 (Production)

  • 整合 (Integration)

  • 測試 (Testing)

    • 除錯 (Debug)

    • 調適 (Tuning)

Concept

Approval

Prototype

Pre-alpha

Alpha

Beta

Final

> Concept approval

> 雛形 (prototype)

> Pre-alpha

> Alpha

> Beta


遊戲發想(Concept Design)

  • 遊戲類型 (Game types)

  • 遊戲世界觀 (Game world)

  • 故事 (Story)

  • 遊戲特色 (Features)

  • 遊戲玩法 (Game play)

  • 遊戲定位 (Game product positioning)

    • Target player

    • Marketing segmentation / positioning

  • 競爭對手評估

  • 風險評估 (Risk)

    • SWOT (Strength/Weakness/Opportunity/Threat) 分析

  • 產出物

    • Concept Design Document (CDD)


遊戲提案(Proposal)

  • 系統分析 (System analysis)

  • GDD 撰寫 (Game design document)

  • MDD 撰寫 (Media design document)

  • TDD 撰寫 (Technical design document)

  • 遊戲專案建立 (Game project)

    • Schedule

    • Milestones / Check points

    • Risk management

  • 測試計畫書

  • 團隊建立 (Team building)

  • 產出物

    • GDD

    • MDD

    • TDD

    • The Team


遊戲開發(Production)

  • 美術量產製作

    • Modeling

    • Textures

    • Animation

    • Motion

    • FX

  • 程式開發 (Coding)

  • 企劃數值設定

量產 !


遊戲整合(Integration)

  • 關卡串聯 (Level integration)

  • 數值調整 (Number tuning)

  • 音效置入 (Audio)

  • 完成所有美術

  • 程式與美術結合

  • Testing within the game team

  • Focus group (User study)

    • Release some playable levels for focus group.

    • Get the feedback from focus group to adjust the game play.

    • Invited outside game players but evaluation in-house


遊戲測試(Test)

  • Alpha 測試

    • 除錯 (Debug)

    • Make the game stable

  • Beta 測試

    • 數值微調

    • Game play 微調

    • 對線上遊戲而言 (MMOG)

      • 封閉測試 (Closed beta)

        • Invited game players

      • 開放測試 (Open beta)

        • Free for public players

  • 極限測試 (Critical testing)

    • Only for MMOG

    • Continuously implementing

    • For servers


Bugs

Bug

Bug Classification

Bug Dispatch

?

Debug

N

Verify

Y

FAQ

  • Bug 分級 (Bug Classification)

    • A Bug

    • B Bug

    • C Bug

    • S Bug

  • Principles

    • Bug 分級從嚴

    • Tester vs Debugger


Game Software System

NPC System

Virtual Agent

Trading System

Story

Fighting System

FX System

Game AI

Script System

Terrain

Collision

Character

Dynamics

Sound FX

UI

3D Scene Mngmt

2D Sprite

Gamepad

Audio

Network

3D Graphics API

2D API

Input Device

OS API

Game

Game Play

Layer

Engine

Layer

System

Layer

Hardware


System Layer – APIs (1/2)

  • 3D Graphics API

    • DirectX 9.0 SDK – Direct3D

      • Newest update : DirectX 9.0c SDK Update (June, 2005)

    • OpenGL

      • 2.0

  • 2D API

    • DirectX 9.0 SDK - DirectMedia

    • Win32 GDI

  • Input device

    • DirectX 9.0 SDK – DirectInput

  • Audio

    • DirectX 9.0 SDK – DirectSound / Direct3DSound / DirectMedia

    • OpenAL


System Layer – APIs (2/2)

  • OS API

    • Win32 SDK

    • MFC

  • Network

    • DirectX 9.0 SDK – DirectPlay

    • Socket library


Engine Layer (1/2)

  • 3D scene management system

    • Scene graph

  • Shaders

  • 2D sprite system

  • Audio system

  • Gamepad

  • Hotkeys

  • Mouse

  • Timers

  • Network

  • DDK interface


Engine Layer (2/2)

  • Terrain system

  • Advanced scene management system

    • Space partition technique

      • BSP Tree

    • Octree

  • Character system

    • Bone-skin

    • Motion Blending

  • Dynamics

    • Particle system

    • Rigid-body dynamics

  • Collision detection

  • Sound FX

  • User interface


Game Play Layer

  • NPC (Non-playable characters) management

  • Game AI

    • Path finding

    • Finite state machine (FSM)

    • Steering behavior

  • Avatar

  • Combat system

  • FX system

  • Script system

  • Trading system

  • Number system


Game Development Tools for Programming (1/2)

  • System Tools

    • Visual C/C++

      • .Net 2003

        • VC/C++ 7.1

      • Visual C/C++ 6.0 + SP5

    • NuMega BoundsChecker

      • Finding memory leaking

    • Intel vTune

      • Finding computation performance bottlenecks

      • for CPU

    • PIX

      • Finding graphics performance bottlenecks

      • For GPU


Game Development Tools for Programming (2/2)

  • SDKs

    • System API

      • Win32 SDK or MFC

      • DirectX SDK or OpenGL

      • Socket library

    • Middleware (Game engine)

      • Renderware

      • Unreal

    • Physics

      • ODE


Game Development Tools for Artists

  • 3D tools

    • Discrete 3dsMax

    • Maya

    • Softimage XSI

  • 2D tools

    • Photoshop

    • Illustrator

  • Motion tools

    • Motion capture devices

    • Motion Builder

    • FiLMBOX


2nd

Game System Analysis


What Will We Talk

  • The idea about system analysis (SA)

  • Mind mapping

  • Case study - Term project


Why System Analysis (1/2)

Job A

Job B

Job D

Job C

  • For 程式結構 analysis

    • The main program

    • Game development tools

  • To identify 工作量

    • New game engine ?

    • Re-used code ?

    • Tools needed to be developed ?

  • For 資源 management

    • Total man month

    • How many programmers ?

    • How good the programmers ?

  • For job dependency analysis


Why System Analysis (2/2)

  • To do technical possibility analysis

    • 技術可行性分析

    • R&D ?

    • Where is the technical bottleneck ?

  • Pre-processor for

    • Technical design document

    • Project management

  • Bridge from game design to programming


Something about System Analysis

  • No standard procedures or approaches

  • It’s not a theory.

    • Experience

  • You can use your own method/tool

    • UML

    • Mind mapping

      • 心智圖法

      • This is the one we will use in this course


Wang’s System Analysis Steps

  • Brainstorming

  • Integration

  • Dependency analysis

  • Create the project

  • Technical design document (TDD) writing


Brainstorming

  • Based on the game design to put everything as many as you could

  • Use mind mapping

  • Including

    • Game system

      • Combat / Village / Puzzle / …

    • Program modulus

      • Camera / PC control / NPC AI / UI / FX /…

    • Tools

      • Level editor / Scene editor / …

    • Entities in games

      • Characters / vehicle / terrain / audio / …


Integration

  • Confirm the resource limitation

  • Technical implement possibility

  • Put all related items together

  • Man month analysis

    • How many ?

    • Who ?

  • Jobs / System identification


Dependency Analysis

  • Sort the Jobs

    • By job dependency

    • By programmers’ schedule

  • Prototype for scheduling


System Analysis – Create the Project

  • Scheduling

  • Job assignment

  • Resource allocation

  • Check points

  • Milestones

    • Major check points

    • Output

    • Risk management

      • Alternatives

  • Risk management policy


Technical Design Document

  • Specification

  • Resources

  • Design in details

  • Implement methods (工法)

  • Algorithms

  • “Project”

  • Output in each milestone

  • SOP (optional)

  • TDD Template


Mind Map

  • 心智圖法

  • A radiant thinking tool

  • Applications

    • 讀書心得

    • Proposal

    • 上課筆記

    • 遊記

    • System Analysis

  • Reference

    • Programs

      • Visio

      • MindManager

    • Books

      • Tony Buzan, Barry Buzan

      • “The Mind Map Book: How to Use Radiant Thinking to Maximize Your Brain's Untapped Potential”


Mind Map Demo Using MindManager

  • Use MindManager X5 pro

  • Developed by MindJet


3rd

The Game Main Loop


Win32 Application (1/3)

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)

{

WNDCLASSEX wc;

...

// register window class

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

wc.lpfnWndProc = KKMainProc;

...

RegisterClassEx(&wc);

...

// the main loop

KKMainLoop();

// unregister the window class

UnregisterClass(wc.lpszClassName, hInst);

return id;

}


Win32 Application (2/3)

LRESULT CALLBACK KKMainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

LRESULT l = FALSE;

...

// switch for all incoming messages from WindowsXX

switch (uMsg) {

case WM_KEYDOWN:

...

l = TRUE;

break;

...

}

// echo the result

if (l) {

return l;

}

else {

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

}


Win32 Application (3/3)

void KKMainLoop()

{

MSG msg;

BOOL kkBeQuit = FALSE;

// the main loop

while (!kkBeQuit) {

// check window's messages

while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

if (msg.message == WM_QUIT) {

kkBeQuit = TRUE;

}

// invoke the WindowsX to handle the incoming messages

TranslateMessage(&msg);

DispatchMessage(&msg);

}

// do your jobs here, for example, check the timing and do something in regular

...

}

}


Event-driven Programming

  • Win32 programs are event-driven

    • Messages = events

    • So as all windows system (for example : X window)

  • We need an infinitive loop to check all incoming events.

  • In the loop :

    • Check if there are incoming events (messages)

    • Handle the events

    • Check the time and do something in regular

  • Incoming events :

    • Interrupts

    • System requests


Timers & Events (1/2)

  • Timers (do something in regular timing)

    • The sub-system to handle timing

    • Must be precise to at least 1 ms or less

      • 30fps = 1/30 second = 33.333… ms

    • On win32 platform, you can use “performance counter” instead of the win32’s “WM_TIMER” message

      • For windows9x, WM_TIMER = 18.2 fps (maximum)

  • Events

    • Input devices

      • Mouse

      • Keyboard

    • Something coming from network

    • System requests

      • Re-draw

      • Losing/getting the input focus


Timers & Events (2/2)

  • Two types of jobs (Callbacks) to do (Call) :

    • In regular

      • Timers callbacks

    • By requests

      • Input device callbacks

  • So as the game main program

    • A game is an interactive application.

      • Mouse

      • Hotkeys

      • Gamepads

    • A game is time-bound.

      • Rendering locked in 30fps or 60fps

      • Motion data produced in 30fps

      • Game running in 30fps


Implement the Timer (1/6)

  • On PC platform : use “Performance Counter”

    • QueryPerformanceFrequency()

    • QueryPerformanceCounter()

// timers data structure

typedef struct {

BOOL beAble; // is the timer is enabled/disabled ?

BOOL be1st; // is this the 1st time for the timer to be checked

// after last initialization ?

BOOL beLockFps; // is locked on FPS ?

double initTime; // initial time

double timeInv; // system ticks for one frame

double nxtTime; // next checking time

void (*timer)(int); // timer's callback

double resetTime; // reset time

} TIMERs, *TIMERptr;


Implement the Timer (2/6)

/*----------------------------------------------------------------------

initialize a timer and bind a user-defined timer callback

------------------------------------------------------------------------*/

void FyBindTimer(DWORD id, float fps, void (*fun)(int), BOOL beLock)

{

if (id < 0 || id >= MAXTIMERS) return;

/* assign the timer's callback */

fyTimer[id].timer = fun;

/* set lock-to-fps flag */

fyTimer[id].beLockFps = beLock;

/* calculate the ticks for one frame */

fyTimer[id].timeInv = (double) (fyFreq) / (double) fps;

fyTimer[id].be1st = TRUE;

fyTimer[id].beAble = TRUE;

}


Implement the Timer (3/6)

/*------------------------------------

get current system clock tick

--------------------------------------*/

double FYGetCurrentSystemTick()

{

LARGE_INTEGER timeCount;

/* get current tick */

QueryPerformanceCounter(&timeCount);

return (double) timeCount.QuadPart;

}

/*

// get the system ticks for one second

QueryPerformanceFrequency(&timeFreq);

fyFreq = timeFreq.LowPart;

*/


Implement the Timer (4/6)

/*------------------------------------------------------------

invoke the TheFly3D system to handle the timers

--------------------------------------------------------------*/

void FyInvokeTheFly(BOOL beTimer)

{

MSG msg;

if (fyBeQuit) return;

while (!fyBeQuit) {

// check window's messages

while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

if (msg.message == WM_QUIT) fyBeQuit = TRUE;

TranslateMessage(&msg);

DispatchMessage(&msg);

}

// check the timer

if (beTimer && fyBeTimer) FYInvokeTimer();

}

}


Implement the Timer (5/6)

/*---------------------

check all timers

----------------------*/

void FYInvokeTimer()

{

int i, skipS;

double dTime;

// get current time

dTime = FYGetCurrentSystemTick();

for (i = 0; i < MAXTIMERS; i++) {

if (fyTimer[i].beAble && fyTimer[i].timer != NULL) {

// for the first time .....

if (fyTimer[i].be1st) {

// initialize the timer

fyTimer[i].be1st = FALSE;

fyTimer[i].initTime = dTime;

fyTimer[i].nxtTime = dTime + fyTimer[i].timeInv;

(*(fyTimer[i].timer))(1);

}


Implement the Timer (6/6)

else {

if (fyTimer[i].beLockFps) {

if (dTime >= fyTimer[i].nxtTime) {

// calculate skip frames

skipS = (int)((dTime - fyTimer[i].nxtTime) / (double)fyTimer[i].timeInv) + 1;

// get next checking time

fyTimer[i].nxtTime += (double) (skipS * fyTimer[i].timeInv);

// check some abnormal conditions

...

// invoke the timer callback

(*(fyTimer[i].timer))(skipS);

}

}

else {

(*(fyTimer[i].timer))(1);

}

}

}

}

}


Game Loop (1/2)

Loop

y

Check game over

Exit the loop

n

Peek player input

Implement

timer callback

Rendering

  • Single player


Game Loop (2/2)

Loop

  • Network client

y

Check game over

Exit

n

Peek user input

Receive messages

From network

Timer callbacks

To network

Send messages

Rendering


Jobs in Regular (In Timers)

  • Check “Win/Lose”

  • Check “Quit”

  • Make objects moving …

  • Play character’s motion to the next frame

  • Play animation to the next frame

    • Models

    • Textures

  • Perform game logic calculation

  • Perform geometry associated calculation

    • i.e. LOD

  • Perform AI “Thinking”

  • Perform collision detection

  • Perform the 3D rendering

  • Play FX


Jobs By Request

  • Mouse Input

    • Press/release the mouse button

    • Drag

    • Double-click

    • Move

  • Keyboard Input

    • Hotkey

    • Typing

  • Gamepad

    • Same behavior as the hotkey

    • Except looking not like the keyboard

  • Network

  • System


4th

TheFly3D Game Engine


The Main Program

void main(int argc, char **argv)

{

// create the game world & 3D scene

...

// set Hotkeys

FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE);

...

// define some mouse functions

FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, EndPivot, NULL);

...

// bind a timer for rendering, frame rate = 60 fps

FyBindTimer(0, 60.0f, RenderIt, TRUE);

// bind a timer for game AI, frame rate = 30 fps

FyBindTimer(1, 30.0f, GameAI, TRUE);

// invoke the system

FyInvokeTheFly(TRUE);

}


Hotkey Callback

//-------------------

// quit the game

//-------------------

void QuitGame(WORLDid gID, BYTE code, BOOL value)

{

if (code == FY_ESCAPE) {

if (value) {

FyWin32EndWorld(gID);

}

}

}


Mouse Callback

/*------------------

pivot the camera

-------------------*/

void PivotCam(WORLDid g, int x, int y)

{

FnModel model;

if (x != oldX) {

model.Object(cID);

model.Rotate(Z_AXIS, (float) (x - oldX), GLOBAL);

oldX = x;

}

if (y != oldY) {

model.Object(cID);

model.Rotate(X_AXIS, (float) (y - oldY), GLOBAL);

oldY = y;

}

}

/*-----------------------------------------

initialize the pivot of the camera

------------------------------------------*/

void InitPivot(WORLDid g, int x, int y)

{

oldX = x;

oldY = y;

}


The Timer Callback

//----------------------------------------------------------------------------------------

// Render callback which will be invoked by TheFly3D every 1/60 second

//----------------------------------------------------------------------------------------

void RenderIt(int skip)

{

FnViewport vp;

FnWorld gw;

// render the scene

vp.Object(vID);

vp.Render(cID, TRUE, TRUE);

// perform double-buffering

gw.Object(gID);

gw.SwapBuffers();

}


Introduction to TheFly3D

  • A real-time 3D graphics programming library

    • Using C++

    • Cross-platform

      • DirectX9.0c

      • OpenGL 1.5

  • An API for 3D graphics developers

  • Provide a fundamental scene management system

    • Scene tree

    • Built-in visibility culling

  • According to the experiences from the author, some game development features are added.

    • Characters

    • Terrain system

  • Current version 0.8a1 (0920, 2005)

    • Shader has been added in D3D version


NPC System

Virtual Agent

Trading System

Story

Combat System

FX System

Game AI

Script System

TheFly3D in A Chart

Game

Game Play

Layer

Terrain

Collision

Character

Dynamics

Sound FX

UI

Engine

Layer

3D Scene Mngmt

2D Sprite

Gamepad

Audio

Network

3D Graphics API

2D API

Input Device

OS API

System

Layer

Hardware

Developing

Developed


Development Environment

  • .NET2003 Visual C++ 7.1

  • DirectX9.0c SDK (Dec, 2004)

  • Two include files : (must!)

    • TheFly.h

    • TheFlyWin32.h (Win32 version + D3D)

  • Linked libraries (API version)

    • TheFlyLibD_08_01.lib

    • d3d9.lib d3dx9.lib dsound.lib dxguid.lib winmm.lib


Create the Visual C++ Project for “TheFly3D”

  • Create folders for TheFly3D API

    • …\include

    • …\lib

  • New a Win32 application project

  • Set the additional include/library directories to TheFly3D API

  • Add DirectX 9.0 SDK’s include/lib to additional search directories

  • Add d3d9.lib d3dx9.lib dsound.lib dxguid.lib winmm.lib to additional dependencies

  • Add TheFly.h, TheFlyWin32.h, TheFly3DLibD_xxxx.lib into the project


The 1st TheFly3D Program – hello.cpp

  • Create a 3D world

  • Create a viewport

  • Create a scene

  • Create 3D entities

    • A camera

    • A teapot model

    • A light source

  • Translate the camera to show the model

  • Bind callbacks to make the program interactive


Demo - Hello

Do it!


The Basics to Write TheFly3D Program

  • All Win32 code is transparent.

    • void main(int argc, char *argv[])

    • All Win32 & DirectX code are hidden within the engine.

  • ID & Function class

    • TheFly3D creates the objects for you.

      • Return the object ID

    • TheFly3D owns the objects.

    • You have the right to handle the objects.

      • Use function classes

    • No internal data structure & functions revealed

// create a viewport

vID = gw.CreateViewport(ox, oy, ww, hh);

FnViewport vp;

vp.Object(vID);

vp.SetBackgroundColor(0.3f, 0.3f, 0.0f);


Initialize TheFly3D

  • In general the 1st function to use TheFly3D is :

    • FyWin32CreateWorld()

  • After the calling successfully, you can get the non-zero ID (WORLDid) of a world object.

  • Assign the ID to a world function class object for manipulating the world.

// create a new world

WORLDid gID = FyWin32CreateWorld(“Tester", 0, 0, 800, 600, 16, FALSE);

FnWorld gw;

gw.Object(gID);

gw.SetTexturePath("Data\\textures");


A World

3D Graphics Layers

Frame Buffers

(front + back)

The World in TheFly3D

  • A world is a set of graphics layers where the 3D objects acts on.


Backdrop

Lights

Camera

3D Models

Board

A 3D Scene

The 3D Graphics Layer

  • A 3D graphics layer is a projection of the rendering of a 3D view.

  • The set of the 3D objects in the view :

    • We call it the “Scene”.

  • The projection we call the “Viewport”


The Viewports & Scenes

  • TheFly3D supports the multiple viewports.

  • A scene can be rendered on different viewports.

  • Viewports & scenes are created by the world object.

// create a new world

WORLDid gID;

gID = FyWin32CreateWorld(Tester", 0, 0, 800, 600, 16, FALSE);

FnWorld world;

world.Object(gID);

SCENEid sID = world.CreateScene();

VIEWPORTid vID = world.CreateViewport(0, 0, 400, 300);

world.DeleteScene(sID);

world.DeleteViewport(vID);


The Scene

  • A scene is not a “real” 3D object, just a “set” of 3D objects.

  • A scene provides multiple rendering groups to handle the priority sorting for the rendering of 3D objects.

    • There are three object lists within a rendering group.

      • Invisible list

      • Opacity list

      • Semi-transparent list

    • The objects are rendered by the order of rendering groups.

    • In the same rendering group, the opaque objects are rendered first.

    • And the semi-transparent objects are sorted and rendered from far to near…


The 3D Entities – Objects

  • A 3D scene is constructed by a set of “objects” which are the basic entities in a scene.

  • An object is a carrier to carry real 3D data including :

    • Model geometry

    • Camera data

    • Lighting data

    • Terrain data

    • Particle emitters

    • Forces

    • Audio

  • Objects are created/deleted by its host scene.

  • Objects can be switched between scenes.

  • Objects should be assigned to a rendering group.


The Objects Can …

  • Can have shapes (geometric data)

  • Can be grouped (hierarchy)

  • Can move (transformation)

  • Can look alike (clone or data sharing)

  • Can perform (animation or deformation)

  • Can be affected (lighted, listened)

  • Can be changed dynamically (modification)


A Scene Object

Hierarchy

Parent Object

Parameters

Move

Transformation

Etc

Animation

Motion Data

Shape

Geometric Data

Clone


A Model Object

  • An object to carry a set of geometry data is a model object

  • You can load the model data from files.

  • TheFly3D loads .cw3 model files in default.

// create 3D entities

nID = scene.CreateObject(ROOT);

FnObject model;

model.Object(nID);

// load a teapot

model.Load("Teapot.cw3");


TheFly3D Scene Tree

  • A tree-based representation

  • Simplified scene graph

Root


TheFly3D Scene Tree Is Simplified Scene Graph

  • A tree-based representation

  • Simplified scene graph

Root


Object Hierarchy

nID = scene.CreateObject(ROOT);

cID = scene.CreateCamera(ROOT);

FnObject model;

model.Object(nID);

model.SetParent(cID);

cID

nID


Clone a Model Object

OBJECTid nID = scene.CreateObject(ROOT);

FnObject model;

model.Object(nID);

OBJECTid nID1 = model.Instance();

nID

nID1

Data

instance


TheFly3D Major Model Object Functions (1/2)

  • void model.SetParent(parent_object_ID);

  • void model.Show(be_show);

  • void model.SetOpacity(opacity);

    • opacity = 0.0 – 1.0

  • void model.SetRenderMode(mode);

    • mode = WIREFRAME or TEXTURE

  • OBJECTid clonedID = model.Instance();

  • void model.ChangeScene(sID);

  • BOOL model.Load(char *file);

    • Load a .cw3 model file to a model object

  • void model.ShowBoundingBox(beShow);

  • void model.Translate(x, y, z, op);


TheFly3D Major Model Object Functions (2/2)

  • model.SetRenderOption(item, value);

    • (item, value) =

      • (Z_BUFFER, TRUE/FALSE)

      • (Z_BUFFER_WRITE, TRUE/FALSE)

      • (ALPHA, TRUE/FALSE)

        • Add/remove the model to/from alpha sorting list

      • (FOG, TRUE/FALSE)

      • (SPECULAR, TRUE/FALSE)

      • (LIGHTING, TRUE/FALSE)

      • (ANTIALIASING, TRUE, FALSE)

      • (SOURCE_BLEND_MODE BLEND_ZERO / BLEND_ONE / BLEND_SRC_COLOR / BLEND_INV_SRC_COLOR / BLEND_SRC_ALPHA / BLEND_INV_SRC_ALPHA / BLEND_DEST_ALPHA / BLEND_INV_DEST_ALPHA / BLEND_DEST_COLOR / BLEND_INV_DEST_COLOR / BLEND_SRC_ALPHA_SAT / BLEND_BOTH_SRC_ALPHA / BLEND_BOTH_INV_SRC_ALPHA)

      • (DESTINATION_BLEND_MODE values are same as the SOURCE_BLEND_MODE)


Coordinate System

Z

Y

X

z

y

x

  • Every model should have its own local coordinate system.

    • Local space

    • Model space

      • The space when it’s modeled

  • To its parent model, it is in the global space.

    • Global space

      • The space for reference

  • World space

    • The global space of the “Root”


Transformation

Rotation matrix

a0 a1 a2 0

a3 a4 a5 0

a6 a7 a8 0

a9 a10 a11 1

Translation vector

  • Three basic linear transformations used in TheFly3D.

    • Translate

    • Rotate

    • Scale

  • Principles :

    • Right-handed rule

    • v’ = v M0M1

    • Matrix in 12-element (I call the M12)


Translation

model.Translate(dx, dy, dz, op);

x’ = x + dx

y’ = y + dy

z’ = z + dz

(dx dy dz)

1 0 0

0 1 0

0 0 1

dx dy dz

T =


Rotation

i.e. rotate with z axis

model.Rotate(Z_AXIS, 30.0f, op);

z

x’ = x cosq – y sinq

y’ = x sinq + y cosq

z’ = z

y

cosq sinq 0

-sinq cosq 0

0 0 1

0 0 0

x

Rz=


Scaling

model.Scale(sx, sy, sz, op);

x’ = x * sx

y’ = y * sy

z’ = z * sz

sx 0 0

0 sy 0

0 0 sz

0 0 0

T =


Matrix Operations

Z

  • Matrix operation

    • REPLACE, LOCAL, GLOBAL

Y

X

op =LOCAL

z

op =REPLACE

x

y

[ML]

[M]

[MG]

op =GLOBAL

Object Transformation Matrix


TheFly3D Model Transformation Functions

  • model.Translate(x, y, z, op);

    • op = LOCAL, GLOBAL, or REPLACE

  • model.Rotate(axis, angle, op);

    • op = LOCAL, GLOBAL, or REPLACE

    • axis = X_AXIS, Y_AXIS, Z_AXIS

  • model.Scale(sx, sy, sz, op);

    • op = LOCAL, GLOBAL, or REPLACE

  • model.Quaternion(w, x, y, z, op);

    • w : the scalar part of the quaternion

    • x, y, z : the vector part of the quaternion

    • The quaternion should be a unit quaternion

    • op = LOCAL, GLOBAL, or REPLACE

  • model.SetMatrix(M12, op);

    • M12 : an M12 matrix

    • op = LOCAL, GLOBAL, or REPLACE

  • float *model.GetMatrix();

    • Get the pointer of the model object’s matrix


Transformations vs Movements

Turn right / left

Move up

Move right

Move forward

  • Transformation is the term used in computer graphics but not friendly for games.

  • We use movements to control the 3D objects moving around in the scene.

    • Move forward

    • Move right

    • Move up

    • Turn right

    • Turn left


Facing Direction and Up Direction

z

y

x

z up + facing to -y

  • Each object is modeled with a facing direction and up direction “visually”

  • In TheFly3D, we use –y axis as the default facing direction for a model, z axis as the default up direction

  • But for a camera :

    • -z axis is the facing direction

    • y axis is the up direction


Move Forward (1/2)

new position = old position +

distance *(facing direction in unit)


Move Forward (2/2)

  • The object has a local coordinate system.

  • Align a local axis of the object with the facing direction

  • Make a translation to move the object align the local axis

  • Apply the matrix first before to apply the existing transformations (op = LOCAL)

  • Then the object is moving forward!

FnObject model;

model.Object(nID);

model.Translate(0.0f, -dist, 0.0f, LOCAL); // facing to -y


Turn Right/Left (1/2)

  • An example : (rotate with an arbitrary axis)

-1 -1 -1

M = T1 * R1 * R2 * Rx(angle) * R2 * R1 * T1

T1 = / 1 0 0 0 R1 = / cs2 -sn2 0 0

0 1 0 0 -z sn2 cs2 0 0

0 0 1 0 0 0 1 0

-x -y -z 1 / 0 0 0 1 /

R2 = / cs1 0 -sn1 0 Rx = / 1 0 0 0

y 0 1 0 0 0 cs sn 0

sn1 0 cs1 0 0 -sn cs 0

0 0 0 1 / 0 0 0 1 /


Turn Right/Left (2/2)

  • The object has a local coordinate system

  • Align a local axis of the object with the up direction

  • Make a rotation matrix to turn the object along the local axis

  • Apply the matrix first before to apply the existing transformations

  • Then the object is turning !

FnObject model;

model.Object(nID);

model.Rotate(Z_AXIS, -angle, LOCAL); // turn right


Terrain

  • A terrain is a place for 3D objects to walk on

  • A terrain can be generated from a model file

  • Neighboring triangles are the next searching target for current triangle for terrain following

  • A terrain for terrain following is not the same as the terrain in visual


Generate Terrain in TheFly3D

// create a terrain object

tID = scene.CreateTerrain();

FnTerrain t;

t.Object(tID);

// load a terrain model (just like a regular object)

// but a terrain is invisible in default

t.Load("test_terrain_following.cw3");

// generate the neighboring data for terrain following

// otherwise, the terrain model is for visual only

t.GenerateTerrainData();


Put a Model on Terrain Using TheFly3D

// if tID is the terrain object (OBJECTid)

// load a model

OBJECTid nID = scene.CreateObject(ROOT);

FnObject obj;

obj.Object(nID);

// put the model on terrain

float pos[3];

pos[0] = x;

pos[1] = y;

pos[2] = 10000.0f; // should be higher than the terrain!

obj.SetPosition(pos);

obj.PutOnTerrain(tID, be3D, offset, rangeF, rangeB, angle);


Terrain Following

Terrain Following (3D)

offset

Terrain Following (2D)

offset


Probe for a Model on Terrain

probeBack

probeFront

: terrain following check point


TheFly3D Model Movement Functions (1/3)

  • void model.GetPosition(pos)

    • pos is a 3D vector to get the position of the model object

  • void model.GetDirection(faceDir, upDir)

    • If you just want to get one of the directions, just send NULL pointer to the one that you do not want to query

  • void model.SetPosition(pos)

    • The position is related to its parent object

  • void model.SetDirection(faceDIr, upDir)

    • If you just want to set one of the directions, just send NULL pointer to the one that you do not want to set

  • Void model.SetDirectionAlignment(fDAxis, uDAxis)

    • You can change the local axes for facing and up directions


TheFly3D Model Movement Functions (2/3)

  • BOOL model.PutOnTerrain(tID, be3D, offset, probeFront, probeBack, probeAngle, hightLimit)

    • tID is a terrain object

    • be3D = TRUE for 3D terrain following

    • Offset is the height above the terrain

    • hightLimit is the terrain following height limit

    • Return TURE if you successfully put the model on a terrain

  • int model.MoveForward(dist, beTerrainFollow, be3D, offset)

    • If you just want to move the model forward but not on terrain, set beTerrainFollow to FALSE

    • You should put a model on terrain first. Then you can move it forward on terrain.

    • Return value :

      • WALK (ok for moving)

      • BOUNDARY (hit the terrain boundary)

      • BLOCK (not implemented yet, for collision)

      • DO_NOTHING (something wrong …)


TheFly3D Model Movement Functions (3/3)

  • int MoveRight(dist, beTerrainFollow, be3D, offset)

    • Same as the MoveForward except moving in right-hand direction

  • void model.TurnRight(float angle)

    • Angle is in degree format

  • int model.GetCurrentTerrainTriangle()

    • Get current triangle ID in terrain, where the model is standing


A Character


Character with motion


Introduction to Characters

Base

  • The characters are the actors of the games.

  • Three types of characters implemented in games :

    • Segmented

    • Mesh

    • Bone-skin

  • Root-base concept

  • Production :

    • 3D animation tools

    • Motion capture (MoCap)

      • For motion data


A Segmented Character

  • A character is composed by a set of models with motion data to simulate a live creature in real world

  • Benefits

    • Hierarchical structure

    • Easy to implement in a scene tree

  • Drawbacks

    • Segment-like


  • The scene tree of a segmented character :

head

up arm

hand

body

groin

fore arm

Base

thigh

groin

foot

shin

body

thigh_r

thigh_l

head

up_arm_r

shin_r

up_arm_l

shin_l

fore_arm_l

fore_arm_r

foot_r

hand_l

foot_l

hand_r


A Mesh Character

  • Vertex animation on skins

    • Animated positional data on skins

    • 3D warping

  • Benefits

    • Easy to implement

    • Flexible mesh in animation

  • Drawbacks

    • No hierarchy

    • Each frame is independent.

    • Massive dataset


A Bone-skin Character

Bone A

Skin

Bone B

  • Bone-skin skeleton

    • Hierarchical bones

    • Skin deformationrun-timely

  • Benefits

    • Hierarchical structure

    • Not segmented look

  • Drawbacks

    • More complicated than the other solutions

    • Skin deformation need more CPU cost than transformation only


Motion Production – by 3D Animation Tools (1/2)

  • Keyframesystem

    • 3ds MAX

    • Softimage

    • Maya


Motion Production – by 3D Animation Tools (2/2)

  • Low cost (relatively)

  • Easy to combine animations

  • Hand-made animations

  • Hard to make “good” motions

  • Long production time


Motion Production – by Motion Capture (1/2)

  • Motion Capture

    • “MoCap” in short

  • Types :

    • Optical

    • Magnetic

    • ...


Motion Production – by Motion Capture (2/2)

  • Expensive solution

    • Hardware and software

  • Every frame is a keyframe

  • Very live motion

    • Noise !

  • Need post-processing for games

    • Patching the data

    • Re-keyframing

    • Cleaning noise

    • Re-targeting

  • Hard to combine motions


The Root-Base Concept (1/2)

  • Use root-base structure to construct the character

Base

Root

(groin)

Base


The Root-Base Concept (2/2)

  • A character has some models to be the geometry roots of the character system.

  • The root plays as the gravity center of the character.

  • The root can be translated and rotated.

  • The others are joints.

  • The joints can rotate only.

  • A ghost object is added to be the parent of the root, which is the base of the character.

  • The base is the center for the character’s movement.

  • We move the base to perform character’s moves.


Motion Data - Pose

walk

run

attack

fall

  • A set of frames to describe a character’s motion

  • For examples : Walk, run, attack, …

  • Keyframed or non-keyframed

  • Motion data in

    • Position (pivot) + quaternion

    • Position (pivot) + Euler angles

    • Position (pivot) + (q, n)

    • Matrix


Load a Character

FnScene scene;

CHARACTERid actorID;

scene.Object(sceneID);

actorID = scene.LoadCharacter(“fm.cwc");

.cwc is a character

description file.


Play a Pose

BLENDTREEid btID = actor.GetBlendTree();

FnBlendTree bt;

bt.Object(btID);

// the 2nd motion definition

BLENDNODEid aaaID;

aaaID = bt.CreateAnimationNode(2);

// start to play a pose (1st time)

actor.PlayBlendNode(aaaID, (float) 0, START, TRUE);

// continue to play a pose

actor.PlayBlendNode(CURRENT, (float) skip, LOOP, TRUE);


Make a Character to Move Forward

FnCharacter actor;

// play walking pose

actor.Object(actorID);

actor.PlayBlendNode(CURRENT, (float) skip, LOOP, TRUE);

// move it forward

actor.MoveForward(dist, beTF, be3D, offset);


TheFly3D Character Movement Functions (1/2)

  • void actor.GetPosition(pos)

    • pos is a 3D vector to get the position of the character

  • void actor.GetDirection(faceDir, upDir)

    • If you just want to get one of the directions, just send NULL pointer to the one that you do not want to query

  • void actor.SetPosition(pos)

    • The position is related to its parent object

  • void actor.SetDirection(faceDIr, upDir)

    • If you just want to set one of the directions, just send NULL pointer to the one that you do not want to set

  • BOOL actor.PutOnTerrain(tID, be3D, offset, probeFront, probeBack, probeAngle, hightLimit)

    • tID is a terrain object

    • be3D = TRUE for 3D terrain following

    • Offset is the height above the terrain

    • hightLimit is the terrain following height limit

    • Return TURE if you successfully put the character on a terrain


TheFly3D Character Movement Functions (2/2)

  • void actor.MoveForward(dist, beTF, be3D, offset)

    • If you just want to move the character forward but not on terrain, set beTF to FALSE

    • You should put a character on terrain first. Then you can move it forward on terrain.

    • A character is always using his local -y-axis as facing direction

  • void actor.TurnRight(float angle)

    • Angle is in degree

    • A character is always using his local z-axis as up direction


TheFly3D Character Functions

  • OBJECTid actor.GetBaseObject()

    • You can get the base object of the character

  • OBJECTidactor.GetObjectByName(name)

    • You can get the each model part of the character by its name in character file

    • For a bone-skin character, this function can get the bones in the skeleton

  • BOOL actor.PlayFrame(frame, beIncludeBase)

    • The function is to play the motion for a specific frame

    • Set beIncludeBase = TRUE to play the base object’s motion

  • BLENDTREEid actor.GetBlendTree()

    • Get the character’s blend tree data

    • You can define all poses in a blend tree (blend nodes)

  • float actor.PlayBlendNode(btNodeID, skipFrame, mode, beIncludeBase)

    • All poses for a character is defined as a blend node

    • Skip frames can be floating-point

    • Mode can be ONCE or LOOP!


5th

Game Mathematics


Essential Mathematics for Game Development

  • Matrices

  • Vectors

  • Fixed-point Real Numbers

  • Triangle Mathematics

  • Intersection Issues

  • Euler Angles

  • Angular Displacement

  • Quaternion

  • Differential Equation Basics


Matrices (1/7)

a11 .. a1n

. .

. .

am1 .. amn

  • Matrix basics

    • Definition

    • Transpose

    • Addition

A = (aij) =

C = A T cij = aji

C = A + B cij = aij + bij


Matrices (2/7)

  • Scalar-matrix multiplication

  • Matrix-matrix multiplication

C = aA cij = aaij

r

C = A B cij = Saikbkj

k = 1


Matrices (3/7)

  • Transformations in Matrix form

    • A point or a vector is a row matrix (de facto convention)

V = [x y z]

  • Using matrix notation, a point V is transformed under translation, scaling and rotation as :

V’ = V + D

V’ = VS

V’ = VR

where D is a translation vector and

S and R are scaling and rotation matrices


Matrices (4/7)

  • To make translation be a linear transformation, we introduce the homogeneous coordinate system

V (x, y, z, w)

, where w is always 1

  • Translation Transformation

x’ = x + Tx

y’ = y + Ty

z’ = z + Tz

V’ = VT

1 0 0 0

0 1 0 0

0 0 1 0

Tx Ty Tz 1

[x’ y’ z’ 1] = [x y z 1]

= [x y z 1] T


Matrices (5/7)

  • Scaling Transformation

x’ = xSx

y’ = ySy

z’ = zSz

V’ = VS

Sx 0 0 0

0 Sy 0 0

0 0 Sz 0

0 0 0 1

[x’ y’ z’ 1] = [x y z 1]

= [x y z 1] S

Here Sx, Sy and Sz are scaling factors.


Matrices (6/7)

  • Rotation Transformations

1 0 0 0

0 cosq sinq 0

0 -sinq cosq 0

0 0 0 1

Rx =

Ry =

Rz =

cosq 0 -sinq 0

0 1 0 0

sinq 0 cosq 0

0 0 0 1

cosq sinq 0 0

-sinq cosq 0 0

0 0 1 0

0 0 0 1


Matrices (7/7)

  • Net Transformation matrix

[x’ y’ z’ 1] = [x y z 1] M1

and

[x” y” z” 1] = [x’ y’ z’ 1] M2

then the transformation matrices can be concatenated

M3 = M1 M2

and

[x” y” z” 1] = [x y z 1] M3

  • Matrix multiplication are not commutative

M1 M2 = M2 M1


Vectors (1/5)

  • A vector is an entity that possesses magnitude and direction.

  • A 3D vector is a triple :

    • V = (v1, v2, v3), where each component vi is a scalar.

  • A ray (directed line segment), that possesses position, magnitude and direction.

V = (x2-x1, y2-y1, z2-z1)

(x1,y1,z1)

(x2,y2,z2)


Vectors (2/5)

  • X = V + W

  • = (x1, y1, z1)

  • = (v1 + w1, v2 + w2, v3 + w3)

  • Addition of vectors

W

V + W

W

V + W

V

V

  • Length of vectors

|V| = (v12 + v22 + v32)1/2

U = V / |V|


Vectors (3/5)

  • Cross product of vectors

    • Definition

  • X = VXW

  • = (v2w3-v3w2)i + (v3w1-v1w3)j + (v1w2-v2w1)k

  • where i, j and k are standard unit vectors :

  • i = (1, 0, 0), j = (0, 1, 0), k = (0, 0, 1)

  • Application

    • A normal vector to a polygon is calculated from 3 (non-collinear) vertices of the polygon.

Np

polygon defined by 4 points

V2

  • Np = V1XV2

V1


Vectors (4/5)

N(X) = detJJ-1TN(x)

where X = F(x)

Jthe Jacobian matrix,Ji(x) =

  • Transformation of the normal vectors

dF(x)

dxi

"Global and Local Deformations of Solid Primitives"

Alan H. Barr

Computer Graphics Volume 18, Number 3 July 1984


Vectors (5/5)

  • Dot product of vectors

    • Definition

  • |X| = V.W

  • = v1w1 + v2w2+v3w3

  • Application

V

q

W

V.W

cosq =

|V||W|


Fixed Point Arithmetics (1/2)

  • Fixed point arithmetics : n bits (signed) integer

    • Example : N = 16 gives range –32768  ă  32767

    • We can use fixed scale to get the decimals.

a = ă / 28

8 integer bits

1

1

1

8 fractional bits

ă = 315, a = 1.2305


Fixed Point Arithmetics (2/2)

e = a.c = ă / 28 . ĉ / 28

 ĕ = (ă .ĉ) / 28

  • Multiplication then requires rescaling

  • Addition just like normal

e = a+c = ă / 28 +ĉ / 28

 ĕ = ă + ĉ


Fixed Point Arithmetics - Application

  • Compression for floating-point real numbers

    • 4 bytes reduced to 2 bytes

    • Lost some accuracy but affordable

  • Network data transfer

  • Software 3D rendering (without hardware-assistant)


Triangular Coordinate System

ha

(xa,ya,za)

Ac

p

Ab

hb

h

(xb,yb,zb)

Aa

hc

(xc,yc,zc)

Aa

Ab

Ac

h = ha + hb + hc

where A = Aa + Ab + Ac

If (Aa < 0 || Ab < 0 || Ac < 0) than

the point is outside the triangle

“Triangular Coordinate System”

A

A

A


Triangle Area – 2D

Area of a triangle in 2D

xa ya

A = ½ xb yb

xc yc

xa ya

= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc)

(xa,ya,za)

(xb,yb,zb)

(xc,yc,zc)


Triangle Area – 3D

Area of a triangle in 3D A = ½ (N. Sum(Pi1 cross Pi2))

where (i1, i2) = (a,b), (b,c), (c,a)

float GmArea3(float *x0, float *x1, float *x2, float *n)

{

float area, len, sum1, sum2, sum0;

len = (float) sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) * 2.0f;

/* find sum of cross products */

sum0 = x1[1] * (-x0[2] + x2[2]) + x2[1] * (-x1[2] + x0[2]) +

x0[1] * (-x2[2] + x1[2]);

sum1 = x1[2] * (-x0[0] + x2[0]) + x2[2] * (-x1[0] + x0[0]) +

x0[2] * (-x2[0] + x1[0]);

sum2 = x1[0] * (-x0[1] + x2[1]) + x2[0] * (-x1[1] + x0[1]) +

x0[0] * (-x2[1] + x1[1]);

/* find the area */

return = (sum0 * n[0] + sum1 * n[1] + sum2 * n[2]) / len;

}


Triangular Coordinate System - Application

  • Terrain following

    • Interpolating the height of arbitrary point within the triangle

  • Hit test

    • Intersection of a ray from camera to a screen position with a triangle

  • Ray cast

    • Intersection of a ray with a triangle

  • Collision detection

    • Intersection


Intersection

  • Ray cast

  • Containment test


Ray Cast – The Ray

  • Cast a ray to calculate the intersection of the ray with models

  • Use parametric equation for a ray

x = x0 + (x1 – x0) t

y = y0 + (y1 – y0) t, t = 0,

z = z0 + (z1 – z0) t

{

8

  • When t = 0, the ray is on the start point (x0,y0,z0)

  • Only the t  0 is the answer candidate

  • The smallest positive t is the answer


Ray Cast – The Plane

  • Each triangle in the 3D models has its plane equation.

  • Use ax + by + cz + d = 0 as the plane equation.

  • (a, b, c) is the plane normal vector.

  • |d| is the distance of the plane to origin.

  • Substitute the ray equation into the plane.

  • Solve the t to find the intersect point.

  • Check the intersect point within the triangle or not by using “Triangle Area Test” (p. 155)


2D Containment Test

Intersection = 1, inside

Intersection = 2, outside

(x0, y0)

Intersection = 0, outside

Trick : Parametric equation for a ray which is parallel to the x-axis

x = x0 + t

y = y0 , t = 0,

{

8

“if the No. of intersection is odd, the point is inside,

otherwise, is outside”


3D Containment Test

  • Same as the 2D containment test

“if the No. of intersection is odd, the point is inside,

otherwise, is outside”


Euler Angles

  • A rotation is described as a sequence of rotations about three mutually orthogonal coordinates axes fixed in space

    • X-roll, Y-roll, Z-roll

R(q1, q2, q3) represents an x-roll, followed by y-roll, followed by z-roll

R(q1, q2, q3) = c2c3 c2s3 -s2 0

s1s2c3-c1s3 s1s2s3+c1c3 s1c2 0

c1s2c3+s1s3 c1s2s3-s1c3 c1c2 0

0 0 0 1

where si = sinqi and ci = cosqi

  • There are 6 possible ways to define a rotation.

    • 3!


Euler Angles & Interpolation

  • Interpolation happening on each angle

  • Multiple routes for interpolation

  • More keys for constrains

R

z

z

y

y

x

x

R


Angular Displacement

  • R(q, n), n is the rotation axis.

rh = (n.r)n

rv = r - (n.r)n , rotate into position Rrv

V = nxrv = nxr

Rrv = (cosq)rv + (sinq)V

->

Rr = Rrh + Rrv

= rh + (cosq)rv + (sinq)V

= (n.r)n + (cosq)(r - (n.r)n) + (sinq) nxr

= (cosq)r + (1-cosq) n(n.r) + (sinq) nxr

V

rv

q

r

r

Rr

rh

n

n

V

rv

q

Rrv


Quaternion

  • Sir William Hamilton (1843)

  • From Complex numbers (a + ib), i 2 = -1

  • 16,October, 1843, Broome Bridge in Dublin

  • 1 real + 3 imaginary = 1 quaternion

  • q = a + bi + cj + dk

  • i2 = j2 = k2 = -1

  • ij = k & ji = -k, cyclic permutation i-j-k-i

  • q = (s, v), where (s, v) = s + vxi + vyj + vzk


Quaternion Algebra

q1=(s1, v1)and q2=(s2, v2)

q3= q1q2 = (s1s2 - v1.v2 , s1v2 + s2v1 + v1xv2)

Conjugate of q = (s, v), q = (s, -v)

qq = s2 + |v|2 = |q|2

A unit quaternion q = (s, v), where qq = 1

A pure quaternion p = (0, v)

Noncommutative


Quaternion VS Angular Displacement

Take a pure quaternion p = (0, r)

and a unit quaternion q = (s, v) where qq = 1

and define Rq(p) = qpq-1 where q-1 = q for a unit quaternion

Rq(p) = (0, (s2 - v.v)r + 2v(v.r) + 2svxr)

Let q = (cosq, sinq n), |n| = 1

Rq(p) = (0, (cos2q - sin2q)r + 2sin2qn(n.r) + 2cosqsinqnxr)

= (0, cos2qr + (1 - cos2q)n(n.r) + sin2qnxr)

Conclusion :

The act of rotating a vector r by an angular displacement (q, n) is the same as taking this displacement, ‘lifting’ it into quaternion space, by using a unit quaternion (cos(q/2), sin(q/2)n)


Quaternion VS Rotation Matrix

1-2y2-2z2 2xy-2wz 2xz+2wy 0

2xy+2wz 1-2x2-2z2 2yz-2wx 0

2xz-2wy 2yz+2wx 1-2x2-2y2 0

0 0 0 1

q =(w,x,y,z)


float tr, s;

tr = m[0] + m[4] + m[8];

if (tr > 0.0f) {

s = (float) sqrt(tr + 1.0f);

q->w = s/2.0f;

s = 0.5f/s;

q->x = (m[7] - m[5])*s;

q->y = (m[2] - m[6])*s;

q->z = (m[3] - m[1])*s;

}

else {

float qq[4];

int i, j, k;

int nxt[3] = {1, 2, 0};

i = 0;

if (m[4] > m[0]) i = 1;

if (m[8] > m[i*3+i]) i = 2;

j = nxt[i]; k = nxt[j];

s = (float) sqrt((m[i*3+i] - (m[j*3+j] + m[k*3+k])) + 1.0f);

qq[i] = s*0.5f;

if (s != 0.0f) s = 0.5f/s;

qq[3] = (m[j+k*3] - m[k+j*3])*s;

qq[j] = (m[i+j*3] + m[j+i*3])*s;

qq[k] = (m[i+k*3] + m[k+i*3])*s;

q->w = qq[3];

q->x = qq[0];

q->y = qq[1];

q->z = qq[2];

}

M0 M1 M2 0

M3 M4 M5 0

M6 M7 M8 0

0 0 0 1


Quaternion Interpolation

  • Spherical linear interpolation, slerp

A

P

B

t

f

sin((1 - t)f)

sin(tf)

slerp(q1, q2, t) = q1 + q2

sinf

sinf


Differential Equation Basics

  • Initial value problems

  • ODE

    • Ordinary differential equation

  • Numerical solutions

    • Euler’s method

    • The midpoint method


Initial Value Problems

  • An ODE

.

x = f (x, t)

wherefis a known function

x is the state of the system, x is the x’s time derivative

x & x are vectors

x(t0) = x0, initial condition

.

.

  • Vector field

  • Solutions

    • Symbolic solution

    • Numerical solution

Follow the vectors …

Start here


Euler’s Method

x(t + Dt) = x(t) + Dt f(x, t)

  • A numerical solution

    • A simplification from Tayler series

  • Discrete time steps starting with initial value

  • Simple but not accurate

    • Bigger steps, bigger errors

    • O(Dt2) errors

  • Can be unstable

  • Not even efficient


The Midpoint Method

Error term

.

..

Concept :x(t0 + h) = x(t0) + h x(t0) + h2/2 x(t0) + O(h3)

Result : x(t0+h) = x(t0) + h(f(x0 + h/2 f(x0))

Method :a. Compute an Euler step

Dx = Dt f(x, t)

b. Evaluate f at the midpoint

fmid = f((x+Dx)/2, (t+Dt)/2)

c. Take a step using the midpoint

x(t+Dt) = x(t) + Dt fmid

c

b

a


The Runge-Kutta Method

  • Midpoint = Runge-Kutta method of order 2

  • Runge-Kutta method of order 4

    • O(h5)

k1 = h f(x0, t0)

k2 = h f(x0 + k1/2, t0 + h/2)

k3 = h f(x0 + k2/2, t0 + h/2)

k4 = h f(x0 + k3, t0 + h)

x(t0+h) = x0 + 1/6 k1 + 1/3 k2 + 1/3 k3 + 1/6 k4


Initial Value Problems - Application

  • Dynamics

    • Particle system

  • Game FX system

  • Fluid simulation


6th

Game Geometry


Game Models

  • Geometry

    • Position

    • vertex normals

    • vertex colors

    • texture coordinates

    • Tangent / Bi-normal

  • Topology

    • Primitive

      • Lines / triangles / surfaces / …

  • Property

    • Materials

    • Textures

  • Motion

  • Hierarchy

  • Level-of-detail


Geometry Data

  • Vertex position

    • (x, y, z, w)

    • In model space or screen space

  • Vertex normal

    • (nx, ny, nz)

  • Vertex color

    • (r, g, b) or (diffuse, specular)

  • Texture coordinates on vertex

    • (u1, v1), (u2, v2), …

  • Skin weights

    • (bone1, w1, bone2, w2, …)

  • Tangent and bi-normal

N

T

Bn


Topology Data

  • Lines

    • Line segments

    • Polyline

      • Open / closed

  • Indexed triangles

  • Triangle strips / fans

  • Surfaces

    • Non-uniform Rational BSpline (NURBS)

  • Subdivision


Indexed Triangles

  • Geometric data

    • Vertex data

    • v0, v1, v2, v3, …

    • (x, y, z, nx, ny, nz, tu, tv)

    • or (x, y, z, cr, cg, cb, tu, tv, …)

  • Topology

    • Face v0 v3 v6 v7

    • Edge table

polygon normal

vertex normal

v0

v7

v3

v6

Right-hand rule for index


Triangle Strips

v0

v6

v2

v4

T0

T4

T2

T5

T3

T1

v5

v1

v7

v3

v0 , v1 ,v2 ,v3 ,v4 ,v5 ,v6 ,v7

“Get great performance to use triangle strips for rendering on current hardware


Property on Surface

  • Material

  • Textures

  • Shaders


Materials

  • Material

    • Ambient

      • Environment

      • Non-lighted area

    • Diffuse

      • Dynamic lighting

    • Emissive

      • Self-lighting

    • Specular with shineness

      • Hi-light

      • View-dependent

      • Not good for hardware rendering

  • Local illumination


Textures

  • Textures

    • Single texture

    • Texture coordinate animation

    • Texture animation

    • Multiple textures

    • Alphamap

Lightmap

Base color texture

Material or vertex colors


Shaders

  • Programmable shading language

    • Vertex shader

    • Pixel shader

  • Procedural way to implement some process of rendering

    • Transformation

    • Lighting

    • Texturing

    • BRDF

    • Rasterization

    • Pixel fill-in

    • Post-processing for rendering


Shader Pipeline

Vertex Data

Topology Data

Classic

Transform &

Lighting

Vertex Shader

Geometry

Stage

Clipping & Viewport Mapping

Texturing

Pixel Shader

Rasterizer

Stage

Fog

Alpha, Stencil, Depth Testing


Powered by Shader

  • Procedural Morphing

  • Water Simulation

  • Per-pixel lighting

  • Motion blur

  • Volume / Height fog

  • Volume lines

  • Depth of field

  • Fur rendering

  • Reflection / Refraction

  • NPR

  • Shadow

  • Linear algebra operators

  • Perlin noise

  • Quaternion

  • Sparse matrix solvers

  • Skin bone deformation

  • Normal map

  • Displacement map

  • Particle shader


Motion Data

  • Time-dependent data

  • Transformation data

    • Position

    • Orientation

  • Formats

    • Pivot

    • Position vector

    • Quaternion

    • Eurler angles

    • Angular displacement


Level-of-detail

  • Discrete LOD

    • Switch multiple resolution models run-timely

  • Continuous LOD

    • Use progressive mesh to dynamically reduce the rendered polygons

  • View-dependent LOD

    • Basically for terrain


Progressive Mesh

  • Render a model in different Level-of-Detail at run time

  • User-controlledly or automatically change the percentage of rendered vertices

  • Use collapse map to control the simplification process

Collapse map

Vertex list

Triangle list

0

0

6

4


View-dependent LOD for Terrain - ROAM

  • Real-time Optimal Adapting Meshes (ROAM)

  • Use height map

  • Run-timely to re-construct the active (for rendering) geometric topology (re-mesh) to get an optimal mesh (polygon count) to improve the rendering performance

  • Someone calls this technique as the view-dependent level-of-detail

  • Very good for fly-simulation-like application


Level-of-detail Suggestion

  • Apply progressive mesh for multi-resolution model generation

  • Use in-game discrete LOD for performance tuning

  • Why ?

    • For modern game API / platform, dynamic vertex update is costly on performance

    • Lock video memory stall CPU/GPU performance


Bounding Volume

  • Bounding sphere

  • Bounding cylinder

  • Axis-aligned bounding box (AABB)

  • Oriented bounding box (OBB)

  • Discrete oriented polytope (k-DOP)

Bounding Sphere

AABB

k-DOP

Bounding Cylinder

OBB


Bounding Volume - Application

  • Collision detection

  • Visibility culling

  • Hit test

  • Steering behavior

    • In “Game AI” section


Application Example - Bounding Sphere

B2

B1

D

c2

c1

Bounding sphere B1(c1, r1), B2(c2, r2)

If the distance between two bounding spheres is

larger than the sum of radius of the spheres, than

these two objects have no chance to collide.

D > Sum(r1, r2)


Application Example - AABB

  • Axis-aligned bounding box (AABB)

    • Simplified calculation using axis-alignment feature

    • But need run-timely to track the bounding box

AABB


Application Example - OBB

  • Oriented bounding box (OBB)

    • Need intersection calculation using the transformed OBB geometric data

      • 3D containment test

      • Line intersection with plane

  • For games, 

OBB


7th

Terrain


Introduction

  • Very game type oriented data

  • Terrain

    • For visual purpose

      • Ground / Building / Static models / Dynamic models

    • For terrain following

      • Polygon mesh

      • Grids

    • For path finding

      • Polygon mesh

      • Grids

  • Terrain following

    • Make a 3D entity (character or model) walking on terrain

  • Path finding

    • Find a “shortest” path to walk before moving

    • Will be taught in Game AI section.

    • A* algorithm


Terrain Formats

Perlin Noise

  • Grid

    • 2D

    • Quadtree

  • Height map

    • Procedural height map

    • Using noise function to generate the height

  • ROAM

    • Real-time Optimally Adapting Meshes

  • Triangular mesh

    • Procedurally generated

    • Created by artists


Grid Map

  • 2D grid map

    • Rectangular or Hexagonal grids

    • Attributes

      • Height

      • Walkable or not

      • Texture pattern ID

  • Step look terrain

  • Application

    • 2D games

    • 3D games with god view

      • 2D tile-based game terrain


Height Map

Top view

  • Almost as same as 2D grid map but :"

    • Height on grid vertex

    • Only height is saved

    • Regular grid

    • Irregular grid but structured

  • Application

    • As the base data structure for ROAM terrain

    • Water simulation


ROAM

  • Application

    • Fly-simulation

  • Real-time optimally adapting mesh

    • http://www.llnl.gov/graphics/ROAM/


Chunked LOD Terrain

  • Use quad tree to construct the level-of-detail of terrain

    • A quad tree for LOD


Triangular Mesh

  • Possibly the most popular way for 3D games

    • General

    • Can be created by artists

  • Multiple-layered terrain issue


Terrain Following Using Triangular Mesh

  • Solve the terrain height for the object to stand on.

    • Use the triangular coordinate system (p. 154)

  • Find the next neighboring triangle

    • Half-edge data structure


Half-edge (1/2)

Edge = two halves

  • Create cohesive relationship between triangles using “half edge”

  • Use half-edge table to search the neighboring triangles


Half-edge (2/2)

struct HE_edge

{

HE_vert* vert; // vertex at the end of the half-edge

HE_edge* pair; // oppositely oriented adjacent half-edge

HE_face* face; // face the half-edge borders

HE_edge* next; // next half-edge around the face

};

struct HE_vert

{

float x;

float y;

float z;

HE_edge* edge; // one of the half-edges

// emantating from the vertex

};

struct HE_face

{

HE_edge* edge; // one of the half-edges bordering the face

};

http://www.flipcode.com/tutorials/tut_halfedge.shtml


8th

Character Motion


A Segmented Character

  • A character is composed by a set of models with motion data to simulate a live creature in real world


A Mesh Character

  • Vertex animation on skins

    • Animated positional data on skins

    • 3D warping


A Bone-skin Character

Bone A

Skin

Bone B

  • Bone-Skin Skeleton

    • Hierarchical bones

    • Skin deformationrun-timely


Motion Data

  • Euler angles

  • Angular displacement

  • Quaternion

    • Can achieve the interpolation by “Slerp”

  • But finally they will be converted into “matrix”


Optical Motion Capture

  • Device

  • Data acquired

    • From skin to joint (Mocap)

    • From joint to skeleton (Post-processing)

    • From skeleton to skin (In-game)

  • The shooting plan


Mocap Devices


Data Acquirement During the Mocap

Raw Data (Positional Data)

Bio-Data

Joint

End Point


Bone-skin Implementation In Game

Skin

Bone-Skin

Skeletons

Skeletons


Planning a Mocap Shoot

  • Starting out – reviewing the animation list and flowchart


  • Creating a shot list

    • Create a database

    • File names

    • Preliminary shot list

A Data Record of Shot List


A Shoot List


  • Getting ready for the shoot

    • When to Shoot ?

    • Find a Studio

    • Make Sure No Technical Blocks

    • Casting

  • Preparing a shooting schedule

    • Organize the Shot List

    • Daily Schedule

    • Do You Need a Rehearsal Day ?

  • Take care of your performer


A Daily Schedule


Apply Motion for Characters

  • Apply motion data on bones

(x,y,z,q,axis)

A

(q,axis)

Joint = pivot(px,py,pz) in A

B

<v’> = <V> [RB][TB][RA][TA]

From position

From pivot


Motion Editing

  • To create more animation from limited work

  • Run-time or pre-processing

  • Issues :

    • Motion re-targeting

      • Run-time

    • Re-key-framing

      • Pre-processing

    • Interpolation between frames

      • Run-time

    • Motion blending

      • Run-time

    • Motion connection

      • Run-time


A Pose Definition Example

start_frame

end_frame

walk

raw_start_frame

raw_end_frame

cut_frame

Parameter {

raw_start_frame

raw_end_frame

start_frame

end_frame

cut_frame

play_speed

length

transition_mode

}


Play a Pose

walk

0

4

8

Frame 5.3

  • If the motion data is in quaternion form

  • Get the motion data on frame 5 & 6

  • Convert the data into quaternion format

  • Apply slerp(5, 6, 0.3) to get the interpolation on frame 5.3

  • Convert the result of step 3 into a rotation matrix

  • Apply the matrix to the object for its transformation


Pose Connection

cut_frame

Pose 1

start_frame

Pose 2

length


Pose Blending

  • Motion blending in run-time

  • Quaternion is used

  • “Blend Tree”

    • Cross fade

    • Countinuous blending

    • Feather blending


Blend Tree

  • Reference

    • Game Developers Conference 2003

    • Proceedings CD, Programming Track

    • “Animation Blending : Achieving Inverse Kinematics and More”

    • Jerry Edsall

    • Mech Warrior blend tree

Walk

Forward

Motion

Fall Transition

Run

Fall

Down


Cross Fade

1

Pose 1

0

1

Pose2

0


Continuous Blending

1

Pose 1

0

1

Pose 2

0


Feather Blending

  • 左右搏擊

Pose 1

Pose 2

Pose 3


Skin Deformation

  • Weights to assign the influences of the deformation by bones on skin vertices

    • 1-weight

    • 2-weight

    • N-weight

  • CPU cost

  • Another way to perform the skin deformation calculation is using vertex shader


Bone A

(root object)

base

Bone B

(Bone A’s child)

base

  • Apply motion data to bones

  • Convert the vertex from “base” space to its associated bone’s space using the natural pose’s inverse transformation

  • Multiple the influence weight

  • Accumulate all influences

  • Then the vertex is deformed by the bone in “base” space


A two-weight skin vertex example

Mb= RbTpivot

Ma= RaTposition

Mvb= Mnb-1 MbMa

Mva= Mna-1Ma

vin_base = vs*waMva + vs*wbMvb


9th

Game Control


Game Control System (1/2)

  • Game control is the interface between the game and the user.

  • Game control is not only input device control but also the camera control

  • Input device control

    • On PC

      • Mouse

      • Keyboard

      • Gamepad

    • On game console

      • Gamepad buttons

        • 0 or 255

      • Joystick

        • 0 - 255


Game Control System (2/2)

  • Camera control

    • First-personal view

    • Third-personal view

    • God view

    • Pre-set camera view

    • Etc


Mouse Control (1/3)

  • Mouse is a 2D device.

    • 2-axis moving

    • Related movement

    • 2 or 3 buttons

  • Mouse can :

    • Move

    • Drag

    • Double-click

  • Behaviors

    • Hit test

    • Selection

    • Pilot

      • Position & orientation


Mouse Control (2/3)

  • Typical game types using mouse control

    • Real-time strategy games

    • Role Playing Game

  • Typical game play examples :

    • Path finding for playable character

    • Hitting the enemy

    • Selecting a group of units

    • Orientating the camera in FPS games

    • Menu selection

  • Features

    • Always coupling with god-view camera control

      • Viewing from the top of game world


Mouse Control (3/3)

  • Easy to hand on

    • 一鼠到底

  • Slow action

    • Compared with joystick

    • Value range from -32727 - 32727


Keyboard Control (1/3)

  • Standard PC input device

  • Simulating the gamepads usually

    • Not every PC game player having gamepad

    • Using keyboard as the alternative device

  • Hotkey system

    • Each key has two states.

      • Pressed

      • Released

    • 256 keys

  • Behaviors

    • Key presses/released

    • ASCII code

  • One hotkey can represent a command


Keyboard Control (2/3)

  • Communication tool

    • Typing messages

  • Typical game types using keyboard

    • MMORPG

      • Needs chatting with friends

    • Real-time strategy games

      • Hotkey system

    • First-person shooting games

    • Fighting games

  • Typical game play examples :

    • Chatting

    • Character controls

      • Move forward

      • Turning


Keyboard Control (3/3)

  • Features

    • Shortcut for a sequence of actions

      • Commands

      • Menu selection

    • But a little bit complicated for players

      • 256 keys


Gamepad Control (1/3)

  • A small “keyboard” designed for game playing

    • Gamepad can map to the hotkey system

    • Same behaviors

    • Less than 20 keys

  • Majors keys :


Gamepad Control (2/3)

  • Recent gamepad capable of two extra digital joysticks

    • For buttons

      • Value range : 0 or 255

    • For joystick

      • Value range : 0 to 255

  • Typical game types using gamepad

    • Almost all types of games except

      • Need typing

      • Need large-range selection for game units

  • Typical game play examples :

    • Character controls

      • Move forward

      • Turn


Gamepad Control (3/3)

  • Combat system in a fighting game

    • Move forward

    • Turn

  • Features

    • Designed for game playing

      • Look and feel

    • Easy to hand-on

      • If you not to challenge the players’ usual practice


  • Camera Control

    • Types

      • First-personal view

      • Third-personal view but following the playable character

      • God view

        • Fixed

        • Following the playable character

      • Fixed view

        • Pre-rendered background

      • Pre-set view

    • Very sensitive to game play design & game control

    • Camera control is not an independent system


    God-view Camera Example

    Age of Empire 3


    Case Study – Third-personal View (1/6)

    • Use arrow keys on keyboard or gamepad

    • Basic key assignments :

      • Up key to move the playable character forward

      • Down key to turn character facing to the camera and move forward

      • Left & right keys to turn the character to left or right


    Camera

    q

    PC

    Height

    Distance

    Case Study – Third-personal View (2/6)

    • The camera following the character to move

    • And keeping a range of distance, a reasonable height and look-down angle with the character.


    Case Study – Third-personal View (3/6)

    • Detailed key assignments :

      • Up key

        • Turn the character facing back to the camera

        • Move the character forward

        • If the distance between the character and the camera is larger a pre-set range, move the camera forward to keep the distance.

        • At the same time, the height to the ground will be changed to synchronize with the character.

      • Down key

        • Turn the character facing to the camera

        • Move the character forward

        • The camera will move backward to keep a distance with the character.

        • The height to the ground will be changed to synchronize with the character.


    PC

    Camera

    Case Study – Third-personal View (4/6)

    • If the camera is blocked by obstacle to move backward, raise the height of the camera but keep the eyes on the character.


    Case Study – Third-personal View (5/6)

    • Right key

      • Turn the character facing to the right of the camera.

      • Take the camera’s position as a circle center and the distance between the camera and the character as the radius.

      • Set the circle as the movement orbit.

      • Let the character move on the orbit.

      • When the character moving, turn the camera to right to keep eyes on the character.


    Case Study – Third-personal View (6/6)

    • When the character hitting the obstacle, let the character keep on turning and moving, use the same approach in “Down key” step to raise the camera.

  • Left key

    • As same as “Right key” step except the left direction.

  • Reference game examples:

    • Sprinter cell 3

    • PSO

    • Prince of Persia(波斯王子)

    • The Legend of Zelda (薩爾達傳說)

  • Demo : 1st DCI students’ work : iRobot


  • 10th

    Advanced Scene

    Management

    System


    Bounding Volume

    • Bounding sphere

    • Bounding cylinder

    • Axis-aligned bounding box (AABB)

    • Oriented bounding box (OBB)

    • Discrete oriented polytope (k-DOP)

    Bounding Sphere

    AABB

    k-DOP

    Bounding Cylinder

    OBB


    Bounding Volume - Application

    • Collision detection

    • Visibility culling

    • Hit test

    • Steering behavior

      • In “Game AI” section


    Application Example - Bounding Sphere

    B2

    B1

    D

    c2

    c1

    Bounding sphere B1(c1, r1), B2(c2, r2)

    If the distance between two bounding spheres is

    larger than the sum of radius of the spheres, than

    these two objects have no chance to collide.

    D > Sum(r1, r2)


    Application Example - AABB

    • Axis-aligned bounding box (AABB)

      • Simplified calculation using axis-alignment feature

      • But need run-timely to track the bounding box

    AABB


    Application Example - OBB

    • Oriented bounding box (OBB)

      • Need intersection calculation using the transformed OBB geometric data

        • 3D containment test

        • Line intersection with plane

    • For games, 

    OBB


    Advanced Scene Graphs

    • This is a game-type-oriented issue

    • Bounding Volume Hierarchies (BVHs)

    • Binary space partition trees (BSP Trees)

      • “Quake”

    • Octree

    • PVS

    • Culling Skills


    Bounding Volume Hierarchies (BVHs)

    • Bounding spheres in hierarchy

    R

    B


    BSP Tree

    • Two varients

      • Axis-aligned

      • Polygon-aligned

    • The trees are created by using a plane to divide the space into two, and then sorting the geometry into two spaces.


    Axis-aligned BSP Tree

    0

    plane0

    2

    plane3

    1

    plane2

    plane1

    3


    Polygon-aligned BSP Tree

    A

    F

    C

    G

    B

    A

    B

    C

    D

    E

    D

    F

    G

    E


    Why BSP Tree ?

    • Quickly to identify where you are

      • BSP = Sorting

    • Need a pre-processor to generate the PVS

      • Visibility culling + occlusion culling

      • PVS : Possible Visible Set

    • Optimized for in-door game environment

    • [Fuch80]

      • Fuchs, H.,

      • On Visible Surface Generation by a Priori Tree Structures,

      • Computer Graphics, 14, 124-33, (Proc. SIGGRAPH’80)


    Octree & Quadtree

    • Very similar to axis-aligned BSP tree.

    • Except that a box is split simultaneously along all three axes.

    • The split point must be the center of the box.

    • This creates eight new boxes.

    • Quadtree is the 2D version of octree.


    Quadtree - Example


    Octree – Some Discussion

    • Data structure coherence

    • Apply visibility culling from parents

    • Split or not split ?

    • Outdoor game scene ?


    Culling (1/2)

    • Culling means “remove from a flock”

    • Visibility culling

      • Remove the object not in view frustum

      • A “must” for game engine

    • Backface culling

      • Remove the polygons facing away from camera

      • Hardware standard

    • Occlusion culling

      • Remove the objects hidden by the others


    Culling (2/2)

    View frustum

    Occlusion

    culling

    eye

    Visibility

    culling

    Backface

    culling


    BSP Implementation

    • A Pre-processor

      • Space partition the scene data from artist

      • Generate the BSP data structure

      • Generate the PVS

    • BSP walk through

      • Identify the room where you are

      • Show/hide the rooms according to the PVS


    BSP Preprocessor (1/2)

    • Input

      • A scene from artist

      • Cutting planes (optional)

        • Can be procedurally generated by algorithm

      • Cutting policy

        • Split or not split

      • Ray casting resolution for PVS

    • Output

      • A BSP file

        • BSP Tree

        • PVS

        • Geometry Data


    BSP Preprocessor (2/2)

    • Process

      • Generate the BSP tree according to the cutting policy

      • Split or sort the geometry into BSP room (leaves)

      • For each “room”, ray cast all rooms to generate the possible visible room set

      • 3D

      • Time consuming


    BSP Challenges

    • Effectiveness of PVS

      • Data set

    • Dynamic objects

    • Room size


    11th

    Game AI


    Contents

    • Search

    • Path finding

    • Steering behavior

    • Finite state machines


    Search

    • What we will talk about

      • Blind search

      • Heuristic search

      • Adversary search

    • Blind search

      • Breadth-first search

      • Depth-first search

    • Heuristic search

      • A*

    • Adversary search

      • Minimax


    Introduction to Search

    i

    d = 1

    d = 2

    d = 3

    d = 4

    c3

    c2

    c1

    g

    • Using tree diagram (usually) to describe a search problem

      • Search starts

        • Node i

      • Goal

        • Goal node g

      • Successors

        • c1, c2, c3…

      • Depth

        • d = 0, 1, 2, …

    • Search problem

      • Input

        • Description of the initial and goal nodes

        • A procedure that produces the successors of an arbitrary node

      • Output

        • A legal sequence of nodes starting with the initial node and ending with the goal node.


    Search Examples in Traditional AI

    • Game playing

      • Chess

      • Backgammon

    • Finding a path to goal

      • The towers of Hanoi

      • Sliding tile puzzles

        • 8 puzzles

    • Simply finding a goal

      • n-queens


    Search Algorithm

    • Set L to be a list of the initial nodes. At any given point in time, L is a list of nodes that have not yet been examined.

    • If L is empty, failed. Otherwise, pick a node n from L.

    • If n is the goal node, stop and return it and the path from the initial node to n

    • Otherwise, remove n from L and add to L all of n’s children, labeling each with its path from the initial node.

    • Return to step 2


    Depth-First Search (1/3)

    1

    d = 1

    d = 2

    d = 3

    d = 4

    9

    8

    2

    10

    3

    7

    4

    g

    5

    6

    11

    • Always exploring the child of the most recently expanded node

    • Terminal nodes being examined from left to right

    • If the node has no children, the procedure backs up a minimum amount before choosing another node to examine.


    Depth-First Search (2/3)

    • We stop the search when we select the goal node g.

    • Depth-first search can be implemented by pushing the children of a given node onto the front of the list L in step 4 of page 6

    • And always choosing the first node on L as the one to expand.


    Depth-First Search (3/3)

    • Algorithm

      • Set L to be a list of the initial nodes in the problem

      • Let n be the first node on L. If L is empty, fail.

      • If n is a goal node, stop and return it and path from the initial node to n.

      • Otherwise, remove n from L and add to the front of L all of n’s children, labelling each with its path from the initial node. Return to step 2.


    Breadth-First Search (1/2)

    1

    d = 1

    d = 2

    d = 3

    d = 4

    4

    3

    2

    7

    8

    5

    6

    9

    g

    10

    11

    12

    • The tree examined from top to down, so every node at depth d is examined before any node at depth d + 1.

    • We can implement breadth-first search by adding the new nodes to the end of the list L.


    Breadth-First Search (2/2)

    • Algorithm

      • Set L to be a list of the initial nodes in the problem

      • Let n be the first node on L. If L is empty, fail.

      • If n is a goal node, stop and return it and path from the initial node to n.

      • Otherwise, remove n from L and add to the end of L all of n’s children, labeling each with its path from the initial node. Return to step 2.


    Heuristic Search (1/2)

    1

    d = 1

    d = 2

    d = 3

    d = 4

    2

    3

    g

    4

    • Neither depth-first nor breadth-first search

    • Exploring the tree in anything resembling an optimal order.

    • Minimizing the cost to solve the problem


    Heuristic Search (2/2)

    • When we picking a node from the list L in step 2 of search procedure, what we will do is to remove steadily from the root node toward the goal by always selecting a node that is as close to the goal as possible.

      • Estimated by distance and minimizing the cost?

        • A* !


    Adversary Search

    • Assumptions

      • Two-person games in which the players alternate moves.

      • They are games of “perfect” information, where the knowledge available to each player is the same.

    • Examples :

      • Tic-tac-toe

      • Checkers

      • Chess

      • Go

      • Othello

      • Backgammon

    • Imperfect information

      • Pokers

      • Bridge


    Minimax (1/6)

    Nodes with the maximizer

    to move are square; nodes

    with the minimizer to move

    are circles

    “ply”

    max

    min

    max

    min

    max

    min

    a

    b

    c

    d

    -1

    e

    f

    g

    1

    -1

    i

    h

    j

    k

    -1

    1

    1

    l

    m

    n

    o

    1

    -1

    1

    p

    q

    r

    Maximizer to achieve the

    Outcome of 1; minimizer to

    Achieve the outcome of -1

    -1

    1

    -1


    Minimax (2/6)

    The maximizer wins!

    1

    max

    min

    max

    min

    max

    min

    a

    1

    -1

    b

    c

    d

    -1

    -1

    e

    f

    g

    1

    1

    1

    -1

    i

    h

    j

    k

    -1

    1

    1

    l

    m

    n

    o

    1

    -1

    1

    p

    q

    r

    -1

    1

    -1


    Minimax (3/6)

    • Basic idea

      • Expand the entire tree below n

      • Evaluate the terminal nodes as wins for the minimizer or maximizer.

      • Select an unlabelled node all of whose children have been assigned values. If there is no such node, return the value assigned to the node n.

      • If the selected node is one at which the minimizer moves, assign it a value that is the minimum of the values of its children. If it is a maximizing node, assign it a value that is the maximum of the children’s values. Return to step 3.


    Minimax (4/6)

    • The algorithm

      • Set L = { n }, the unexpanded nodes in the tree

      • Let x be the 1st node on L. If x = n and there is a value assigned to it, return this value.

      • If x has been assigned a value vx, let p be the parent of x and vp the value currently assigned to p. If p is a minimizing node, set vp = min(vp, vx). If p is a maximizing node, set vp = max(vp, vx). Remove x from L and return to step 2.

      • If x has not been assigned a value and is a terminal node, assign it the value 1 or -1 depending on whether it is a win for the maximizer or minimizer respectively. Assign x the value 0 if the position is a draw. Leave x on L and return to step 2.

      • If x has not been assigned a value and is a nonterminal node, set vx to be –∞ if x is a maximizing node and + ∞ if x is a minimizing node. Add the children of x to the front of L and return to step 2.


    Minimax (5/6)

    • Some issues

      • Draw

      • Estimated value e(n)

        • e(n) = 1 : the node is a win for maximizer

        • e(n) = -1 : the node is a win for minimizer

        • e(n) = 0 : that is a draw

        • e(n) = -1 ~ 1 : the others

      • When to decide stop the tree expanding further ?


    Minimax (6/6)

    • The algorithm (final)

      • Set L = { n }, the unexpanded nodes in the tree

      • Let x be the 1st node on L. If x = n and there is a value assigned to it, return this value.

      • If x has been assigned a value vx, let p be the parent of x and vp the value currently assigned to p. If p is a minimizing node, set vp = min(vp, vx). If p is a maximizing node, set vp = max(vp, vx). Remove x from L and return to step 2.

      • If x has not been assigned a value and either x is a terminal node or we have decided not to expand the tree further, compute its value using the evaluation function. Leave x on L and return to step 2.

      • Otherwise, set vx to be –∞ if x is a maximizing node and + ∞ if x is a minimizing node. Add the children of x to the front of L and return to step 2.


    Introduction to Path Finding

    • A common situation of game AI

    • Path planning

      • From start position to the goal

    • Most popular technique

      • A* (A Star)

        • 1968

        • A search algorithm

        • Favorite teaching example : 15-pizzule

        • Algorithm that searches in a state space for the least costly path from start state to a goal state by examining the neighboring states


    A* Algorithm (1/4)

    The A* Algorithm

    Open : priorityqueue of searchnode

    Closed : list of searchnode

    AStarSearch( location StartLoc, location GoalLoc, agenttype Agent) {

    clear Open & Closed

    // initialize a start node

    StartNode.Loc = StartLoc;

    StartNode.CostFromStart = 0;

    StartNode.CostToGoal = PathCostEstimate(StartLoc, GoalLoc, Agent);

    StartNode.TotalCost = StartNode.CostToGoal ;

    StartNode.Parent = NULL;

    push StartNode on Open;

    // process the list until success or failure

    while Open is not empty {

    pop Node from Open // node has the lowest TotalCost


    A* Algorithm (2/4)

    // if at a goal, we’re done

    if (Node is a goal node) {

    construct a path backward from Node to StartLoc

    return SUCCESS;

    }

    else {

    for each successor NewNode of Node {

    NewCost = Node.CostFromStart + TraverseCost(Node, NewNode, Agent);

    // ignore this node if exists and no improvement

    if (NewNode is in Open or Closed) and

    (NewNode.CostFromStart <= NewCost) {

    continue;

    }

    else { // store the new or improved information

    NewNode.Parent = Node;

    NewNode.CostFromStart = NewCost;

    NewNode.CostToGoal = PathCostEstimate(NewNode.Loc, GoalLoc, Agent);

    NewNode.TotalCost = NewNode.CostFromStart + NewNode.CostToGoal;

    if (NewNode is in Closed) {

    remove NewNode from Closed

    }


    A* Algorithm (3/4)

    if (NewNode is in Open) {

    adjust NewNode’s position in Open

    }

    else {

    Push NewNode onto Open

    }

    }

    }

    }

    push Node onto Closed

    }

    }


    A* Algorithm (4/4)

    • State

      • Location

      • Neighboring states

    • Search space

      • Related to terrain format

      • Grids

      • Triangles

      • Points of visibility

    • Cost estimate

    • Path

      • Typical A* path

      • Straight path

      • Smooth path

    • Hierarchical path finding


    Search Space & Neighboring States (1/2)

    Rectangular Grid

    Triangles

    Quadtree

    • Quadtree

      • Use grid center

    • Rectangular grid

      • Use grid center

    • Triangles or convex polygons

      • Use edge mid-point

      • Use triangle center


    Search Space & Neighboring States (2/2)

    Points of Visibility

    Generalized Cylinders

    • Generalized cylinders

      • Use intersections

    • Points of visibility


    Cost Estimate

    • Cost function

      • CostFromStart

      • CostToGoal

    • Minimum cost

      • Distance traveled

      • Time of traveled

      • Movement points expended

      • Fuel consumed

      • Penalties for passing through undesired area

      • Bonuses for passing through desired area

    • Estimate

      • To goal “distance”


    Result Path

    Straight Path

    Typical A* Path

    Smooth Path


    Catmull-Rom Spline

    Output_point = p1*(-0.5u3 +u2 - 0.5u) +

    p2*(1.5u3 – 2.5u2 + 1) +

    p3*(-1.5u3 + 2u2 + 0.5u) +

    p4*(0.5u3 – 0.5u2)

    spline output_points

    p3

    p2

    p1

    p4


    Hierarchical Path Finding

    • Break the terrain for path finding to several ones hierarchically

      • Room-to-room

      • 3D layered terrain

      • Terrain LOD

    • Pros

      • Speedup the search

      • Solve the problem of layered path finding


    Path Finding Challenges

    • Moving goal

      • Do you need to find path each frame ?

    • Moving obstacles

      • Prediction Scheme

    • Complexity of the terrain

      • Hierarchical path finding

    • “Good” path


    Motion Behavior

    A Hierarchy of Motion Behavior

    • Action selection

    • Steering

    • Locomotion


    Action Selection

    • Game AI engine

      • State machine

        • Discussed in “Finite State Machine” section

      • Goals

      • Planning

      • Strategy

    • Scripting

    • Assigned by players

      • Players’ input


    Steering

    • Path determination

      • Path finding or path planning

      • Discussed in “Path Finding”

    • Behaviors

      • Seek & flee

      • Pursuit & evasion

      • Obstacle avoidance

      • Wander

      • Path following

      • Unaligned collision avoidance

    • Group steering


    Locomotion

    • Character physically-based models

    • Movement

      • Turn right, move forward, …

    • Animation

      • By artists

    • Implemented / managed by game engine


    A Simple Vehicle Model (1/2)

    • A point mass

      • Linear momentum

      • No rotational momentum

    • Parameters

      • Mass

      • Position

      • Velocity

        • Modified by applied forces

        • Max speed

          • Top speed of a vehicle

      • Max steering force

        • Self-applied

      • Orientation

        • Car

        • Aircraft


    A Simple Vehicle Model (2/2)

    • Local space

      • Origin

      • Forward

      • Up

      • Side

    • Steering forces

      • Asymmetrical

        • Thrust

        • Braking

        • Steering

    • Velocity alignment

      • No slide, spin, …

      • Turn


    Euler Integration

    • The approach :

      • Steer_force = Truncate(streer_direction, Max_force)

      • Acceleration = Steer_force / mass

      • Velocity = Truncate(Velocity + Acceleration, Max_speed)

      • Position = Position + Velocity


    Seek & Flee Behaviors

    • Pursuit to a static target

      • Steer a character toward to a target position

    • “A moth buzzing a light bulb”

    • Flee

      • Inverse of seek

    • Variants

      • Arrival

      • Pursuit to a moving target

    • Seek Steering force

      • desired_velocity = normalize(target - position)*max_speed

      • steering = desired_velocity – velocity


    Arrival Behavior

    • One of the idea : a stopping radius

      • Outside the radius, arrival is identical to seek

      • Inside the radius, the speed is ramped down to zero

        • target_offset = target – position

        • distance = length(target_offset)

        • ramped_speed = max_speed*(distance/slowing_distance)

        • clipped_speed = minimum(ramped_speed, max_speed)

        • desired_velocity = (clipped_speed/distance)*target_offset

        • steering = desired_velocity – velocity


    Pursuit & Evasion Behaviors

    • Estimate the prediction interval T

      • T = Dc

      • D = distance(pursur, quarry)

      • c = turning parameter

    • Target is moving

    • Apply seek or flee to the target’s predicted position

    • Variants

      • Offset pursuit

        • “Fly by”


    Obstacle Avoidance Behavior

    steering force

    • Use bounding sphere

    • Not collision detection

    • Probe

      • A cylinder lying along forward axis

      • Diameter = character’s bounding sphere

      • Length = speed (means Alert range)

    • Find the most threaten obstacle

      • Nearest intersected obstacle

    • Steering


    Wander Behavior

    • Random steering

    • One solution :

      • Retain steering direction state

        • Constrain steering force to the sphere surface located slightly ahead of the character

      • Make small random displacements to it each frame

        • A small sphere on sphere surface to indicate and constrain the displacement

    • Another one :

      • Perlin noise

    • Variants

      • Explore


    Path Following Behavior

    • The path

      • Spine

        • A spline or poly-line to define the path

      • Pipe

        • The tube or generated cylinder by a defined “radius”

    • Following

      • A velocity-based prediction position

        • Inside the tube

          • Do nothing about steering

        • Outside the tube

          • “Seek” to the on-path projection

    • Variants

      • Wall following

      • Containment


    Flow Field Following Behavior

    • A flow field environment is defined.

    • Virtual reality

      • Not common in games


    Unaligned Collision Avoidance Behavior

    • Turn away from possible collision

    • Predict the potential collision

      • Use bounding spheres

    • If possibly collide,

      • Apply the steering on both characters

      • Steering direction is possible collision result

        • Use “future” possible position

        • The connected line between two sphere centers


    Steering Behaviors for Groups of Characters

    • Steering behaviors determining how the character reacts to the other characters within his/her local neighborhood

    • The behaviors including :

      • Separation

      • Cohesion

      • Alignment


    The Local Neighborhood of a Character

    The Neighborhood

    • The local neighborhood is defined as :

      • A distance

      • The field-of-view

        • Angle


    Separation Behavior

    • Make a character to maintain a distance from others nearby.

      • Compute the repulsive forces within local neighborhood

        • Calculate the position vector for each nearby

        • Normalize it

        • Weight the magnitude with distance

          • 1/distance

        • Sum the result forces

        • Negate it


    Cohesion Behavior

    • Make a character to cohere with the others nearby

      • Compute the cohesive forces within local neighborhood

        • Compute the average position of the others nearby

          • Gravity center

        • Apply “Seek” to the position


    Alignment Behavior

    • Make a character to align with the others nearby

      • Compute the steering force

        • Average the together velocity of all other characters nearby

        • The result is the desired velocity

        • Correct the current velocity to the desired one with the steering force


    Flocking Behavior

    • “Boids Model of Flocks”

      • [Reynolds 87]

    • Combination of :

      • Separation steering

      • Cohesion steering

      • Alignment steering

    • For each combination including :

      • A weight for each combination

      • A distance

      • An Angle


    Leader Following Behavior

    • Follow a leader

      • Stay with the leader

        • “Pursuit” behavior (Arrival style)

      • Stay out of the leader’s way

        • Defined as “next position” with an extension

        • “Evasion” behavior when inside the above area

      • “Separation” behavior for the followers


    Behavior Conclusion

    • Wall following

    • Containment

    • Flow field following

    • Unaligned collision avoidance

    • Separation

    • Cohesion

    • Alignment

    • Flocking

    • Leader following

    • A simple vehicle model with local neighborhood

    • Common steering behaviors including :

      • Seek

      • Flee

      • Pursuit

      • Evasion

      • Offset pursuit

      • Arrival

      • Obstacle avoidance

      • Wander

      • Path following

    • Combining the above behaviors in your application


    Introduction to FSM (1/2)

    • Finite State Machine (FSM) is the most commonly used game AI technology today.

      • Simple

      • Efficient

      • Easily extensible

      • Powerful enough to handle a wide variety of situations

    • Theory (simplified)

      • A set of states, S

      • An input vocabulary, I

      • Transition function, T(s, i)

        • Map a state and an input to another state


    Introduction FSM (2/2)

    • Practical use

      • State

        • Behavior

      • Transition

        • Across states

        • Conditions

      • It’s all about driving behavior

    • Flow-chart diagram

      • UML State chart

        • Arrow

          • Transition

        • Rectangle

          • State


    An Example of FSM As a Diagram

    Monster in sight

    Gather

    Treasure

    Flee

    No monster

    Monster dead

    Cornered

    Fight


    FSM for Games

    • Character AI

    • “Decision-Action” model

    • Behavior

      • Mental state

    • Transition

      • Players’ action

      • The other characters’ actions

      • Some features in the game world


    Implement FSM

    • Code-based FSM

      • Simple Code One Up

        • Straightforward

        • Most common

      • Macro-assisted FSM Language

    • Data-Driven FSM

      • FSM Script Language


    Coding an FSM – Code Example 1

    void RunLogic(int *state)

    {

    switch(*state)

    {

    case 0: // Wander

    Wander();

    if (SeeEnemy()) *state = 1;

    if (Dead()) *state = 2;

    break;

    case 1: // Attack

    Attack();

    *state = 0;

    if (Dead()) *state = 2;

    break;

    case 2: // Dead

    SlowlyRot();

    break;

    }

    }


    Coding an FSM – Code Example 2

    void RunLogic(FSM *fsm)

    {

    // Do action based on the state and determine next input

    input = 0;

    switch(fsm->GetStateID())

    {

    case 0: // Wander

    Wander();

    if (SeeEnemy()) input = SEE_ENEMY;

    if (Dead()) input = DEAD;

    break;

    case 1: // Attack

    Attack();

    input = WANDER;

    if (Dead()) input = DEAD;

    break;

    case 2: // Dead

    SlowlyRot();

    break;

    }

    // DO state transition based on computed input

    fsm->StateTransition(input);

    }


    FSM Language Use Macros

    • Coding a state machine directly causes lack of structure

      • Going complex when FSM at their largest

    • Use macros

    • Beneficial properties

      • Structure

      • Readability

      • Debugging

    • Simplicity


    FSM Language Use Macros – An Example

    #define BeginStateMachine …

    #define State(a) …

    bool MyStateMachine::States(StateMachineEvent event,

    int state)

    {

    BeginStateMachine

    State(0)

    OnUpdate

    Wander();

    if (SeeEnemy()) SetState(1);

    if (Dead()) SetState(2);

    State(1)

    OnUpdate

    Attack();

    SetState(0);

    if (Dead()) SetState(2);

    State(2);

    OnUpdate

    RotSlowly();

    EndStateMachine

    }


    Data-Driven FSM

    • Scripting language

      • Text-based script file

      • Transformed into

        • C++

          • Integrated into source code

        • Bytecode

          • Interpreted by the game

    • Authoring

      • Compiler

      • AI editing tool

    • Game

      • FSM script engine

      • FSM interface


    Data-Driven FSM Diagram

    Authoring

    Games

    FSMs

    FSM Script

    Engine

    bytecode

    Compiler

    Artist,

    Designers, &

    Developers

    AI Editing

    Tool

    FSM Interface

    Condition &

    Action Code

    Game Engine

    Condition &

    Action

    Vocabulary


    AI Editing Tool for FSM

    • Pure text

      • Syntax ?

    • Visual graph with text

    • Used by Designers, Artists, or Developers

      • Non-programmers

    • Conditions & action vocabulary

      • SeeEnemy

      • CloseToEnemy

      • Attack


    FSM Interface

    • Facilitating the binding between vocabulary and game world

    • Glue layer that implements the condition & action vocabulary in the game world

    • Native conditions

      • SeeEnemy(), CloseToEnemy()

    • Action library

      • Attack(…)


    FSM Script Language Benefits

    • Accelerated productivity

    • Contributions from artists & designers

    • Ease of use

    • Extensibility


    Processing Models for FSMs

    • Processing the FSMs

      • Evaluate the transition conditions for current state

      • Perform any associated actions

    • When and how ?

      • Depend on the exact need of games

    • Three common FSM processing models

      • Polling

      • Event-driven

      • Multithread


    Polling Processing Model

    • Processing each FSM at regular time intervals

      • Tied to game frame rate

      • Or some desired FSM update frequency

      • Limit one state transition in a cycle

      • Give a FSM a time-bound

    • Pros

      • Straightforward

      • Easy to implement

      • Easy to debug

    • Cons

      • Inefficiency

        • Some transition are not necessary to check every frame

    • Careful design to your FSM


    Event-driven Processing Model

    • Designed to prevent from wasted FSM processing

    • An FSM is only processed when it’s relevant

    • Implementation

      • A Publish-subscribe messaging system (Observer pattern)

      • Allows the engine to send events to individual FSMs

      • An FSM subscribes only to the events that have the potential to change the current state

      • When an event is generated, the FSMs subscribed to that events are all processed

    • “As-needed” approach

      • Should be much more efficient than polling ?

    • Tricky balance for fine-grained or coarse-grained events


    Multithread Processing Model

    • Both polling & event-driven are serially processed

    • Multithread processing model

      • Each FSM is assigned to its own thread for processing

      • Game engine is running in another separate thread

      • All FSM processing is effectively concurrent and continuous

      • Communication between threads must be thread-safe

        • Using standard locking & synchronization mechanisms

    • Pros

      • FSM as an autonomous agent who can constantly and independently examine and react to his environment

    • Cons

      • Overhead when many simultaneous characters active

      • Multithreaded programming is difficult


    Interfacing with Game Engine (1/2)

    • FSMs encapsulate complex behavior logic

      • Decision, condition, action, …

    • Game engine does corresponding

      • Character animation, movements, sounds, …

    • The interface :

      • Code each action as a function

        • Need recompile if any code is changed

        • ie., FleeWolf()

      • Callbacks

        • Function pointers

        • ie., actionFunction[fleeWolf]()

      • Container method

        • actionFunctions->FleeWolf();

        • DLL


    Interfacing with Game Engine (2/2)

    class AArmyUnit : public FnCharacter

    {

    void DoAttack(…);

    }

    AArmyUnit *army;

    army->Object(…);

    army->MoveForward(dist, …);

    army->DoAttack(…);

    • Take TheFly as example:


    FSM Efficiency & Optimization

    • Two categories :

      • Time spent

      • Computational cost

    • Scheduled processing

      • Priority for each FSM

      • Different update frequency

    • Load balancing scheme

      • Collecting statistics of past performance & extrapolating

    • Time-bound for each FSM

    • Do careful design

      • At the design level

    • Level-of-detail FSMs


    Level-Of-Detail FSMs

    • Simplify the FSM when the player won’t notice the differences

      • Outside the player’s perceptual range

      • Just like the LOD technique used in 3D game engine

    • Three design keys :

      • Decide how many LOD levels

        • How much development time available ?

        • The approximation extent

      • LOD selection policy

        • The distance between the NPC with the player ?

        • If the NPC can “see” the player ?

        • Be careful the problem of “visible discontinuous behavior”

      • What kind of approximations

        • Cheaper and less accurate solution


    Extending the Basic FSM

    BeginDoAction();

    DoActions();

    EndDoAction();

    • Extending states

      • Begin-end block

    • Stacks & FSMs

      • Stack-based “history” of FSMs

        • “Remember” the sequence of states passed through

        • “Retrace” its steps at will

      • Hierarchical FSM

    • Polymorphic FSMs

    • Fuzzy State Machine

      • Combined with fuzzy logic


    A Hierarchical FSM Example

    Monster in sight

    Gather

    Treasure

    No monster

    Flee

    Monster dead

    Fight

    Cornered

    Find

    Treasure

    Active FSM

    Go To

    Treasure

    Gather

    Treasure

    Find

    Treasure

    Live

    Stack

    Take

    Treasure


    Another Hierarchical FSM Example

    Patrol

    Done

    Done

    Noise

    Investigate

    Saw Enemy

    Attack

    Saw Enemy

    Patrol

    Go to A

    Look for

    Intruders

    Go to B

    Look for

    Intruders

    noise

    noise

    Report

    Noise

    Go Over

    To Noise

    Look for

    Intruders

    False

    Alarm!

    Investigate


    More Topics in Game AI

    • Scripting

    • Goal-based planning

    • Rule-based inference engine

    • Neural network

    • References

      • Game Programming Gems

      • AI Game Programming Wisdom


    12th

    Game Physics


    Introduction to Game Physics

    • Traditional game physics

      • Particle system

      • Rigid body dynamics

      • Flexible body dynamics

    • Some state-of-art topics

      • Car physics

      • Fluid dynamics

      • Rag-doll physics

    • Physics

      • Rigid body kinematics

      • Newton’s Laws

      • Forces

      • Momentum

      • Energy


    Basic Concepts from Physics (1/2)

    • Newton’s Laws

      • 1st Law

        • “靜者恆靜,動者恆成等速度運動”

      • 2nd Law

        • F = ma = mdv/dt

      • 3rd Law

        • 作用力與反作用力

    • Forces

      • Gravity / Spring forces / Friction / Viscosity

      • Torque

        •  = r X F

      • Equilibrium


    Basic Concepts from Physics (2/2)

    • Momentum

      • Linear momentum

      • Angular momentum

      • Moment of inertia


    Particle Dynamics

    • Particles are objects with

      • Mass

      • Position

      • Velocity

      • Respond to forces

    • But no spatial extent (no size!)

      • Point mass

    • Based on Newton Laws

      • f = ma

      • x = f / m

      • v = f / m, x = v

    ..

    .

    .


    Basic Particle System (1/5)

    x

    v

    f

    m

    x

    v

    f

    m

    x

    v

    f

    m

    x

    v

    f

    m

    x

    v

    f

    m

    x

    v

    f

    m

    typedef struct {

    float m; /* mass */

    float *x; /* position */

    float *v; /* velocity */

    float *f; /* force accumulator */

    } *Particle;

    typedef struct {

    Particle *p /* array of pointers to particles */

    int n; /* number of particles */

    float t; /* simulation clock */

    } *ParticleSystem;

    states

    Particle n time


    Basic Particle System (2/5)

    /* gather states from the particles */

    void ParticleGetState(ParticleSystem p, float *dst)

    {

    int i;

    for (i = 0; i < p->n; i++) {

    *(dst++) = p->p[I]->x[0];

    *(dst++) = p->p[I]->x[1];

    *(dst++) = p->p[I]->x[2];

    *(dst++) = p->p[I]->v[0];

    *(dst++) = p->p[I]->v[1];

    *(dst++) = p->p[I]->v[2];

    }}


    Basic Particle System (3/5)

    /* scatter states into the particles */

    void ParticleSetState(ParticleSystem p, float *src)

    {

    int i;

    for (i = 0; i < p->n; i++) {

    p->p[i]->x[0] = *(src++);

    p->p[i]->x[1] = *(src++);

    p->p[i]->x[2] = *(src++);

    p->p[i]->v[0] = *(src++);

    p->p[i]->v[1] = *(src++);

    p->p[i]->v[2] = *(src++);

    }}


    Basic Particle System (4/5)

    /* calculate derivative, place in dst */

    void ParticleDerivative(ParticleSystem p, float *dst)

    {

    int i;

    ClearForce(p);

    ComputeForce(p);

    for (i = 0; i < p->n; i++) {

    *(dst++) = p->p[i]->v[0];

    *(dst++) = p->p[i]->v[1];

    *(dst++) = p->p[i]->v[2];

    *(dst++) = p->p[i]->f[0]/p->p[i]->m;

    *(dst++) = p->p[i]->f[1]/p->p[i]->m;

    *(dst++) = p->p[i]->f[2]/p->p[i]->m;

    }}


    Basic Particle System (5/5)

    /* Euler Solver */

    void EulerStep(ParticleSystem p, float DeltaT)

    {

    ParticleDeriv(p, temp1);

    ScaleVector(temp1, DeltaT);

    ParticleGetState(p, temp2);

    AddVector(temp1, temp2, temp2);

    ParticleSetState(p, temp2);

    p->t += DeltaT;

    }


    Rigid Body Dynamics

    • Mass of a body

      • Mass center

    • Force

      • Linear momentum

      • P(t) = M v(t)

      • Velocity (v)

    • Torque

      • Angular momentum

      • L(t) = I w(t)

      • Local rotation (w)

    • Inertia tensor

    • Reference

      • www-2.cs.cmu.edu/afs/cs/user/baraff/www/pbm


    Flexible Body Dynamics (1/2)

    • Particle-spring model

      • F = k x

      • Not a stress-strain model

      • Lack of Elasticity, Plasticity, & Viscous-Elasticity

      • Can be unstable


    Flexible Body Dynamics (2/2)

    • Finite element method

      • 有限元素法

      • Solver for ODE/PDE

      • Boundary conditions

      • Energy equation

      • Stress-strain model

      • Very complicated computing process

    • Conservation of energy


    Advanced Topics in Game Physics

    • Fracture mechanics (破壞力學模擬)

    • Fluid dynamics (流體力學)

    • Car dynamics (車輛動力學)

    • Rag-doll physics (人體物理模擬)


    13th

    Game FX


    Introduction to Game FX

    • Improve the visual & sound game effects

    • Includes :

      • Combat FX

      • Environment FX

      • Character FX

      • Scene FX

      • Sound FX

    • FX editor needed

      • General 3D animation tools can not do it

        • Key-frame system is not working

        • FX animation is always

          • Procedurally

          • Related to the previous frame

    • Small work but large effect


    FX Editing Tool


    Combat FX

    • During the combat

      • Weapon motion blur

      • Weapon effect

      • Skill effect

    • After the combat

      • Damage effect

    • FX editor


    Combat FX Example


    Motion Blur – Image Solution

    • Computer animation :

      • Image solution

      • Blending rendered image sequence

        • Render too many frames

        • Divide the frames

        • Average

        • Done!

    • Shader solution :

      • HDRI

        • High dynamic range image

        • Multiple pass rendering


    Motion Blur – Geometry Solution

    • In games, use transparent objects to simulate the motion blur

    • “False” motion blur

      • Tracking the motion path of the object

      • Connecting them as a triangular mesh

      • Use time-dependent semi-transparency to simulate the “blur”

      • The path can be smoothed using Catmull-Rom spline

        • Local stability of the curve


    FX Uses Texture Animation

    • Almost all game FXs use this trick

    • Geometry object on which the texture animation playing

      • Billboard

      • 3D Plate

      • Cylinder

      • Sphere

      • Revolving a cross section curve

    • Texture sequence with color-key

    • Semi-transparent textures

      • Alpha blending

        • Source color added to background


    Particle System for FXs in Combat

    • The FXs

      • Fire / exposure / smoke / dust

    • Initial value + time dependency

    • Combined with billboard FX

      • Billboard to play the texture animation

      • Particle system to calculate the motion path

    • Gravity is the major force used

    • Emitter pattern

      • Single emitter

      • Area emitter

      • Emitter on vertices


    Environment FX

    • Weather

      • Use particle system

        • Rain

        • Snow

        • Wind

    • Fog

      • Traditional fog

        • From near to far

        • Hardware standard feature

      • Volume fog

        • Layered fog

        • Use vertex shader

    • Volume lighting

      • Polygon solution

    • Day & night


    Character FX

    • Fatality

      • Case by case and need creative solutions

    • Rendering effects on skins

      • Environment mapping

      • Bump map

      • Normal map

        • Need pixel shader

      • Multiple texture map

    • Flexible body

      • Flexible body dynamics

    • Fur

      • Real-time fur rendering


    Scene FX – Sky Box

    • Use a very large box or dome-like model to surround the whole game scene

    • Use textures on the box or dome as the backdrop

    • Use multiple textures and texture coordinates animation to simulate the moving of the clouds


    Scene FX – Len’s Flare

    • Runtime calculate the position and orientation of the camera with the sun

    • Put textures to simulate the len’s flare


    Scene FX – Light Scattering

    • Atmospheric light scattering

    • Caused by dust, molecules, or water vapor

      • These can cause light to be:

        • Scattered into the line of sight (in-scattering)

        • Scattered out of the line of sight (out-scattering)

        • Absorbed altogether (absorption)

    • Skylight and sun light

    • Can be implemented by vertex shader


    Scene FX – Light Scattering Examples

    Without scattering

    With scattering


    14th

    Network Gaming


    History of Network Gaming

    • MUD

      • Multi-user Dungeons

    • Modem games

    • Match makings

      • Real-time strategy

      • FPS

    • MMORPG

      • MMOG

      • Massive Multiplayer Online Role-playing Games


    MUD

    • A large scale game with fantasy-type

    • Create your own character

      • Strength/Intelligence/Endurance/Wisdom

    • Most of the MUDs are text-based

      • MUD commands

    • Players play them simultaneously

    • Persistent worlds

      • The amount of time invested

      • Never ending

    • Competition

      • Skill & Attributes

      • Experience points

    • Ownership

      • Acquire better items

    • Society


    Match Makings

    • Arcade

      • 休閒遊戲

    • RTS

      • Real-time strategy games

      • Internet

    • FPS

      • First-personal-view shooting games

    • AI

    • Game lobby (遊戲大廳)

      • Match making

      • Ranking

      • Chatting

      • A special case

        • Xbox Live


    Xbox Live

    • Xbox games with Live features

      • Xbox is capable of networking in default.

      • An official lobby server created and maintained by Microsoft

      • Unique gamer tag in the whole world for each registered player

      • Every player in any Xbox Live game will know whether his friends are on or off in any Xbox Live game.

      • Then he/she can send the invitation to his friends for them to join the same game.

      • All Xbox Live development project must be followed and certificated by Microsoft Xbox Live guide.

      • World-wide ranking on the Xbox Live lobbies

      • Game points controlled by Microsoft

      • Voice chatting


    MMORPGs

    • Graphical MUD

      • With much more functionality

    • Persistent

    • Real-time online (即時在線)

    • Pay-for-play

    • New game gtyles :

      • RPG

      • Adventure

      • Action

      • Society

      • Messaging

      • Avatar


    Network Gaming Architecture

    • Peer-to-peer

    • Client server


    Client 2

    Client 4

    Host

    Client 1

    Client 3

    Peer-to-peer

    • The client broadcasts messages to each other clients.

    • Simple pattern

    • Easy to code

    • But need more bandwidth for each client

    • Limited players

    • Fault tolerance

    • High security risk


    Client 2

    Client 1

    Server

    Client 3

    Client Server

    • One server serves every clients

    • More complex in coding

    • Many players

    • The server needs more bandwidth

      • This means “Expensive” for hosting the servers.

    • Improved security

    • No fault tolerance

    • Dedicated server solution


    Internet Consideration

    • Limit network bandwidth

    • Unstable network environment

    • Update frame rate

      • For match making : 5-8 fps

        • Position updates

        • Inventory updates

        • Kill or killed

      • For MMOG : 1-2 fps

        • Position updates

        • Inventory updates

        • Kill or killed

        • Friend list updates

        • Chatting

        • Messaging


    Poor Networking Problems

    • Limited bandwidth

      • Data packet size is important

      • Solutions

        • Data compression

        • Lower update frame rate

    • Low update frame rate

      • Synchronization is the key

      • Solutions

        • Extrapolation

        • Prediction

        • Chasing

    • Network LOD

    • Network visibility


    Data Packet

    • Data compression

    • Encode / Decode

    • CPU time vs data size

    • Socket

    • TCP/IP & UDP


    Network Data Compression

    • Must be lossless compression ?

    • Zip ?

    • Bit, byte, short or long ?

    • Fixed-point or floating-point real number

    • Run-length compression

    • Use index or ID instead of data

    • 1 Boolean = 4 bytes


    i-1

    i

    i+1

    real path

    i+1

    i+2

    Position Extrapolation

    • Use position(i-1) & position(i) to get the next possible position (i+1)

    • No velocity data is needed


    i-1

    i

    i+1

    real path

    i+1

    i+2

    Position Prediction

    • Use current facing direction to predict next position

    • Update : (position, facing direction)


    i-1

    i

    real path

    i+1

    Position Chasing

    • Always being late one step to the master client

    • Not good for fast moving objects


    Network Visibility

    • Server will not update the position of the clients that are out of a specific range of one client.

    • Only when the outside clients move into the range of the client.

    • That means the client can not “see” the clients outside its range on the net.


    invisible

    1fps

    Me

    3fps

    5fps

    Network Level-of-details

    • Besides the network visibility, apply the network LOD to the client between clients.

    • For each client, the server should adaptively adjust the update frame rate for the other clients to the target client according to their physical distance.


    TCP/IP or UDP

    • Some game data can be lost.

      • Position for the object in each frame

      • UDP is fast but not reliable

    • Some must be not

      • Kill or not kill, killed or nor killed

      • TCP/IP

    • Hybrid data transmission ways :

      • Data-can-not-lost going through TCP

      • Data-can-lost going through UDP

      • Or implement you reliable UDP solution


    Network Security

    • Anti-hacking

      • System administration issues

      • Very important in MMORPG

    • Cheat prevention

      • Technical example:

        • A “fake” client instead of the game client to send cheating data packets.

      • Game-playing example:

        • Using the game bugs to get the improper fortune

          • Be-killed -> Dead -> Get free money -> Be-killed ->…


    Develop Tools & Solutions

    • Match-makings

      • DirectPlay in DirectX SDK

      • Socket programming

      • Middleware

        • ie., Quazal NetZ

    • Internet causal games

      • Web programming

      • C#

      • SQL programming

    • MMORPG

      • Socket programming

      • Middleware

        • Terezona from Zona

        • Eterna from Quazal

        • Butterfly.net


    MMORPG Server


    MMORPGs Features

    • Avatar

    • Levels

    • RPG game play

    • Mission

    • Chatting

    • Society & community

    • Friends

    • Combat

    • NPCs / monsters

    • Experience points

    • Extended game contents

    • Online customer services (GM)


    MMORPGs Technical Elements

    • Client-server architecture

    • Servers

    • Network bandwidth

    • Network data packet

    • Network security

    • Graphics

    • Database


    MMORPG Servers

    • Stand alone servers

    • Distributed system


    Standalone Server

    • Using a set of large servers as the game servers

    • Each server plays as a single function

    internet

    Login server

    Game play

    Community

    Database


    Distributed System - Concept

    • Distributed PC clusters

    internet

    servers

    Database servers

    servers

    Login servers

    Game play servers

    servers


    Distributed PC Clusters

    • 1-UWeb Server Based on PC Architecture

    • Two CPUs

    • Two Network IPs

    Net IP 1

    Net IP 2

    Internet

    Internal LAN

    1U server


    Distributed System - Features

    • Two IPs

    • Scalability

    • Fault tolerance

    • Load balance

    • Dynamic zoning

    • “Blade Servers”


    Distributed System - Scalability

    • Regional operation consideration

    • Cost effective

    • Flexible capacity

    Serve concurrent 500 users

    Serve concurrent 1000 users


    Distributed System – Fault Tolerance

    • All servers can not down in anytime.

    • If some one is going to failed,

      • For distributed servers, the jobs must be transferred to the other servers.

      • For standalone server, use redundant policy.


    Master server

    !

    Internal LAN

    !

    Internet

    Slave server A

    Slave server B


    Master server

    Internal LAN

    X

    Internet

    Slave server A

    Slave server B


    Redundant Policy

    !

    Server on duty

    !

    ZZZ

    Server off duty


    Redundant policy

    X

    Server off duty

    Server on duty


    Master servers

    Internal LAN

    Internet

    Slave server A

    Slave server B

    .

    .


    Distributed System – Load Balance

    • Distributed MMOGs always do the load balance by In-house approach.

    • Load ?

      • CPU bound

      • Memory bound

      • Network bandwidth

    1 server = 500 concurrent players

    10 servers = 5000 concurrent players

    X

    !It’s very important to move the jobs

    on a crowded server to another ones


    Master server

    Internal LAN

    Internet

    Slave server A

    Slave server B

    Load balance – case 1


    Master server

    !

    Internal LAN

    !

    Internet

    Slave server A

    Slave server B

    Load balance case 2


    Master server

    Internal LAN

    Internet

    Slave server A

    Slave server B

    Load balance case 2


    Zone Concept

    • A “zone” is logically a region of game happening on servers.

    • We always map the “zone” to a physical 3D scene.

    • A zone is not really a physical hardware server but a process usually.

    • A region communicates to the players directly (in the same memory block)


    Master server

    Internal LAN

    Internet

    Slave server A

    Slave server B


    Server A

    Zone X

    combating

    messaging

    NPC

    Zone Z

    messaging

    Zone Y

    transaction

    Server B


    Interaction within/between Zones (Game Play)

    • Within the zone

      • Combating

      • Chatting

      • Transaction

    • Between zones

      • Messaging

      • Transaction (remote)

      • Banking


    Server A

    Zone Z

    Zone X

    Server C

    NPC

    Zone Z

    Zone Y

    If server B is over-loaded,

    move the “Zone Y” or “Zone Z” to an available server C

    Server B


    If server A is over-loaded,

    the whole server is over-loaded

    due that server A is occupied only

    one zone.

    Thinking :

    “Can we divide the zone into

    two or more ? “

    Zone X

    Server A

    Zone Y

    Server B

    Zone Z

    Server C

    Load Balance Using Zone Moving

    • Under the concept of “zone moving”, we can move the zones between hardware servers to achieve the load balance.

    • But is this a total solution to solve the load balance ?

    • Condition :


    Dynamic Zoning

    • Dynamically adjust the zones to meet the loading requirement.

    • Moving zones between hardware

    • Divide the zone into small ones according to the load and move the smaller ones to different hardware.

    • For the players in the same game zone but running on different process or machine, the data synchronization is becoming a challenge job.


    My Suggestion about Dynamic Zoning

    • A “zone” is mapping to a physical scene or map (geometry).

    • A zone is composed by several groups.

    • A “group” is a collection of players (population)

    • All players in the same group are running in the same process.

    • Players in different group communicate between processes.

      • Inter-process communication (IPC)

      • or Networking.

    • Group is more dynamically to be managed due to the concept of population


    Zone = A 3D Scene

    • A 3D scene

      • 3D models

      • Moving objects

      • NPCs

    • Zones are physically neighboring together

    • Using portals to connect the relationship among zones.

    • Player travels between zones.

      • Logout current scene and login to another neighboring zone when stepping on the zone portal

      • Player in client will feel a little hanging during the moving


    Population Group

    • A data structure to manage the players.

    • When a player logins into one zone, he should be assigned into one available population group and got the ID.

    • Players should not be moved between population groups when he is staying in this zone.

    • A zone can be divided into several groups.

    • Groups can be created/deleted/moved by the servers for load balance management.


    P1G1

    P5G1

    P2G2

    P7G2

    P12G2

    P6G3

    P5G3

    Players in a Zone

    Zone X


    Server A

    Zone X

    Group 2

    Group 1

    When a new player

    logins into Zone X, insert

    the player into group 2 which

    has space for new comer

    Server B


    Group 3

    Server A

    Zone X

    Group 2

    Group 1

    But if there are no

    groups with available space,

    create a new group for new comers

    Server B


    Zone X (2)

    Group7

    Server A

    Zone X

    Group 6

    Group 5

    Group 4

    Group 3

    Group 2

    Group 1

    If the Zone X on Server A is full for new comer,

    duplicate Zone X on Server B and

    create a new group

    for new players

    Server B


    Zone X (2)

    Group7

    Group 8

    Server A

    Zone X

    Group 6

    Group 5

    Group 4

    Group 3

    Group 2

    Group 1

    Network communication

    Server B

    IPC


    The Challenge of Dynamic Zoning (1/2)

    • Physical terrain and models are not easy to be divided dynamically in-game.

      • If your zones are coupling with the physical scenes, to divide the geometric data in runtime needs some specific algorithms for 3D scenes

      • From my suggestion, don’t do it

      • Find a mechanism that makes the zone dividable

      • And that is the “Population Group”

    • One server can have multiple zones running.

    • One zone can run on several servers.

      • Hard to code

      • Duplicated memory on servers

      • Synchronization between servers

      • Data communication overhead

    • Game Play Sensitive


    The Challenge of Dynamic Zoning (2/2)

    • Running players in the same zone by multiple processes.

      • Duplicated memory on the same server

      • Synchronization between processes

      • Data communication between processes

    • Players’ attribute data should be classified as :

      • Frequently used between players

        • Must be duplicated between processes

      • Seldom used between players

        • Send between players when necessary

      • Locally used by player himself

    • Cross-platform server API design


  • Login