447
1 3 3 D Game D Game Programming Programming 王王王 王王王 [email protected] [email protected] 2005 2005

3 D Game Programming

Embed Size (px)

DESCRIPTION

3 D Game Programming. 王銓彰 [email protected] 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) - PowerPoint PPT Presentation

Citation preview

Page 1: 3 D Game Programming

1

33D Game D Game ProgrammingProgramming

王銓彰王銓彰[email protected]@msa.hinet.net

20052005

Page 2: 3 D Game Programming

2

Introduction to Game Development (3hr)Introduction to Game Development (3hr) Game System Analysis (3hr)Game System Analysis (3hr) The Game Main Loop (3hr)The Game Main Loop (3hr) 3D Game Engine Training (Using TheFly3D) (6hr)3D Game Engine Training (Using TheFly3D) (6hr) Game Mathematics & Geometry (3hr)Game Mathematics & Geometry (3hr) Terrain (3hr)Terrain (3hr) Character Motion (3hr)Character Motion (3hr) Game Control System (3hr)Game Control System (3hr) Advanced Scene Management System (3hr)Advanced Scene Management System (3hr) Game AI (6hr)Game AI (6hr) Game Physics (3hr)Game Physics (3hr) Game FX (3hr)Game FX (3hr) Network Gaming (3hr)Network Gaming (3hr) MMOG (3hr)MMOG (3hr) SummarySummary

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

課程大綱課程大綱

Page 3: 3 D Game Programming

3

One term projectOne term project The students are in teams.The students are in teams. Use TheFly3D 3D engine to code a Real-time 3D Game.Use TheFly3D 3D engine to code a Real-time 3D Game.

Action RPGAction RPG The teacher will provide graphics materials and setup the The teacher will provide graphics materials and setup the

game design.game design. Final examinationFinal examination Homework will be closely coupled with the term Homework will be closely coupled with the term

project.project.

課程課程要求要求

Page 4: 3 D Game Programming

4

目前目前 數位內容學院 專任講師 數位內容學院 專任講師 / / 顧問顧問 宇峻奧汀 顧問宇峻奧汀 顧問 鈊象電子 鈊象電子 3D3D 技術顧問技術顧問

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

資歷資歷 04-04 04-04 資策會網路多媒體研究所 專案顧問資策會網路多媒體研究所 專案顧問 97-097-04 4 昱泉國際股份有限公司 技術長昱泉國際股份有限公司 技術長 96-96 96-96 虛擬實境電腦動畫股份有限公司 研發經理虛擬實境電腦動畫股份有限公司 研發經理 93-96 93-96 西基電腦動畫股份有限公司 研發經理西基電腦動畫股份有限公司 研發經理 90-93 90-93 國家高速電腦中心 助理研究員國家高速電腦中心 助理研究員 89-90 89-90 台灣大學土木工程學系 台灣大學土木工程學系 CAE Lab CAE Lab 研究助理研究助理

王銓彰 王銓彰 (1/3)(1/3)

Page 5: 3 D Game Programming

5

GameGame 作品作品 CurrentCurrent

TheFly3D 3D EngineTheFly3D 3D Engine 昱泉國際昱泉國際

DragonFly 3D Game EngineDragonFly 3D Game Engine M2M2 神甲奇兵神甲奇兵 , , VRLobby, VRLobby, 天劍記天劍記

Lizard 3D Game EngineLizard 3D Game Engine 幻影特攻、笑傲江湖 幻影特攻、笑傲江湖 I & III & II 、、神鵰俠侶 神鵰俠侶 I & III & II 、、風雲、小李飛刀、風雲、小李飛刀、

笑傲江湖網路版、怪獸總動員、聖劍大陸、笑傲外傳 笑傲江湖網路版、怪獸總動員、聖劍大陸、笑傲外傳 西基電腦動畫西基電腦動畫

Ultimate FighterUltimate Fighter 11stst real-time 3D fighting game in Taiwan real-time 3D fighting game in Taiwan

王銓彰 王銓彰 (2/3)(2/3)

Page 6: 3 D Game Programming

6

專長 專長 ((Expertise)Expertise) 33D Computer GraphicsD Computer Graphics Geometric ModelingGeometric Modeling Numerical MethodsNumerical Methods Character AnimationCharacter Animation Photo-realistic RenderingPhoto-realistic Rendering Real-time ShadingReal-time Shading Volume RenderingVolume Rendering

應用領域 應用領域 ((Applications)Applications) 即時即時 33DD 遊戲開發 遊戲開發 ((Real-time 3D Game Development)Real-time 3D Game Development) 電腦動畫 電腦動畫 ((Computer Animation)Computer Animation) 虛擬實境 虛擬實境 ((Virtual Reality)Virtual Reality) 電腦輔助設計 電腦輔助設計 ((Computer-aided Design, CAD)Computer-aided Design, CAD) 科學視算 科學視算 ((Scientific Visualization)Scientific Visualization)

王銓彰 王銓彰 (3/3)(3/3)

Page 7: 3 D Game Programming

7

1st1stIntroduction toIntroduction toGame DevelopmentGame Development

Page 8: 3 D Game Programming

8

Game platformGame platform Game typesGame types Game teamGame team Game development pipelineGame development pipeline Game software systemGame software system ToolsTools

Introduction to Game DevelopmentIntroduction to Game Development

Page 9: 3 D Game Programming

9

PCPC Single playerSingle player Match MakingsMatch Makings MMOG (Massive Multi-player Online Game)MMOG (Massive Multi-player Online Game) Web-based GamesWeb-based Games

ConsoleConsole Sony PS2Sony PS2 MS XboxMS Xbox Nintendo GameCubeNintendo GameCube

ArcadeArcade MobileMobile

Nintendo GBANintendo GBA Nintendo DSNintendo DS Sony PSPSony PSP Hand-heldHand-held

Game PlatformGame Platform

Page 10: 3 D Game Programming

10

PC is designed for general office application.PC is designed for general office application. Not for entertainment purposeNot for entertainment purpose A virtual memory systemA virtual memory system

Unlimited system memoryUnlimited system memory But video memory is limited.But video memory is limited.

For frame buffers, z buffers, textures, vertices, …For frame buffers, z buffers, textures, vertices, … PCI / AGP might be a problem for performance.PCI / AGP might be a problem for performance. Open architectureOpen architecture

Hardware driver version issueHardware driver version issue Different capabilitiesDifferent capabilities Different performanceDifferent performance

Compatibility test is very important.Compatibility test is very important. Development is easy to setup.Development is easy to setup.

Visual C/C++ with DirectXVisual C/C++ with DirectX

Game Development on PCGame Development on PC

Page 11: 3 D Game Programming

11

Specific hardware designed for gamesSpecific hardware designed for games Single user OSSingle user OS Single process OSSingle process OS No hard disk drive (?)No hard disk drive (?) Closed systemClosed system Native coding environmentNative coding environment

Proprietary SDKProprietary SDK Hardware related featuresHardware related features C language with assemblyC language with assembly

Limited resourcesLimited resources Memory for everythingMemory for everything

32M for PS232M for PS2 64M for Xbox64M for Xbox

One console runs, the others do !One console runs, the others do ! Use gamepad and no keyboardUse gamepad and no keyboard

Game Development for ConsolesGame Development for Consoles

Page 12: 3 D Game Programming

12

RPG (Role playing games)RPG (Role playing games) AVG (Adventure games)AVG (Adventure games) RTS (Real-time strategy games)RTS (Real-time strategy games) FPS (First-person shooting games)FPS (First-person shooting games) RSLG (RSLG ( 戰棋戰棋 )) STGSTG SportsSports ActionAction Puzzle gamesPuzzle games Table gamesTable games MMORPGMMORPG

Massive Multiple Player Online Role Playing GamesMassive Multiple Player Online Role Playing Games

Game TypesGame Types

Page 13: 3 D Game Programming

13

開發團隊開發團隊 製作人製作人 執行製作人執行製作人 企劃團隊企劃團隊 程式團隊程式團隊 美術團隊美術團隊

行銷業務團隊行銷業務團隊 產品經理產品經理 (PM)(PM)

測試團隊測試團隊 遊戲審議委員會遊戲審議委員會

Game project approvalGame project approval 遊戲經營團隊遊戲經營團隊

線上遊戲 線上遊戲 game master (GM)game master (GM) Customer servicesCustomer services MISMIS

Game Team MembersGame Team Members

Page 14: 3 D Game Programming

14

Team leader (usually)Team leader (usually) 資源管理 資源管理 ((Resource management)Resource management) 行政管理 行政管理 ((Administration)Administration) 專案管理 專案管理 ((Project management)Project management) 向上負責 向上負責 ((Upward management)Upward management) 團隊的決策團隊的決策 風險管理風險管理

Game Producer Game Producer 遊戲製作人遊戲製作人

Page 15: 3 D Game Programming

15

專案管理執行專案管理執行 Daily Daily 運作運作

House keepingHouse keeping Meeting coordinatorMeeting coordinator Schedule checkingSchedule checking Cross-domain communicationCross-domain communication

Usually not a full-time job positionUsually not a full-time job position A position for training and becoming a producerA position for training and becoming a producer

遊戲執行製作人遊戲執行製作人

Page 16: 3 D Game Programming

16

故事設計 故事設計 ((Story telling)Story telling) 腳本設計 腳本設計 ((Scripting)Scripting) 玩法設計 玩法設計 ((Game play design)Game play design) 角色設計角色設計 ((Character design)Character design) 動作設計動作設計 ((Animation design)Animation design) 關卡設計 關卡設計 ((Level design)Level design) 特效設計特效設計 ((Effect design)Effect design) 物件設計物件設計 介面設計介面設計 ((User Interface design)User Interface design) 遊戲調適 遊戲調適 ((Game tuning)Game tuning) 數值設定 數值設定 ((Numerical setup)Numerical setup) AI AI 設計 設計 ((Game AI design)Game AI design) 音效設定 音效設定 ((Sound FX setup)Sound FX setup)

遊戲企劃 遊戲企劃 (1/2)(1/2)

Page 17: 3 D Game Programming

17

場景設定 場景設定 ((Scene setup)Scene setup) Game document writingGame document writing Game quality checkingGame quality checking

遊戲企劃 遊戲企劃 (2/2)(2/2)

Page 18: 3 D Game Programming

18

Visual setup for game designVisual setup for game design 2D setup2D setup 3D setup3D setup

Graphics design and productionGraphics design and production 場景 場景 ((Terrain)Terrain) 人物 人物 ((Character)Character) 建模 建模 ((Models)Models) 材質 材質 ((Textures)Textures) 動作 動作 ((Motion / Animation)Motion / Animation) 特效 特效 ((FX)FX) User InterfaceUser Interface 行銷支援 行銷支援 (( 封面封面 .. 海報海報 .... 等等 ))

遊戲美術遊戲美術

Page 19: 3 D Game Programming

19

遊戲程式 遊戲程式 ((Game Program) Game Program) 撰寫撰寫 遊戲開發工具 遊戲開發工具 ((Game Tools) Game Tools) 開發開發

Level editorLevel editor Scene editorScene editor FX editorFX editor Script editorScript editor Game editorGame editor

遊戲遊戲 Data exporters from 3D animation SoftwareData exporters from 3D animation Software 3dsMax / Maya / Softimage3dsMax / Maya / Softimage

Game engine developmentGame engine development Game technique researchGame technique research Online game server developmentOnline game server development

遊戲程式遊戲程式

Page 20: 3 D Game Programming

20

Basic Procedures for Game Development

Idea Proposal Production IntegrationTesting

Debug Tuning

ConceptApproval

Prototype Pre-alpha Alpha Beta Final

發想 發想 ((Idea)Idea) 提案 提案 ((Proposal)Proposal) 製作 製作 ((Production)Production) 整合 整合 ((Integration)Integration) 測試 測試 ((Testing)Testing)

除錯 除錯 ((Debug)Debug) 調適 調適 ((Tuning)Tuning)

> Concept approval

> 雛形 (prototype)

> Pre-alpha

> Alpha

> Beta

遊戲開發流程遊戲開發流程

Page 21: 3 D Game Programming

21

遊戲類型 遊戲類型 ((Game types)Game types) 遊戲世界觀 遊戲世界觀 ((Game world)Game world) 故事 故事 ((Story)Story) 遊戲特色 遊戲特色 ((Features)Features) 遊戲玩法 遊戲玩法 ((Game play)Game play) 遊戲定位 遊戲定位 ((Game product positioning)Game product positioning)

Target playerTarget player Marketing segmentation / positioningMarketing segmentation / positioning

競爭對手評估競爭對手評估 風險評估 風險評估 ((Risk)Risk)

SWOT (Strength/Weakness/Opportunity/Threat) SWOT (Strength/Weakness/Opportunity/Threat) 分析分析 產出物產出物

Concept Design Document (CDD)Concept Design Document (CDD)

遊戲發想遊戲發想 ((Concept Design)Concept Design)

Page 22: 3 D Game Programming

22

系統分析 系統分析 ((System analysis)System analysis) GDD GDD 撰寫 撰寫 ((Game design document)Game design document) MDD MDD 撰寫 撰寫 ((Media design document)Media design document) TDD TDD 撰寫 撰寫 ((Technical design document)Technical design document) 遊戲專案建立 遊戲專案建立 ((Game project)Game project)

ScheduleSchedule Milestones / Check pointsMilestones / Check points Risk managementRisk management

測試計畫書測試計畫書 團隊建立 團隊建立 ((Team building)Team building) 產出物產出物

GDDGDD MDDMDD TDDTDD The TeamThe Team

遊戲提案遊戲提案 ((Proposal)Proposal)

Page 23: 3 D Game Programming

23

美術量產製作美術量產製作 ModelingModeling TexturesTextures AnimationAnimation MotionMotion FXFX

程式開發 程式開發 ((Coding)Coding) 企劃數值設定企劃數值設定 ……

量產 量產 !!

遊戲開發遊戲開發 ((Production)Production)

Page 24: 3 D Game Programming

24

關卡串聯 關卡串聯 ((Level integration)Level integration) 數值調整 數值調整 ((Number tuning)Number tuning) 音效置入 音效置入 ((Audio)Audio) 完成所有美術完成所有美術 程式與美術結合程式與美術結合 Testing within the game teamTesting within the game team Focus group (User study)Focus group (User study)

Release some playable levels for focus group.Release some playable levels for focus group. Get the feedback from focus group to adjust the game play.Get the feedback from focus group to adjust the game play. Invited outside game players but evaluation in-houseInvited outside game players but evaluation in-house

遊戲整合遊戲整合 ((Integration)Integration)

Page 25: 3 D Game Programming

25

Alpha Alpha 測試測試 除錯 除錯 ((Debug)Debug) Make the game stableMake the game stable

Beta Beta 測試測試 數值微調數值微調 Game play Game play 微調微調 對線上遊戲而言 對線上遊戲而言 ((MMOG)MMOG)

封閉測試 封閉測試 ((Closed beta)Closed beta) Invited game playersInvited game players

開放測試 開放測試 ((Open beta)Open beta) Free for public playersFree for public players

極限測試 極限測試 ((Critical testing)Critical testing) Only for MMOGOnly for MMOG Continuously implementingContinuously implementing For serversFor servers

遊戲測試遊戲測試 ((Test)Test)

Page 26: 3 D Game Programming

26

Bug Bug 分級 分級 ((Bug Classification)Bug Classification) A BugA Bug B BugB Bug C BugC Bug S BugS Bug

PrinciplesPrinciples Bug Bug 分級從嚴分級從嚴 Tester vs DebuggerTester vs Debugger

Bug Classification

Bug Dispatch

Debug

Verify

Bug

FAQ

Y

N

?

BugsBugs

Page 27: 3 D Game Programming

27

Hardware

3D Graphics API 2D API Input Device OS API

3D Scene Mngmt 2D Sprite Gamepad NetworkAudio

Terrain Collision Character UIDynamics Sound FX

Fighting System FX System Game AI Script System

NPC System Virtual Agent Trading System Story

Game

Game PlayLayer

EngineLayer

SystemLayer

Game Software SystemGame Software System

Page 28: 3 D Game Programming

28

33D Graphics APID Graphics API DirectX 9.0 SDK – Direct3DDirectX 9.0 SDK – Direct3D

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

2.02.0 2D API2D API

DirectX 9.0 SDK - DirectMediaDirectX 9.0 SDK - DirectMedia Win32 GDIWin32 GDI

Input deviceInput device DirectX 9.0 SDK – DirectInputDirectX 9.0 SDK – DirectInput

AudioAudio DirectX 9.0 SDK – DirectSound / Direct3DSound / DirectMediaDirectX 9.0 SDK – DirectSound / Direct3DSound / DirectMedia OpenALOpenAL

System Layer – APIs (1/2)System Layer – APIs (1/2)

Page 29: 3 D Game Programming

29

OS APIOS API Win32 SDKWin32 SDK MFCMFC

NetworkNetwork DirectX 9.0 SDK – DirectPlayDirectX 9.0 SDK – DirectPlay Socket librarySocket library

System Layer – APIs (2/2)System Layer – APIs (2/2)

Page 30: 3 D Game Programming

30

3D scene management system3D scene management system Scene graphScene graph

ShadersShaders 2D sprite system2D sprite system Audio systemAudio system GamepadGamepad HotkeysHotkeys MouseMouse TimersTimers NetworkNetwork DDK interfaceDDK interface

Engine Layer (1/2)Engine Layer (1/2)

Page 31: 3 D Game Programming

31

Terrain systemTerrain system Advanced scene management systemAdvanced scene management system

Space partition techniqueSpace partition technique BSP TreeBSP Tree

OctreeOctree Character systemCharacter system

Bone-skinBone-skin Motion BlendingMotion Blending

DynamicsDynamics Particle systemParticle system Rigid-body dynamicsRigid-body dynamics

Collision detectionCollision detection Sound FXSound FX User interfaceUser interface

Engine Layer (2/2)Engine Layer (2/2)

Page 32: 3 D Game Programming

32

NPC (Non-playable characters) managementNPC (Non-playable characters) management Game AIGame AI

Path findingPath finding Finite state machine (FSM)Finite state machine (FSM) Steering behaviorSteering behavior

AvatarAvatar Combat systemCombat system FX systemFX system Script systemScript system Trading systemTrading system Number systemNumber system ……

Game Play LayerGame Play Layer

Page 33: 3 D Game Programming

33

System ToolsSystem Tools Visual C/C++Visual C/C++

.Net 2003.Net 2003 VC/C++ 7.1VC/C++ 7.1

Visual C/C++ 6.0 + SP5Visual C/C++ 6.0 + SP5 NuMega BoundsCheckerNuMega BoundsChecker

Finding memory leakingFinding memory leaking Intel vTuneIntel vTune

Finding computation performance bottlenecksFinding computation performance bottlenecks for CPUfor CPU

PIXPIX Finding graphics performance bottlenecksFinding graphics performance bottlenecks For GPUFor GPU

Game Development Tools for Programming (1/2)Game Development Tools for Programming (1/2)

Page 34: 3 D Game Programming

34

SDKsSDKs System APISystem API

Win32 SDK or MFCWin32 SDK or MFC DirectX SDKDirectX SDK or OpenGL or OpenGL Socket librarySocket library

Middleware (Game engine)Middleware (Game engine) RenderwareRenderware UnrealUnreal ……

PhysicsPhysics ODEODE

Game Development Tools for Programming (2/2)Game Development Tools for Programming (2/2)

Page 35: 3 D Game Programming

35

3D tools3D tools Discrete 3dsMaxDiscrete 3dsMax MayaMaya Softimage XSISoftimage XSI

2D tools2D tools PhotoshopPhotoshop IllustratorIllustrator

Motion toolsMotion tools Motion capture devicesMotion capture devices Motion BuilderMotion Builder FiLMBOXFiLMBOX

Game Development Tools for ArtistsGame Development Tools for Artists

Page 36: 3 D Game Programming

36

2nd2ndGame System AnalysisGame System Analysis

Page 37: 3 D Game Programming

37

The idea about system analysis (SA)The idea about system analysis (SA) Mind mappingMind mapping Case study - Term projectCase study - Term project

What Will We TalkWhat Will We Talk

Page 38: 3 D Game Programming

38

For For 程式結構 程式結構 analysisanalysis The main programThe main program Game development toolsGame development tools

To identify To identify 工作量工作量 New game engine ?New game engine ? Re-used code ?Re-used code ? Tools needed to be developed ?Tools needed to be developed ?

For For 資源 資源 managementmanagement Total man monthTotal man month How many programmers ?How many programmers ? How good the programmers ?How good the programmers ?

For job dependency analysisFor job dependency analysis

Why System Analysis (1/2)Why System Analysis (1/2)

Job A Job B

Job C

Job D

Page 39: 3 D Game Programming

39

To do technical possibility analysisTo do technical possibility analysis 技術可行性分析技術可行性分析 R&D ?R&D ? Where is the technical bottleneck ?Where is the technical bottleneck ?

Pre-processor forPre-processor for Technical design documentTechnical design document Project managementProject management

Bridge from game design to programmingBridge from game design to programming

Why System Analysis (2/2)Why System Analysis (2/2)

Page 40: 3 D Game Programming

40

No standard procedures or approachesNo standard procedures or approaches It’s not a theory.It’s not a theory.

ExperienceExperience You can use your own method/toolYou can use your own method/tool

UMLUML Mind mappingMind mapping

心智圖法心智圖法 This is the one we will use in this courseThis is the one we will use in this course

……

Something about System AnalysisSomething about System Analysis

Page 41: 3 D Game Programming

41

BrainstormingBrainstorming IntegrationIntegration Dependency analysisDependency analysis Create the projectCreate the project Technical design document (TDD) writingTechnical design document (TDD) writing

Wang’s System Analysis StepsWang’s System Analysis Steps

Page 42: 3 D Game Programming

42

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

Use mind mappingUse mind mapping IncludingIncluding

Game systemGame system Combat / Village / Puzzle / …Combat / Village / Puzzle / …

Program modulusProgram modulus Camera / PC control / NPC AI / UI / FX /…Camera / PC control / NPC AI / UI / FX /…

Tools Tools Level editor / Scene editor / …Level editor / Scene editor / …

Entities in gamesEntities in games Characters / vehicle / terrain / audio / …Characters / vehicle / terrain / audio / …

BrainstormingBrainstorming

Page 43: 3 D Game Programming

43

Confirm the resource limitationConfirm the resource limitation Technical implement possibilityTechnical implement possibility Put all related items togetherPut all related items together Man month analysisMan month analysis

How many ?How many ? Who ?Who ?

Jobs / System identificationJobs / System identification

IntegrationIntegration

Page 44: 3 D Game Programming

44

Sort the JobsSort the Jobs By job dependencyBy job dependency By programmers’ scheduleBy programmers’ schedule

Prototype for schedulingPrototype for scheduling

Dependency AnalysisDependency Analysis

Page 45: 3 D Game Programming

45

SchedulingScheduling Job assignmentJob assignment Resource allocationResource allocation Check pointsCheck points MilestonesMilestones

Major check pointsMajor check points OutputOutput Risk managementRisk management

AlternativesAlternatives

Risk management policyRisk management policy

System Analysis – Create the ProjectSystem Analysis – Create the Project

Page 46: 3 D Game Programming

46

SpecificationSpecification ResourcesResources Design in detailsDesign in details Implement methods (Implement methods ( 工法工法 )) AlgorithmsAlgorithms ““Project”Project” Output in each milestoneOutput in each milestone SOP (optional)SOP (optional) TDD TemplateTDD Template

Technical Design DocumentTechnical Design Document

Page 47: 3 D Game Programming

47

心智圖法心智圖法 A radiant thinking toolA radiant thinking tool ApplicationsApplications

讀書心得讀書心得 ProposalProposal 上課筆記上課筆記 遊記遊記 System AnalysisSystem Analysis ……

ReferenceReference ProgramsPrograms

VisioVisio MindManagerMindManager

BooksBooks Tony Buzan, Barry BuzanTony Buzan, Barry Buzan ““The Mind Map Book: How to Use Radiant Thinking to The Mind Map Book: How to Use Radiant Thinking to

Maximize Your Brain's Untapped PotentialMaximize Your Brain's Untapped Potential” ”

Mind MapMind Map

Page 48: 3 D Game Programming

48

Page 49: 3 D Game Programming

49

Page 50: 3 D Game Programming

50

Use MindManager X5 proUse MindManager X5 pro Developed by MindJetDeveloped by MindJet

Mind Map Demo Using MindManagerMind Map Demo Using MindManager

Page 51: 3 D Game Programming

51

3rd3rdThe Game Main LoopThe Game Main Loop

Page 52: 3 D Game Programming

52

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 (1/3)Win32 Application (1/3)

Page 53: 3 D Game Programming

53

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 (2/3)Win32 Application (2/3)

Page 54: 3 D Game Programming

54

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

Win32 Application (3/3)Win32 Application (3/3)

Page 55: 3 D Game Programming

55

Win32 programs are event-drivenWin32 programs are event-driven Messages = eventsMessages = events So as all windows system (for example : X window)So as all windows system (for example : X window)

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

In the loop :In the loop : Check if there are incoming events (messages)Check if there are incoming events (messages) Handle the eventsHandle the events Check the time and do something in regularCheck the time and do something in regular

Incoming events :Incoming events : InterruptsInterrupts System requestsSystem requests

Event-driven ProgrammingEvent-driven Programming

Page 56: 3 D Game Programming

56

Timers (do something in regular timing)Timers (do something in regular timing) The sub-system to handle timingThe sub-system to handle timing Must be precise to at least 1 ms or lessMust be precise to at least 1 ms or less

30fps = 1/30 second = 33.333… ms30fps = 1/30 second = 33.333… ms On win32 platform, you can use “performance counter” On win32 platform, you can use “performance counter”

instead of the win32’s “WM_TIMER” messageinstead of the win32’s “WM_TIMER” message For windows9x, WM_TIMER = 18.2 fps (maximum)For windows9x, WM_TIMER = 18.2 fps (maximum)

EventsEvents Input devicesInput devices

MouseMouse KeyboardKeyboard

Something coming from networkSomething coming from network System requestsSystem requests

Re-drawRe-draw Losing/getting the input focusLosing/getting the input focus ……

Timers & Events (1/2)Timers & Events (1/2)

Page 57: 3 D Game Programming

57

Two types of jobs (Callbacks) to do (Call) :Two types of jobs (Callbacks) to do (Call) : In regularIn regular

Timers callbacksTimers callbacks By requestsBy requests

Input device callbacksInput device callbacks So as the game main programSo as the game main program

A game is an interactive application.A game is an interactive application. MouseMouse HotkeysHotkeys GamepadsGamepads

A game is time-bound.A game is time-bound. Rendering locked in 30fps or 60fpsRendering locked in 30fps or 60fps Motion data produced in 30fpsMotion data produced in 30fps Game running in 30fpsGame running in 30fps

Timers & Events (2/2)Timers & Events (2/2)

Page 58: 3 D Game Programming

58

On PC platform : use “Performance Counter”On PC platform : use “Performance Counter” QueryPerformanceFrequency() QueryPerformanceCounter()

// timers data structuretypedef 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 (1/6)Implement the Timer (1/6)

Page 59: 3 D Game Programming

59

/*---------------------------------------------------------------------- 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 (2/6)Implement the Timer (2/6)

Page 60: 3 D Game Programming

60

/*------------------------------------ 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 (3/6)Implement the Timer (3/6)

Page 61: 3 D Game Programming

61

/*------------------------------------------------------------ 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 (4/6)Implement the Timer (4/6)

Page 62: 3 D Game Programming

62

/*--------------------- 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 (5/6)Implement the Timer (5/6)

Page 63: 3 D Game Programming

63

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

Implement the Timer (6/6)Implement the Timer (6/6)

Page 64: 3 D Game Programming

64

Single playerSingle player

Check game over

Peek player input

Implementtimer callback

Exit the loop

Rendering

Loop

y

n

Game Loop (1/2)Game Loop (1/2)

Page 65: 3 D Game Programming

65

Network clientNetwork client

Check game over

Peek user input

Receive messages

Send messages

Timer callbacks

Rendering

Exit

y

n

From network

To network

Loop

Game Loop (2/2)Game Loop (2/2)

Page 66: 3 D Game Programming

66

Check “Win/Lose”Check “Win/Lose” Check “Quit”Check “Quit” Make objects moving …Make objects moving … Play character’s motion to the next framePlay character’s motion to the next frame Play animation to the next framePlay animation to the next frame

ModelsModels TexturesTextures ……

Perform game logic calculationPerform game logic calculation Perform geometry associated calculationPerform geometry associated calculation

i.e. LODi.e. LOD Perform AI “Thinking”Perform AI “Thinking” Perform collision detectionPerform collision detection Perform the 3D renderingPerform the 3D rendering Play FXPlay FX ……

Jobs in Regular (In Timers)Jobs in Regular (In Timers)

Page 67: 3 D Game Programming

67

Mouse InputMouse Input Press/release the mouse buttonPress/release the mouse button DragDrag Double-clickDouble-click MoveMove

Keyboard InputKeyboard Input HotkeyHotkey TypingTyping

GamepadGamepad Same behavior as the hotkeySame behavior as the hotkey Except looking not like the keyboardExcept looking not like the keyboard

NetworkNetwork SystemSystem ……

Jobs By RequestJobs By Request

Page 68: 3 D Game Programming

68

4th4thTheFly3D Game EngineTheFly3D Game Engine

Page 69: 3 D Game Programming

69

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

The Main ProgramThe Main Program

Page 70: 3 D Game Programming

70

//-------------------// quit the game//-------------------void QuitGame(WORLDid gID, BYTE code, BOOL value){ if (code == FY_ESCAPE) { if (value) { FyWin32EndWorld(gID); } }}

Hotkey CallbackHotkey Callback

Page 71: 3 D Game Programming

71

/*----------------------------------------- initialize the pivot of the camera ------------------------------------------*/void InitPivot(WORLDid g, int x, int y){ oldX = x; oldY = y;}

/*------------------ 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; }}

Mouse CallbackMouse Callback

Page 72: 3 D Game Programming

72

//----------------------------------------------------------------------------------------// 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();}

The Timer CallbackThe Timer Callback

Page 73: 3 D Game Programming

73

Introduction to TheFly3DIntroduction to TheFly3D A real-time 3D graphics programming libraryA real-time 3D graphics programming library

Using C++Using C++ Cross-platformCross-platform

DirectX9.0cDirectX9.0c OpenGL 1.5OpenGL 1.5

An API for 3D graphics developersAn API for 3D graphics developers Provide a fundamental scene management systemProvide a fundamental scene management system

Scene treeScene tree Built-in visibility cullingBuilt-in visibility culling

According to the experiences from the author, some According to the experiences from the author, some game development features are added.game development features are added. CharactersCharacters Terrain systemTerrain system ……

Current version 0.8a1 (0920, 2005)Current version 0.8a1 (0920, 2005) Shader has been added in D3D versionShader has been added in D3D version

Page 74: 3 D Game Programming

74

TheFly3D in A ChartTheFly3D in A Chart

Hardware

3D Graphics API 2D API Input Device OS API

3D Scene Mngmt 2D Sprite Gamepad NetworkAudio

Terrain Collision Character UIDynamics Sound FX

Combat System FX System Game AI Script System

NPC System Virtual Agent Trading System Story

Game

Game PlayLayer

EngineLayer

SystemLayer

Developing

Developed

Page 75: 3 D Game Programming

75

Development EnvironmentDevelopment Environment .NET2003 Visual C++ 7.1.NET2003 Visual C++ 7.1 DirectX9.0c SDK (Dec, 2004)DirectX9.0c SDK (Dec, 2004) Two include files : (must!)Two include files : (must!)

TheFly.hTheFly.h TheFlyWin32.h (Win32 version + D3D)TheFlyWin32.h (Win32 version + D3D)

Linked libraries (API version)Linked libraries (API version) TheFlyLibD_08_01.libTheFlyLibD_08_01.lib d3d9.lib d3dx9.lib dsound.lib dxguid.lib winmm.libd3d9.lib d3dx9.lib dsound.lib dxguid.lib winmm.lib

Page 76: 3 D Game Programming

76

Create the Visual C++ Project for “TheFly3D”Create the Visual C++ Project for “TheFly3D” Create folders for TheFly3D APICreate folders for TheFly3D API

……\include\include ……\lib\lib

New a Win32 application projectNew a Win32 application project Set the additional include/library directories to Set the additional include/library directories to

TheFly3D APITheFly3D API Add DirectX 9.0 SDK’s include/lib to additional Add DirectX 9.0 SDK’s include/lib to additional

search directoriessearch directories Add d3d9.lib d3dx9.lib dsound.lib dxguid.lib Add d3d9.lib d3dx9.lib dsound.lib dxguid.lib

winmm.lib to additional dependencieswinmm.lib to additional dependencies Add TheFly.h, TheFlyWin32.h, TheFly3DLibD_xxxx.lib Add TheFly.h, TheFlyWin32.h, TheFly3DLibD_xxxx.lib

into the projectinto the project

Page 77: 3 D Game Programming

77

The 1The 1stst TheFly3D Program – hello.cpp TheFly3D Program – hello.cpp Create a 3D worldCreate a 3D world Create a viewportCreate a viewport Create a sceneCreate a scene Create 3D entitiesCreate 3D entities

A cameraA camera A teapot modelA teapot model A light sourceA light source

Translate the camera to show the modelTranslate the camera to show the model Bind callbacks to make the program interactiveBind callbacks to make the program interactive

Page 78: 3 D Game Programming

78

Demo - HelloDemo - Hello

Do it!

Page 79: 3 D Game Programming

79

The Basics to Write TheFly3D ProgramThe Basics to Write TheFly3D Program All Win32 code is transparent.All Win32 code is transparent.

void main(int argc, char *argv[])void main(int argc, char *argv[]) All Win32 & DirectX code are hidden within the engine.All Win32 & DirectX code are hidden within the engine.

ID & Function classID & Function class TheFly3D creates the objects for you.TheFly3D creates the objects for you.

Return the object IDReturn the object ID TheFly3D owns the objects.TheFly3D owns the objects. You have the right to handle the objects.You have the right to handle the objects.

Use function classesUse function classes No internal data structure & functions revealedNo 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);

Page 80: 3 D Game Programming

80

Initialize TheFly3DInitialize TheFly3D In general the 1In general the 1stst function to use TheFly3D is : function to use TheFly3D is :

FyWin32CreateWorld()FyWin32CreateWorld() After the calling successfully, you can get the After the calling successfully, you can get the non-non-

zerozero ID (WORLDid) of a world object. ID (WORLDid) of a world object. Assign the ID to a world function class object for Assign the ID to a world function class object for

manipulating the world.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");

Page 81: 3 D Game Programming

81

3D Graphics Layers

A World

Frame Buffers(front + back)

The World in TheFly3DThe World in TheFly3D

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

Page 82: 3 D Game Programming

82

Camera3D Models

Lights

Backdrop

Board

A 3D Scene

The 3D Graphics LayerThe 3D Graphics Layer

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

The set of the 3D objects in the view :The set of the 3D objects in the view : We call it the “We call it the “SceneScene”.”.

The projection we call the “The projection we call the “ViewportViewport””

Page 83: 3 D Game Programming

83

The Viewports & ScenesThe Viewports & Scenes

TheFly3D supports the multiple viewports.TheFly3D supports the multiple viewports. A scene can be rendered on different viewports.A scene can be rendered on different viewports. Viewports & scenes are created by the world object.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);

Page 84: 3 D Game Programming

84

The SceneThe Scene

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

A scene provides multiple rendering groups to A scene provides multiple rendering groups to handle the priority sorting for the rendering of 3D handle the priority sorting for the rendering of 3D objects.objects. There are three object lists within a rendering group.There are three object lists within a rendering group.

Invisible listInvisible list Opacity listOpacity list Semi-transparent listSemi-transparent list

The objects are rendered by the order of rendering groups.The objects are rendered by the order of rendering groups. In the same rendering group, the opaque objects are In the same rendering group, the opaque objects are

rendered first.rendered first. And the semi-transparent objects are sorted and rendered And the semi-transparent objects are sorted and rendered

from far to near…from far to near…

Page 85: 3 D Game Programming

85

The 3D Entities – ObjectsThe 3D Entities – Objects

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

An object is a An object is a carriercarrier to carry real 3D data including : to carry real 3D data including : Model geometryModel geometry Camera dataCamera data Lighting dataLighting data Terrain dataTerrain data Particle emittersParticle emitters ForcesForces AudioAudio

Objects are created/deleted by its host scene.Objects are created/deleted by its host scene. Objects can be switched between scenes.Objects can be switched between scenes. Objects should be assigned to a rendering group.Objects should be assigned to a rendering group.

Page 86: 3 D Game Programming

86

The Objects Can …The Objects Can …

Can have shapes (geometric data)Can have shapes (geometric data) Can be grouped (hierarchy)Can be grouped (hierarchy) Can move (transformation)Can move (transformation) Can look alike (clone or data sharing)Can look alike (clone or data sharing) Can perform (animation or deformation)Can perform (animation or deformation) Can be affected (lighted, listened)Can be affected (lighted, listened) Can be changed dynamically (modification)Can be changed dynamically (modification)

Page 87: 3 D Game Programming

87

Etc

Parameters

Geometric Data

Shape

Transformation Move

Parent ObjectHierarchy

Motion Data

Animation

Clone

A Scene ObjectA Scene Object

Page 88: 3 D Game Programming

88

A Model ObjectA Model Object

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

You can load the model data from files.You can load the model data from files. TheFly3D loads .cw3 model files in default.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");

Page 89: 3 D Game Programming

89

A tree-based representation Simplified scene graph

Root

TheFly3D Scene TreeTheFly3D Scene Tree

Page 90: 3 D Game Programming

90

A tree-based representation Simplified scene graph

TheFly3D Scene Tree Is Simplified Scene GraphTheFly3D Scene Tree Is Simplified Scene Graph

Root

Page 91: 3 D Game Programming

91

nID = scene.CreateObject(ROOT);cID = scene.CreateCamera(ROOT);

FnObject model;

model.Object(nID);model.SetParent(cID);

nID

cID

Object HierarchyObject Hierarchy

Page 92: 3 D Game Programming

92

OBJECTid nID = scene.CreateObject(ROOT);

FnObject model;

model.Object(nID);OBJECTid nID1 = model.Instance();

Clone a Model ObjectClone a Model Object

nID nID1

Data

instance

Page 93: 3 D Game Programming

93

TheFly3D Major Model Object Functions (1/2)TheFly3D Major Model Object Functions (1/2) void model.SetParent(parent_object_ID);void model.SetParent(parent_object_ID); void model.Show(be_show);void model.Show(be_show); void model.SetOpacity(opacity);void model.SetOpacity(opacity);

opacity = 0.0 – 1.0opacity = 0.0 – 1.0 void model.SetRenderMode(mode);void model.SetRenderMode(mode);

mode = WIREFRAME or TEXTUREmode = WIREFRAME or TEXTURE OBJECTid clonedID = model.Instance();OBJECTid clonedID = model.Instance(); void model.ChangeScene(sID);void model.ChangeScene(sID); BOOL model.Load(char *file);BOOL model.Load(char *file);

Load a .cw3 model file to a model objectLoad a .cw3 model file to a model object void model.ShowBoundingBox(beShow);void model.ShowBoundingBox(beShow); void model.Translate(x, y, z, op);void model.Translate(x, y, z, op);

Page 94: 3 D Game Programming

94

TheFly3D Major Model Object Functions (2/2)TheFly3D Major Model Object Functions (2/2) model.SetRenderOption(item, value);model.SetRenderOption(item, value);

(item, value) = (item, value) = ((Z_BUFFERZ_BUFFER, TRUE/FALSE), TRUE/FALSE) ((Z_BUFFER_WRITEZ_BUFFER_WRITE, TRUE/FALSE), TRUE/FALSE) ((ALPHAALPHA, TRUE/FALSE), TRUE/FALSE)

Add/remove the model to/from alpha sorting listAdd/remove the model to/from alpha sorting list ((FOGFOG, TRUE/FALSE), TRUE/FALSE) ((SPECULARSPECULAR, TRUE/FALSE), TRUE/FALSE) ((LIGHTINGLIGHTING, TRUE/FALSE), TRUE/FALSE) ((ANTIALIASINGANTIALIASING, TRUE, FALSE), TRUE, FALSE) ((SOURCE_BLEND_MODESOURCE_BLEND_MODE BLEND_ZERO / BLEND_ONE / BLEND_ZERO / BLEND_ONE /

BLEND_SRC_COLOR / BLEND_INV_SRC_COLOR / BLEND_SRC_COLOR / BLEND_INV_SRC_COLOR / BLEND_SRC_ALPHA / BLEND_INV_SRC_ALPHA / BLEND_SRC_ALPHA / BLEND_INV_SRC_ALPHA / BLEND_DEST_ALPHA / BLEND_INV_DEST_ALPHA / BLEND_DEST_ALPHA / BLEND_INV_DEST_ALPHA / BLEND_DEST_COLOR / BLEND_INV_DEST_COLOR / BLEND_DEST_COLOR / BLEND_INV_DEST_COLOR / BLEND_SRC_ALPHA_SAT / BLEND_BOTH_SRC_ALPHA / BLEND_SRC_ALPHA_SAT / BLEND_BOTH_SRC_ALPHA / BLEND_BOTH_INV_SRC_ALPHA)BLEND_BOTH_INV_SRC_ALPHA)

((DESTINATION_BLEND_MODEDESTINATION_BLEND_MODE values are same as the values are same as the SOURCE_BLEND_MODE)SOURCE_BLEND_MODE)

Page 95: 3 D Game Programming

95

Coordinate SystemCoordinate System Every model should have its own local coordinate Every model should have its own local coordinate

system.system. Local spaceLocal space Model spaceModel space

The space when it’s modeledThe space when it’s modeled To its parent model, it is in the global space.To its parent model, it is in the global space.

Global spaceGlobal space The space for referenceThe space for reference

World spaceWorld space The global space of the “Root”The global space of the “Root”

X

YZ

x

yz

Page 96: 3 D Game Programming

96

Rotation matrix

TransformationTransformation Three basic linear transformations used in TheFly3D.Three basic linear transformations used in TheFly3D.

TranslateTranslate RotateRotate ScaleScale

Principles :Principles : Right-handed ruleRight-handed rule v’ = v v’ = v MM00 MM11

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

a0 a1 a2 0a3 a4 a5 0a6 a7 a8 0a9 a10 a11 1Translation vector

Page 97: 3 D Game Programming

97

x’ = x + dxy’ = y + dyz’ = z + dz

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

T =

1 0 00 1 00 0 1dx dy dz

(dx dy dz)

TranslationTranslation

Page 98: 3 D Game Programming

98

RotationRotation

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

i.e. rotate with z axis

x’ = x cos – y siny’ = x sin + y cosz’ = z

Rz =

cos sin 0-sin cos 0 0 0 1 0 0 0

x

y

z

Page 99: 3 D Game Programming

99

ScalingScaling

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

x’ = x * sxy’ = y * syz’ = z * sz

T =

sx 0 00 sy 00 0 sz0 0 0

Page 100: 3 D Game Programming

100

Matrix OperationsMatrix Operations

Matrix operationMatrix operation REPLACE, LOCAL, GLOBAL REPLACE, LOCAL, GLOBAL

x

y

zX

Y

Z

[M][ML] [MG]

Object Transformation Matrix

op = REPLACE

op = GLOBAL

op = LOCAL

Page 101: 3 D Game Programming

101

TheFly3D Model Transformation FunctionsTheFly3D Model Transformation Functions model.Translate(x, y, z, op);model.Translate(x, y, z, op);

op = LOCAL, GLOBAL, or REPLACEop = LOCAL, GLOBAL, or REPLACE model.Rotate(axis, angle, op);model.Rotate(axis, angle, op);

op = LOCAL, GLOBAL, or REPLACEop = LOCAL, GLOBAL, or REPLACE axis = X_AXIS, Y_AXIS, Z_AXISaxis = X_AXIS, Y_AXIS, Z_AXIS

model.Scale(sx, sy, sz, op);model.Scale(sx, sy, sz, op); op = LOCAL, GLOBAL, or REPLACEop = LOCAL, GLOBAL, or REPLACE

model.Quaternion(w, x, y, z, op);model.Quaternion(w, x, y, z, op); w : the scalar part of the quaternionw : the scalar part of the quaternion x, y, z : the vector part of the quaternionx, y, z : the vector part of the quaternion The quaternion should be a unit quaternionThe quaternion should be a unit quaternion op = LOCAL, GLOBAL, or REPLACEop = LOCAL, GLOBAL, or REPLACE

model.SetMatrix(M12, op);model.SetMatrix(M12, op); M12 : an M12 matrixM12 : an M12 matrix op = LOCAL, GLOBAL, or REPLACEop = LOCAL, GLOBAL, or REPLACE

float *model.GetMatrix();float *model.GetMatrix(); Get the pointer of the model object’s matrixGet the pointer of the model object’s matrix

Page 102: 3 D Game Programming

102

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 …

Move forward

Move up

Move right

Turn right / left

Transformations vs MovementsTransformations vs Movements

Page 103: 3 D Game Programming

103

x

y

z

z up + facing to -y

Facing Direction and Up DirectionFacing Direction and Up Direction 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

Page 104: 3 D Game Programming

104

new position = old position + distance *(facing direction in unit)

Move Forward (1/2)Move Forward (1/2)

Page 105: 3 D Game Programming

105

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!

Move Forward (2/2)Move Forward (2/2)

FnObject model;

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

Page 106: 3 D Game Programming

106

-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 /

An example : (rotate with an arbitrary axis)

Turn Right/Left (1/2)Turn Right/Left (1/2)

Page 107: 3 D Game Programming

107

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 !

Turn Right/Left (2/2)Turn Right/Left (2/2)

FnObject model;

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

Page 108: 3 D Game Programming

108

A terrain is a place for 3D objects to walk onA terrain is a place for 3D objects to walk on A terrain can be generated from a model fileA terrain can be generated from a model file Neighboring triangles are the next searching target Neighboring triangles are the next searching target

for current triangle for terrain followingfor current triangle for terrain following A terrain for terrain following is not the same as the A terrain for terrain following is not the same as the

terrain in visualterrain in visual

TerrainTerrain

Page 109: 3 D Game Programming

109

Generate Terrain in TheFly3DGenerate Terrain in TheFly3D

// create a terrain objecttID = scene.CreateTerrain();

FnTerrain t;t.Object(tID);

// load a terrain model (just like a regular object)// but a terrain is invisible in defaultt.Load("test_terrain_following.cw3");

// generate the neighboring data for terrain following// otherwise, the terrain model is for visual onlyt.GenerateTerrainData();

Page 110: 3 D Game Programming

110

Put a Model on Terrain Using TheFly3DPut 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 terrainfloat 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);

Page 111: 3 D Game Programming

111

Terrain Following (3D)

Terrain Following (2D)

offset

offset

Terrain FollowingTerrain Following

Page 112: 3 D Game Programming

112

Probe for a Model on TerrainProbe for a Model on Terrain

probeFront

probeBack

: terrain following check point

Page 113: 3 D Game Programming

113

TheFly3D Model Movement Functions (1/3)TheFly3D Model Movement Functions (1/3) void model.GetPosition(pos)void model.GetPosition(pos)

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

void model.GetDirection(faceDir, upDir)void model.GetDirection(faceDir, upDir) If you just want to get one of the directions, just send NULL If you just want to get one of the directions, just send NULL

pointer to the one that you do not want to querypointer to the one that you do not want to query

void model.SetPosition(pos)void model.SetPosition(pos) The position is related to its parent objectThe position is related to its parent object

void model.SetDirection(faceDIr, upDir)void model.SetDirection(faceDIr, upDir) If you just want to set one of the directions, just send NULL If you just want to set one of the directions, just send NULL

pointer to the one that you do not want to setpointer to the one that you do not want to set

Void model.SetDirectionAlignment(fDAxis, uDAxis)Void model.SetDirectionAlignment(fDAxis, uDAxis) You can change the local axes for facing and up directionsYou can change the local axes for facing and up directions

Page 114: 3 D Game Programming

114

TheFly3D Model Movement Functions (2/3)TheFly3D Model Movement Functions (2/3) BOOL model.PutOnTerrain(tID, be3D, offset, BOOL model.PutOnTerrain(tID, be3D, offset,

probeFront, probeBack, probeAngle, hightLimit)probeFront, probeBack, probeAngle, hightLimit) tID is a terrain objecttID is a terrain object be3D = TRUE for 3D terrain followingbe3D = TRUE for 3D terrain following Offset is the height above the terrainOffset is the height above the terrain hightLimit is the terrain following height limithightLimit is the terrain following height limit Return TURE if you successfully put the model on a terrainReturn TURE if you successfully put the model on a terrain

int model.MoveForward(dist, beTerrainFollow, be3D, int model.MoveForward(dist, beTerrainFollow, be3D, offset)offset) If you just want to move the model forward but not on terrain, If you just want to move the model forward but not on terrain,

set beTerrainFollow to FALSEset beTerrainFollow to FALSE You should put a model on terrain first. Then you can move it You should put a model on terrain first. Then you can move it

forward on terrain.forward on terrain. Return value :Return value :

WALK (ok for moving)WALK (ok for moving) BOUNDARY (hit the terrain boundary)BOUNDARY (hit the terrain boundary) BLOCK (not implemented yet, for collision)BLOCK (not implemented yet, for collision) DO_NOTHING (something wrong …)DO_NOTHING (something wrong …)

Page 115: 3 D Game Programming

115

TheFly3D Model Movement Functions (3/3)TheFly3D Model Movement Functions (3/3) int MoveRight(dist, beTerrainFollow, be3D, offset)int MoveRight(dist, beTerrainFollow, be3D, offset)

Same as the MoveForward except moving in right-hand direction

void model.TurnRight(float angle)void model.TurnRight(float angle) Angle is in degree formatAngle is in degree format

int model.GetCurrentTerrainTriangle()int model.GetCurrentTerrainTriangle() Get current triangle ID in terrain, where the model is Get current triangle ID in terrain, where the model is

standingstanding

Page 116: 3 D Game Programming

116

A CharacterA Character

Page 117: 3 D Game Programming

117

Character with motion

Page 118: 3 D Game Programming

118

Introduction to CharactersIntroduction to Characters The characters are the actors of the games.The characters are the actors of the games. Three types of characters implemented in games :Three types of characters implemented in games :

SegmentedSegmented MeshMesh Bone-skinBone-skin

Root-base conceptRoot-base concept

Base

Production :Production : 3D animation tools3D animation tools Motion capture (MoCap)Motion capture (MoCap)

For motion dataFor motion data

Page 119: 3 D Game Programming

119

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

A Segmented CharacterA Segmented Character

BenefitsBenefits Hierarchical structureHierarchical structure Easy to implement in a scene treeEasy to implement in a scene tree

DrawbacksDrawbacks Segment-likeSegment-like

Page 120: 3 D Game Programming

120

groin

body

head

thigh_r

thigh_l

shin_r

shin_l

foot_r

foot_l

up_arm_l

up_arm_r

fore_arm_l fore_arm_r

hand_lhand_r

Base

The scene tree of a segmented character :The scene tree of a segmented character :

head

body

up arm

fore arm

handgroin

thigh

shinfoot

Page 121: 3 D Game Programming

121

Vertex animation on skinsVertex animation on skins Animated positional data on skinsAnimated positional data on skins 3D warping3D warping

A Mesh CharacterA Mesh Character

BenefitsBenefits Easy to implementEasy to implement Flexible mesh in animationFlexible mesh in animation

DrawbacksDrawbacks No hierarchyNo hierarchy Each frame is independent.Each frame is independent. Massive datasetMassive dataset

Page 122: 3 D Game Programming

122

Bone-skin skeletonBone-skin skeleton Hierarchical Hierarchical bbonesones Skin deformation Skin deformation run-timelyrun-timely

Bone A

Skin

Bone B

A Bone-skin CharacterA Bone-skin Character

BenefitsBenefits Hierarchical structureHierarchical structure Not segmented lookNot segmented look

DrawbacksDrawbacks More complicated than the other More complicated than the other

solutionssolutions Skin deformation need more CPU cost Skin deformation need more CPU cost

than transformation onlythan transformation only

Page 123: 3 D Game Programming

123

Keyframe system 3ds MAX Softimage Maya

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

Page 124: 3 D Game Programming

124

Low cost (relatively) Easy to combine animations Hand-made animations Hard to make “good” motions Long production time

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

Page 125: 3 D Game Programming

125

Motion Capture “MoCap” in short

Types : Optical Magnetic ...

Motion Production – by Motion Capture (1/2)Motion Production – by Motion Capture (1/2)

Page 126: 3 D Game Programming

126

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

Motion Production – by Motion Capture (2/2)Motion Production – by Motion Capture (2/2)

Page 127: 3 D Game Programming

127

Use root-base structure to construct the characterUse root-base structure to construct the character

Base

Root(groin)

Base

The Root-Base Concept (1/2)The Root-Base Concept (1/2)

Page 128: 3 D Game Programming

128

A character has A character has somesome models to be the geometry models to be the geometry rootsroots of the character system. of the character system.

The root plays as the gravity center of the character.The root plays as the gravity center of the character. The root can be translated and rotated.The root can be translated and rotated. The others are joints.The others are joints. The joints can rotate only.The joints can rotate only. A A ghostghost object is added to be the parent of the root, object is added to be the parent of the root,

which is the which is the basebase of the character. of the character. The The basebase is the center for the character’s movement. is the center for the character’s movement. We move the base to perform character’s moves.We move the base to perform character’s moves.

The Root-Base Concept (2/2)The Root-Base Concept (2/2)

Page 129: 3 D Game Programming

129

A set of frames to describe a character’s motionA set of frames to describe a character’s motion For examples : Walk, run, attack, …For examples : Walk, run, attack, … KKeyframed or non-keyframedeyframed or non-keyframed MMotion data inotion data in

Position (pivot) + quaternionPosition (pivot) + quaternion Position (pivot) + Euler anglesPosition (pivot) + Euler angles Position (pivot) + (q, Position (pivot) + (q, nn)) MMatrixatrix

walk run attack fall

Motion Data - PoseMotion Data - Pose

Page 130: 3 D Game Programming

130

Load a CharacterLoad a Character

FnScene scene;CHARACTERid actorID;

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

.cwc is a character description file.

Page 131: 3 D Game Programming

131

Play a PosePlay 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);

Page 132: 3 D Game Programming

132

Make a Character to Move ForwardMake a Character to Move Forward

FnCharacter actor;

// play walking poseactor.Object(actorID);actor.PlayBlendNode(CURRENT, (float) skip, LOOP, TRUE);

// move it forwardactor.MoveForward(dist, beTF, be3D, offset);

Page 133: 3 D Game Programming

133

TheFly3D Character Movement Functions (1/2)TheFly3D Character Movement Functions (1/2) void actor.GetPosition(pos)void actor.GetPosition(pos)

pos is a 3D vector to get the position of the character pos is a 3D vector to get the position of the character void actor.GetDirection(faceDir, upDir)void actor.GetDirection(faceDir, upDir)

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

void actor.SetPosition(pos)void actor.SetPosition(pos) The position is related to its parent objectThe position is related to its parent object

void actor.SetDirection(faceDIr, upDir)void actor.SetDirection(faceDIr, upDir) If you just want to set one of the directions, just send NULL If you just want to set one of the directions, just send NULL

pointer to the one that you do not want to setpointer to the one that you do not want to set BOOL actor.PutOnTerrain(tID, be3D, offset, BOOL actor.PutOnTerrain(tID, be3D, offset,

probeFront, probeBack, probeAngle, hightLimit)probeFront, probeBack, probeAngle, hightLimit) tID is a terrain objecttID is a terrain object be3D = TRUE for 3D terrain followingbe3D = TRUE for 3D terrain following Offset is the height above the terrainOffset is the height above the terrain hightLimit is the terrain following height limithightLimit is the terrain following height limit Return TURE if you successfully put the character on a Return TURE if you successfully put the character on a

terrainterrain

Page 134: 3 D Game Programming

134

TheFly3D Character Movement Functions (2/2)TheFly3D Character Movement Functions (2/2) void actor.MoveForward(dist, beTF, be3D, offset)void actor.MoveForward(dist, beTF, be3D, offset)

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

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

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

void actor.TurnRight(float angle)void actor.TurnRight(float angle) Angle is in degreeAngle is in degree A character is always using his local z-axis as up directionA character is always using his local z-axis as up direction

Page 135: 3 D Game Programming

135

TheFly3D Character FunctionsTheFly3D Character Functions OBJECTid actor.GetBaseObject()OBJECTid actor.GetBaseObject()

You can get the base object of the characterYou can get the base object of the character OBJECTidOBJECTid actor.GetObjectByName(name)actor.GetObjectByName(name)

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

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

BOOL actor.PlayFrame(frame, beIncludeBase)BOOL actor.PlayFrame(frame, beIncludeBase) The function is to play the motion for a specific frameThe function is to play the motion for a specific frame Set beIncludeBase = TRUE to play the base object’s motionSet beIncludeBase = TRUE to play the base object’s motion

BLENDTREEid actor.GetBlendTree()BLENDTREEid actor.GetBlendTree() Get the character’s blend tree dataGet the character’s blend tree data You can define all poses in a blend tree (blend nodes)You can define all poses in a blend tree (blend nodes)

float actor.PlayBlendNode(btNodeID, skipFrame, float actor.PlayBlendNode(btNodeID, skipFrame, mode, beIncludeBase)mode, beIncludeBase) All poses for a character is defined as a blend nodeAll poses for a character is defined as a blend node Skip frames can be floating-pointSkip frames can be floating-point Mode can be ONCE or LOOP!Mode can be ONCE or LOOP!

Page 136: 3 D Game Programming

136

5th5thGame MathematicsGame Mathematics

Page 137: 3 D Game Programming

137

MatricesMatrices VectorsVectors Fixed-point Real NumbersFixed-point Real Numbers Triangle MathematicsTriangle Mathematics Intersection IssuesIntersection Issues Euler AnglesEuler Angles Angular DisplacementAngular Displacement QuaternionQuaternion Differential Equation BasicsDifferential Equation Basics

Essential Mathematics for Game DevelopmentEssential Mathematics for Game Development

Page 138: 3 D Game Programming

138

Matrix basicsMatrix basics DefinitionDefinition

TransposeTranspose

AdditionAddition

Matrices (1/7)Matrices (1/7)

A = (aij) =

a11 .. a1n

. . . .am1 .. amn

C = A T cij = aji

C = A + B cij = aij + bij

Page 139: 3 D Game Programming

139

Scalar-matrix multiplicationScalar-matrix multiplication

Matrix-matrix multiplicationMatrix-matrix multiplication

C = A cij = aij

C = A B cij = aikbkj

k = 1

r

Matrices (2/7)Matrices (2/7)

Page 140: 3 D Game Programming

140

Transformations in Transformations in MatrixMatrix form form A point or a vector is a row matrix (de facto convention)A point or a vector is a row matrix (de facto convention)

V = [x y z]

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

V’ = V + DV’ = VSV’ = VR

where D is a translation vector andS and R are scaling and rotation matrices

Matrices (3/7)Matrices (3/7)

Page 141: 3 D Game Programming

141

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

V (x, y, z, w) , where w is always 1

Translation TransformationTranslation Transformation

x’ = x + Tx

y’ = y + Ty

z’ = z + Tz

V’ = VT

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

= [x y z 1] T

1 0 0 00 1 0 00 0 1 0Tx Ty Tz 1

Matrices (4/7)Matrices (4/7)

Page 142: 3 D Game Programming

142

Scaling TransformationScaling Transformation

x’ = xSx

y’ = ySy

z’ = zSz

V’ = VS

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

= [x y z 1] S

Sx 0 0 00 Sy 0 00 0 Sz 00 0 0 1

Here Sx, Sy and Sz are scaling factors.

Matrices (5/7)Matrices (5/7)

Page 143: 3 D Game Programming

143

Rotation TransformationsRotation Transformations

1 0 0 00 cos sin 00 -sin cos 00 0 0 1

Rx =

Ry =

Rz =

cos 0 -sin 0 0 1 0 0sin 0 cos 00 0 0 1

cos sin 0 0-sin cos 0 0 0 0 1 0 0 0 0 1

Matrices (6/7)Matrices (6/7)

Page 144: 3 D Game Programming

144

Net Transformation matrixNet 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

M1 M2 = M2 M1

Matrices (7/7)Matrices (7/7)

Matrix multiplication are Matrix multiplication are not commutativenot commutative

Page 145: 3 D Game Programming

145

A vector is an entity that possesses A vector is an entity that possesses magnitudemagnitude and and direcdirectiontion..

A 3D vector is a triple :A 3D vector is a triple : VV = (v = (v11, v, v22, v, v33), where each component v), where each component vii is a scalar. is a scalar.

A ray (directed line segment), that possesses A ray (directed line segment), that possesses positionposition, , magnitudemagnitude and and directiondirection..

Vectors (1/5)Vectors (1/5)

(x1,y1,z1)

(x2,y2,z2)

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

Page 146: 3 D Game Programming

146

AddAddiition of vectorstion of vectorsX = V + W = (x1, y1, z1) = (v1 + w1, v2 + w2, v3 + w3)

V

W

V + W

V

W

V + W

|V| = (v12 + v2

2 + v32)1/2

U = V / |V|

Vectors (2/5)Vectors (2/5)

Length of vectorsLength of vectors

Page 147: 3 D Game Programming

147

Cross product of vectorsCross product of vectors DefinitionDefinition

X = V X W = (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)

NpV2

V1

polygon defined by 4 points

Np = V1 X V2

Vectors (3/5)Vectors (3/5)

ApplicationApplication A normal vector to a polygon is calculated from 3 (non-A normal vector to a polygon is calculated from 3 (non-

collinear) vertices of the polygon.collinear) vertices of the polygon.

Page 148: 3 D Game Programming

148

Transformation of the normal vectorsTransformation of the normal vectors

N(X) = detJ J-1T N(x)

where X = F(x) J the Jacobian matrix, Ji(x) =

F(x)xi

"Global and Local Deformations of Solid Primitives" Alan H. BarrComputer Graphics Volume 18, Number 3 July 1984

Vectors (4/5)Vectors (4/5)

Page 149: 3 D Game Programming

149

Dot product of vectorsDot product of vectors DefinitionDefinition

|X| = V . W = v1w1 + v2w2 + v3w3

V

W

cos =V . W

|V||W|

Vectors (5/5)Vectors (5/5)

ApplicationApplication

Page 150: 3 D Game Programming

150

Fixed point arithmetics : n bits (signed) integerFixed point arithmetics : n bits (signed) integer Example : N = 16 gives range –32768 Example : N = 16 gives range –32768 ă ă 32767 32767 We can use fixed scale to get the decimals.We can use fixed scale to get the decimals.

Fixed Point Arithmetics (1/2)Fixed Point Arithmetics (1/2)

a = ă / 28

1 1 18 integer bits

8 fractional bits

ă = 315, a = 1.2305

Page 151: 3 D Game Programming

151

Multiplication then requires rescalingMultiplication then requires rescaling

Fixed Point Arithmetics (2/2)Fixed Point Arithmetics (2/2)

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

ĕ = (ă . ĉ) / 28

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

ĕ = ă + ĉ

Addition just like normal Addition just like normal

Page 152: 3 D Game Programming

152

Compression for floating-point real numbersCompression for floating-point real numbers 4 bytes reduced to 2 bytes4 bytes reduced to 2 bytes Lost some accuracy but affordableLost some accuracy but affordable

Network data transferNetwork data transfer Software 3D rendering (without hardware-assistant)Software 3D rendering (without hardware-assistant)

Fixed Point Arithmetics - ApplicationFixed Point Arithmetics - Application

Page 153: 3 D Game Programming

153

h

ha

hb

hc

Aa

Ac

Ab

h = ha + hb + hc

where A = Aa + Ab + Ac

If (Aa < 0 || Ab < 0 || Ac < 0) thanthe point is outside the triangle

“Triangular Coordinate System”

Aa Ab Ac

A A A

p

(xa,ya,za)

(xb,yb,zb)

(xc,yc,zc)

Triangular Coordinate SystemTriangular Coordinate System

Page 154: 3 D Game Programming

154

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)

Triangle Area – 2DTriangle Area – 2D

(xa,ya,za)

(xb,yb,zb)

(xc,yc,zc)

Page 155: 3 D Game Programming

155

Area of a triangle in 3D A = ½ (N. Sum(Pi1 cross Pi2)) where (i1, i2) = (a,b), (b,c), (c,a)

Triangle Area – 3DTriangle Area – 3D

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

Page 156: 3 D Game Programming

156

Terrain followingTerrain following Interpolating the height of arbitrary point within the triangleInterpolating the height of arbitrary point within the triangle

Hit testHit test Intersection of a ray from camera to a screen position with Intersection of a ray from camera to a screen position with

a trianglea triangle Ray castRay cast

Intersection of a ray with a triangleIntersection of a ray with a triangle Collision detectionCollision detection

IntersectionIntersection

Triangular Coordinate System - ApplicationTriangular Coordinate System - Application

Page 157: 3 D Game Programming

157

Ray castRay cast Containment testContainment test

IntersectionIntersection

Page 158: 3 D Game Programming

158

Ray Cast – The RayRay Cast – The Ray

x = x0 + (x1 – x0) ty = y0 + (y1 – y0) t, t = 0,z = z0 + (z1 – z0) t

{

Cast a ray to calculate the intersection of the ray with Cast a ray to calculate the intersection of the ray with modelsmodels

Use parametric equation for a rayUse parametric equation for a ray

8

When t = 0, the ray is on the start point (xWhen t = 0, the ray is on the start point (x00,y,y00,z,z00)) Only the t Only the t 0 is the answer candidate 0 is the answer candidate The smallest positive t is the answerThe smallest positive t is the answer

Page 159: 3 D Game Programming

159

Ray Cast – The PlaneRay Cast – The Plane Each triangle in the 3D models has its plane Each triangle in the 3D models has its plane

equation.equation. UUse se ax + by + cz + d = 0ax + by + cz + d = 0 as the plane equation. as the plane equation. ((a, b, c)a, b, c) is the plane normal vector. is the plane normal vector. |d||d| is the distance of the plane to origin. is the distance of the plane to origin. Substitute the ray equation into the plane.Substitute the ray equation into the plane. SSolve the olve the tt to find the intersect point. to find the intersect point. CCheck the intersect point within the triangle or not heck the intersect point within the triangle or not

by using “Triangle Area Test” (p. 155)by using “Triangle Area Test” (p. 155)

Page 160: 3 D Game Programming

160

Intersection = 1, inside

Intersection = 2, outside

Intersection = 0, outside

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

x = x0 + t y = y0 , t = 0,

{ 8

(x0, y0)

2D Containment Test2D Containment Test

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

Page 161: 3 D Game Programming

161

3D Containment Test3D Containment Test

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

Same as the 2D containment testSame as the 2D containment test

Page 162: 3 D Game Programming

162

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

X-roll, X-roll, Y-Y-roll, roll, Z-Z-rollroll

R(1, 2, ) represents an x-roll, followed by y-roll, followed by z-roll

R(1, 2, c2c3 c2s3 -s2 0 s1s2c3-c1s3 s1s2s3+c1c3 s1c2 0 c1s2c3+s1s3 c1s2s3-s1c3 c1c2 0 0 0 0 1 where si = sini and ci = cosi

Euler AnglesEuler Angles

TThere are 6 possible ways to define a rotation.here are 6 possible ways to define a rotation. 3!3!

Page 163: 3 D Game Programming

163

Interpolation happening on each angleInterpolation happening on each angle Multiple routes for interpolationMultiple routes for interpolation MMore keys for constrainsore keys for constrains

z

x

y

Rz

x

y

R

Euler Angles & InterpolationEuler Angles & Interpolation

Page 164: 3 D Game Programming

164

RR((, , nn), ), nn is the rotation axis. is the rotation axis.

n

r Rr

n

r

rv

rh

V

rv

V

Rrv

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

V = nxrv = nxr

Rrv = (cos)rv + (sin)V-> Rr = Rrh + Rrv

= rh + (cos)rv + (sin)V = (n.r)n + (cos)r - (n.r)n) + (sin) nxr = (cos)r + (1-cos) n (n.r) + (sin) nxr

Angular DisplacementAngular Displacement

Page 165: 3 D Game Programming

165

Sir William Hamilton (1843)Sir William Hamilton (1843) FFrom Complex numbers (a + rom Complex numbers (a + iib), b), i i 22 = -1 = -1 16,16,October, 1843, October, 1843, Broome BridgeBroome Bridge in in DublinDublin 1 1 realreal + 3 + 3 imaginaryimaginary = 1 = 1 quaternionquaternion qq = a + b = a + bii + c + cjj + d + dkk ii22 = = jj22 = = kk22 = -1 = -1 ijij = = kk & & jiji = - = -kk, cyclic permutation , cyclic permutation ii--jj--kk--ii qq = ( = (ss, , vv), where (), where (ss, , vv) = ) = ss + + vvxxii + + vvyyjj + + vvzzkk

QuaternionQuaternion

Page 166: 3 D Game Programming

166

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 AlgebraQuaternion Algebra

Page 167: 3 D Game Programming

167

Take a pure quaternion p = (0, r)and a unit quaternion q = (s, v) where qq = 1and 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 = (cos, sinn), |n| = 1

Rq(p) = (0, (cos2- sin2)r + 2sin2 n(n.r) + 2cossin nxr) = (0, cos2r + (1 - cos2)n(n.r) + sin2 nxr)

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

Quaternion VS Angular DisplacementQuaternion VS Angular Displacement

Page 168: 3 D Game Programming

168

1-2y2-2z2 2xy-2wz 2xz+2wy 02xy+2wz 1-2x2-2z2 2yz-2wx 02xz-2wy 2yz+2wx 1-2x2-2y2 0 0 0 0 1

q = (w,x,y,z)

Quaternion VS Rotation MatrixQuaternion VS Rotation Matrix

Page 169: 3 D Game Programming

169

M0 M1 M2 0M3 M4 M5 0M6 M7 M8 0 0 0 0 1

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]; }

Page 170: 3 D Game Programming

170

Spherical linear interpolation, Spherical linear interpolation, slerpslerp

A

B

P

t

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

sin((1 - t))

sin sinsin(t)

Quaternion InterpolationQuaternion Interpolation

Page 171: 3 D Game Programming

171

Initial value problemsInitial value problems OODEDE

Ordinary differential equationOrdinary differential equation NNumerical solutionsumerical solutions

EEuler’s methoduler’s method TThe midpoint methodhe midpoint method

Differential Equation BasicsDifferential Equation Basics

Page 172: 3 D Game Programming

172

An ODEAn ODE

x = f (x, t)

where f is a known functionx is the state of the system, x is the x’s time derivative

x & x are vectorsx(t0) = x0, initial condition

.

Start here Follow the vectors …

Initial Value ProblemsInitial Value Problems

.

.

VVector fieldector field SSolutionsolutions

SSymbolic solutionymbolic solution NNumerical solutionumerical solution

Page 173: 3 D Game Programming

173

A numerical solutionA numerical solution A simplification from A simplification from Tayler seriesTayler series

DDiscrete time steps starting with initial valueiscrete time steps starting with initial value Simple but not accurateSimple but not accurate

Bigger steps, bigger errorsBigger steps, bigger errors OO((tt22) ) errorserrors

Can be unstableCan be unstable Not even efficientNot even efficient

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

Euler’s MethodEuler’s Method

Page 174: 3 D Game Programming

174

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 stepx = t f(x, t)

b. Evaluate f at the midpointfmid = f((x+x)/2, (t+t)/2)

c. Take a step using the midpointx(t+t) = x(t) + t fmid

. ..

a

b

c

Error term

The Midpoint MethodThe Midpoint Method

Page 175: 3 D Game Programming

175

Midpoint = Midpoint = Runge-KuttaRunge-Kutta method of order 2 method of order 2 Runge-KuttaRunge-Kutta method of order 4 method of order 4

OO(h(h55))

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

The The Runge-KuttaRunge-Kutta Method Method

Page 176: 3 D Game Programming

176

DynamicsDynamics Particle systemParticle system

Game FX systemGame FX system Fluid simulationFluid simulation

Initial Value Problems - ApplicationInitial Value Problems - Application

Page 177: 3 D Game Programming

177

6th6thGame GeometryGame Geometry

Page 178: 3 D Game Programming

178

GeometryGeometry PositionPosition vertex normalsvertex normals vertex colorsvertex colors texture coordinatestexture coordinates Tangent / Bi-normalTangent / Bi-normal

TopologyTopology PrimitivePrimitive

Lines / triangles / surfaces / …Lines / triangles / surfaces / … PropertyProperty

MaterialsMaterials TexturesTextures

MotionMotion HierarchyHierarchy Level-of-detailLevel-of-detail

Game ModelsGame Models

Page 179: 3 D Game Programming

179

Vertex positionVertex position (x, y, z(x, y, z, w, w)) In model space or screen spaceIn model space or screen space

Vertex normalVertex normal (n(nxx, n, nyy, n, nzz))

Vertex colorVertex color (r, g, b) or (diffuse, specular)(r, g, b) or (diffuse, specular)

Texture coordinates on vertexTexture coordinates on vertex (u(u11, v, v11), (u), (u22, v, v22), …), …

Skin weightsSkin weights ((bonebone11, w, w11, bone, bone22, w, w22, …), …)

TTangent and bi-normalangent and bi-normal

Geometry DataGeometry Data

N

T

Bn

Page 180: 3 D Game Programming

180

LinesLines Line segmentsLine segments PolylinePolyline

Open / closedOpen / closed Indexed trianglesIndexed triangles TTriangle strips / fansriangle strips / fans SSurfacesurfaces

NNon-on-uuniform niform RRational ational BB SSpline (pline (NURBSNURBS)) SSubdivisionubdivision

Topology DataTopology Data

Page 181: 3 D Game Programming

181

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

v0

v3

v6

v7

Right-hand rule for index

polygon normal

vertex normal

Indexed TrianglesIndexed Triangles

Page 182: 3 D Game Programming

182

v0

v1

v2

v3

v4

v5

v6

v7

T0

T1

T2 T3

T4

T5

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

Triangle StripsTriangle Strips

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

Page 183: 3 D Game Programming

183

MaterialMaterial TexturesTextures ShadersShaders

Property on SurfaceProperty on Surface

Page 184: 3 D Game Programming

184

MaterialMaterial AmbientAmbient

EnvironmentEnvironment Non-lighted areaNon-lighted area

DiffuseDiffuse Dynamic lightingDynamic lighting

EmissiveEmissive Self-lightingSelf-lighting

Specular with shinenessSpecular with shineness Hi-lightHi-light View-dependentView-dependent Not good for hardware renderingNot good for hardware rendering

LLocal illuminationocal illumination

MaterialsMaterials

Page 185: 3 D Game Programming

185

TexturesTextures Single textureSingle texture Texture coordinate animationTexture coordinate animation Texture animationTexture animation Multiple texturesMultiple textures AlphamapAlphamap

TexturesTextures

Material or vertex colors

Base color texture

Lightmap

Page 186: 3 D Game Programming

186

Programmable shading languageProgrammable shading language Vertex shaderVertex shader PPixel shaderixel shader

Procedural way to implement some process of renderingProcedural way to implement some process of rendering TransformationTransformation LightingLighting TexturingTexturing BRDFBRDF RasterizationRasterization Pixel fill-inPixel fill-in PPost-processing for renderingost-processing for rendering

ShadersShaders

Page 187: 3 D Game Programming

187

Vertex Data Topology Data

ClassicTransform &

LightingVertex Shader

Clipping & Viewport Mapping

Texturing Pixel Shader

Fog

Alpha, Stencil, Depth Testing

GeometryStage

RasterizerStage

Shader PipelineShader Pipeline

Page 188: 3 D Game Programming

188

Per-pixel lightingPer-pixel lighting Motion blurMotion blur Volume / Height fogVolume / Height fog Volume linesVolume lines Depth of fieldDepth of field Fur renderingFur rendering Reflection / RefractionReflection / Refraction NPRNPR ShadowShadow Linear algebra operatorsLinear algebra operators Perlin noisePerlin noise QuaternionQuaternion Sparse matrix solversSparse matrix solvers Skin bone deformationSkin bone deformation NNormal mapormal map DDisplacement mapisplacement map PParticle shaderarticle shader

Procedural Morphing Water Simulation

Powered by ShaderPowered by Shader

Page 189: 3 D Game Programming

189

TTime-dependent dataime-dependent data TTransformation dataransformation data

PPositionosition OOrientationrientation

FormatsFormats PivotPivot Position vectorPosition vector QuaternionQuaternion Eurler anglesEurler angles Angular displacementAngular displacement

Motion DataMotion Data

Page 190: 3 D Game Programming

190

Discrete LODDiscrete LOD Switch multiple resolution models run-timelySwitch multiple resolution models run-timely

CContinuous LODontinuous LOD Use progressive mesh to dynamically reduce the rendered Use progressive mesh to dynamically reduce the rendered

polygonspolygons VView-dependent LODiew-dependent LOD

Basically for terrainBasically for terrain

Level-of-detailLevel-of-detail

Page 191: 3 D Game Programming

191

Render a model in different Level-of-Detail at run timeRender a model in different Level-of-Detail at run time User-controlledly or automatically change the User-controlledly or automatically change the

percentage of rendered verticespercentage of rendered vertices Use collapse map to control the simplification processUse collapse map to control the simplification process

Progressive MeshProgressive Mesh

Collapse map

Vertex list

Triangle list

IndexIndex 00 11 22 33 44 55 66 77 88MapMap 00 11 11 22 33 00 44 55 66

00 11 22 33 44 55 66 77 88

00 22 55 00 11 22 33 55 88

0 0 6

4

Page 192: 3 D Game Programming

192

Real-time Optimal Adapting Meshes (ROAM)Real-time Optimal Adapting Meshes (ROAM) UUse height mapse height map RRun-timely to re-construct the active (for rendering) un-timely to re-construct the active (for rendering)

geometric topology (re-mesh) to get an optimal geometric topology (re-mesh) to get an optimal mesh (polygon count) to improve the rendering mesh (polygon count) to improve the rendering performanceperformance

SSomeone calls this technique as the omeone calls this technique as the view-dependent view-dependent level-of-detaillevel-of-detail

VVery good for fly-simulation-like applicationery good for fly-simulation-like application

View-dependent LOD for Terrain - ROAMView-dependent LOD for Terrain - ROAM

Page 193: 3 D Game Programming

193

Apply progressive mesh for multi-resolution model Apply progressive mesh for multi-resolution model generationgeneration

Use in-game discrete LOD for performance tuningUse in-game discrete LOD for performance tuning WWhy ?hy ?

FFor modern game API / platform, dynamic vertex update is or modern game API / platform, dynamic vertex update is costly on performancecostly on performance

LLock video memory stall CPU/GPU performanceock video memory stall CPU/GPU performance

Level-of-detail SuggestionLevel-of-detail Suggestion

Page 194: 3 D Game Programming

194

Bounding sphere Bounding cylinder Axis-aligned bounding box (AABB) Oriented bounding box (OBB) Discrete oriented polytope (k-DOP)

Bounding Sphere AABB

OBB

k-DOP

Bounding VolumeBounding Volume

Bounding Cylinder

Page 195: 3 D Game Programming

195

Collision detectionCollision detection VVisibility cullingisibility culling HHit testit test SSteering behaviorteering behavior

IIn “Game AI” sectionn “Game AI” section

Bounding Volume - ApplicationBounding Volume - Application

Page 196: 3 D Game Programming

196

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

If the distance between two bounding spheres islarger than the sum of radius of the spheres, thanthese two objects have no chance to collide.

D > Sum(r1, r2)

D

B1

B2

c1

c2

Application Example - Bounding SphereApplication Example - Bounding Sphere

Page 197: 3 D Game Programming

197

Axis-aligned bounding box (AABB)Axis-aligned bounding box (AABB) SSimplified calculation using axis-alignment featureimplified calculation using axis-alignment feature BBut need run-timely to track the bounding boxut need run-timely to track the bounding box

AABB

Application Example - AABBApplication Example - AABB

Page 198: 3 D Game Programming

198

Oriented bounding box (OBB)Oriented bounding box (OBB) Need intersection calculation using the transformed Need intersection calculation using the transformed

OBB geometric dataOBB geometric data 3D containment test3D containment test Line intersection with planeLine intersection with plane

FFor games, or games,

OBB

Application Example - OBBApplication Example - OBB

Page 199: 3 D Game Programming

199

7th7thTerrainTerrain

Page 200: 3 D Game Programming

200

Very game type oriented dataVery game type oriented data TerrainTerrain

For visual purposeFor visual purpose Ground / Building / Static models / Dynamic modelsGround / Building / Static models / Dynamic models

For terrain followingFor terrain following Polygon meshPolygon mesh GridsGrids

For path findingFor path finding Polygon meshPolygon mesh GridsGrids

Terrain followingTerrain following Make a 3D entity (character or model) walking Make a 3D entity (character or model) walking onon terrain terrain

Path findingPath finding Find a “shortest” path to walk before movingFind a “shortest” path to walk before moving Will be taught in Game AI section.Will be taught in Game AI section. A* algorithmA* algorithm

IntroductionIntroduction

Page 201: 3 D Game Programming

201

GridGrid 2D2D QuadtreeQuadtree

Height mapHeight map Procedural height mapProcedural height map Using noise function to generate the heightUsing noise function to generate the height

Terrain FormatsTerrain Formats

Perlin Noise

ROAMROAM RReal-time eal-time OOptimally ptimally AAdapting dapting MMesheseshes

Triangular meshTriangular mesh Procedurally generatedProcedurally generated Created by artistsCreated by artists

Page 202: 3 D Game Programming

202

2D grid map2D grid map Rectangular or Hexagonal gridsRectangular or Hexagonal grids AttributesAttributes

HeightHeight Walkable or notWalkable or not Texture pattern IDTexture pattern ID

Grid MapGrid Map

Step look terrainStep look terrain

ApplicationApplication 2D games2D games 3D games with god view3D games with god view

2D tile-based game terrain2D tile-based game terrain

Page 203: 3 D Game Programming

203

Almost as same as 2D grid map but :"Almost as same as 2D grid map but :" Height on grid vertexHeight on grid vertex Only height is savedOnly height is saved Regular gridRegular grid Irregular grid but structuredIrregular grid but structured

Height MapHeight Map

Top view

ApplicationApplication As the base data structure for ROAM terrainAs the base data structure for ROAM terrain Water simulationWater simulation

Page 204: 3 D Game Programming

204

Real-time optimally adapting meshReal-time optimally adapting mesh http://http://www.llnl.govwww.llnl.gov/graphics/ROAM//graphics/ROAM/

ROAMROAM

ApplicationApplication Fly-simulationFly-simulation

Page 205: 3 D Game Programming

205

Use quad tree to construct the level-of-detail of terrainUse quad tree to construct the level-of-detail of terrain A A quad tree for LODquad tree for LOD

Chunked LOD TerrainChunked LOD Terrain

Page 206: 3 D Game Programming

206

Possibly the most popular way for 3D gamesPossibly the most popular way for 3D games GeneralGeneral Can be created by artistsCan be created by artists

Triangular MeshTriangular Mesh

Multiple-layered terrain issueMultiple-layered terrain issue

Page 207: 3 D Game Programming

207

Solve the terrain height for the object to stand on.Solve the terrain height for the object to stand on. Use the triangular coordinate system (p. 154)Use the triangular coordinate system (p. 154)

Find the next neighboring triangleFind the next neighboring triangle Half-edge data structureHalf-edge data structure

Terrain Following Using Triangular MeshTerrain Following Using Triangular Mesh

Page 208: 3 D Game Programming

208

Create cohesive relationship between triangles using Create cohesive relationship between triangles using “half edge”“half edge”

UUse half-edge table to search the neighboring trianglesse half-edge table to search the neighboring triangles

Edge = two halves

Half-edge (1/2)Half-edge (1/2)

Page 209: 3 D Game Programming

209

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

Half-edge (2/2)Half-edge (2/2)

Page 210: 3 D Game Programming

210

8th8thCharacter MotionCharacter Motion

Page 211: 3 D Game Programming

211

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

A Segmented CharacterA Segmented Character

Page 212: 3 D Game Programming

212

Vertex animation on skinsVertex animation on skins Animated positional data on skinsAnimated positional data on skins 3D warping3D warping

A Mesh CharacterA Mesh Character

Page 213: 3 D Game Programming

213

Bone-Skin SkeletonBone-Skin Skeleton Hierarchical Hierarchical bbonesones Skin deformation Skin deformation run-timelyrun-timely

Bone A

Skin

Bone B

A Bone-skin CharacterA Bone-skin Character

Page 214: 3 D Game Programming

214

Motion DataMotion Data Euler anglesEuler angles Angular displacementAngular displacement QuaternionQuaternion

Can achieve the interpolation by “Slerp”Can achieve the interpolation by “Slerp” But finally they will be converted into “matrix”But finally they will be converted into “matrix”

Page 215: 3 D Game Programming

215

Optical Motion CaptureOptical Motion Capture Device Device Data acquiredData acquired

From skin to joint (Mocap)From skin to joint (Mocap) From joint to skeleton (Post-processing)From joint to skeleton (Post-processing) From skeleton to skin (In-game)From skeleton to skin (In-game)

The shooting planThe shooting plan

Page 216: 3 D Game Programming

216

Mocap DevicesMocap Devices

Page 217: 3 D Game Programming

217

Raw Data (Positional Data)

Joint

End Point

Bio-Data

Data Acquirement During the MocapData Acquirement During the Mocap

Page 218: 3 D Game Programming

218

Skeletons Skin

Skeletons

Bone-Skin

Bone-skin Implementation In GameBone-skin Implementation In Game

Page 219: 3 D Game Programming

219

Starting out – reviewing the animation list and flowchart

Planning a Mocap ShootPlanning a Mocap Shoot

Page 220: 3 D Game Programming

220

Page 221: 3 D Game Programming

221

Creating a shot list Create a database File names Preliminary shot list

A Data Record of Shot List

Page 222: 3 D Game Programming

222

A Shoot List

Page 223: 3 D Game Programming

223

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

Page 224: 3 D Game Programming

224

A Daily Schedule

Page 225: 3 D Game Programming

225

Page 226: 3 D Game Programming

226

Page 227: 3 D Game Programming

227

Apply motion data on bonesApply motion data on bones

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

A

B

(x,y,z,,axis)

(,axis)

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

From pivot From position

Apply Motion for CharactersApply Motion for Characters

Page 228: 3 D Game Programming

228

To create more animation from limited workTo create more animation from limited work RRun-time or pre-processingun-time or pre-processing IIssues :ssues :

MMotion re-targetingotion re-targeting Run-timeRun-time

RRe-key-framinge-key-framing Pre-processingPre-processing

IInterpolation between framesnterpolation between frames Run-timeRun-time

MMotion blending otion blending Run-timeRun-time

MMotion connectionotion connection RRun-timeun-time

Motion EditingMotion Editing

Page 229: 3 D Game Programming

229

walkraw_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}

start_frame end_frame

A Pose Definition ExampleA Pose Definition Example

Page 230: 3 D Game Programming

230

walk

0 4 8

Frame 5.3

1. If the motion data is in quaternion form2. Get the motion data on frame 5 & 63. Convert the data into quaternion format4. Apply slerp(5, 6, 0.3) to get the interpolation on frame 5.35. Convert the result of step 3 into a rotation matrix6. Apply the matrix to the object for its transformation

Play a PosePlay a Pose

Page 231: 3 D Game Programming

231

Pose 1

Pose 2

cut_frame

start_frame

length

Pose ConnectionPose Connection

Page 232: 3 D Game Programming

232

Motion blending in run-timeMotion blending in run-time QQuaternion is useduaternion is used ““ Blend Tree”Blend Tree”

CCross fadeross fade CCountinuous blendingountinuous blending FFeather blendingeather blending

Pose BlendingPose Blending

Page 233: 3 D Game Programming

233

RReferenceeference GGame Developersame Developers Conference 2003 Conference 2003 PProceedings CD, Programming Trackroceedings CD, Programming Track ““AAnimation Blending : Achieving Inverse Kinematics and nimation Blending : Achieving Inverse Kinematics and

More”More” JJ erry Edsallerry Edsall MMech Warrior blend treeech Warrior blend tree

Fall Transition

ForwardMotion

FallDown

Walk

Run

Blend TreeBlend Tree

Page 234: 3 D Game Programming

234

Pose 1

Pose2

0

1

0

1

Cross FadeCross Fade

Page 235: 3 D Game Programming

235

Pose 1

Pose 2

0

1

0

1

Continuous BlendingContinuous Blending

Page 236: 3 D Game Programming

236

左右搏擊左右搏擊

Pose 1 Pose 2

Pose 3

Feather BlendingFeather Blending

Page 237: 3 D Game Programming

237

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

1-weight1-weight 2-weight2-weight N-weightN-weight

CCPU costPU cost Another way to perform the skin deformation Another way to perform the skin deformation

calculation is using vertex shadercalculation is using vertex shader

Skin DeformationSkin Deformation

Page 238: 3 D Game Programming

238

Bone A(root object)

Bone B(Bone A’s child)

base

base

1. Apply motion data to bones2. Convert the vertex from “base” space to its associated

bone’s space using the natural pose’s inverse transformation

3. Multiple the influence weight4. Accumulate all influences5. Then the vertex is deformed by the bone in “base” space

Page 239: 3 D Game Programming

239

Mb = RbTpivot

Ma = RaTposition

Mvb = Mnb-1 MbMa

Mva = Mna-1Ma

vin_base = vs*waMva + vs*wbMvb

A two-weight skin vertex example

Page 240: 3 D Game Programming

240

9th9thGame ControlGame Control

Page 241: 3 D Game Programming

241

Game Control System (1/2)Game Control System (1/2) Game control is the interface between the game and Game control is the interface between the game and

the user.the user. Game control is not only input device control but also Game control is not only input device control but also

the camera controlthe camera control Input device controlInput device control

On PCOn PC MouseMouse KeyboardKeyboard GamepadGamepad

On game consoleOn game console Gamepad buttonsGamepad buttons

0 or 2550 or 255 JoystickJoystick

0 - 2550 - 255

Page 242: 3 D Game Programming

242

Game Control System (2/2)Game Control System (2/2) Camera controlCamera control

First-personal viewFirst-personal view Third-personal viewThird-personal view God viewGod view Pre-set camera viewPre-set camera view EtcEtc

Page 243: 3 D Game Programming

243

Mouse Control (1/3)Mouse Control (1/3) Mouse is a 2D device.Mouse is a 2D device.

2-axis moving2-axis moving Related movementRelated movement 2 or 3 buttons2 or 3 buttons

Mouse can :Mouse can : MoveMove DragDrag Double-clickDouble-click

BehaviorsBehaviors Hit testHit test SelectionSelection PilotPilot

Position & orientationPosition & orientation

Page 244: 3 D Game Programming

244

Mouse Control (2/3)Mouse Control (2/3) Typical game types using mouse controlTypical game types using mouse control

Real-time strategy gamesReal-time strategy games Role Playing GameRole Playing Game

Typical game play examples :Typical game play examples : Path finding for playable characterPath finding for playable character Hitting the enemyHitting the enemy Selecting a group of unitsSelecting a group of units Orientating the camera in FPS gamesOrientating the camera in FPS games Menu selectionMenu selection ……

FeaturesFeatures Always coupling with god-view camera controlAlways coupling with god-view camera control

Viewing from the top of game worldViewing from the top of game world

Page 245: 3 D Game Programming

245

Mouse Control (3/3)Mouse Control (3/3) Easy to hand onEasy to hand on

一鼠到底一鼠到底 Slow actionSlow action

Compared with joystickCompared with joystick Value range from -32727 - 32727Value range from -32727 - 32727

Page 246: 3 D Game Programming

246

Keyboard Control (1/3)Keyboard Control (1/3)

Standard PC input deviceStandard PC input device Simulating the gamepads usuallySimulating the gamepads usually

Not every PC game player having gamepadNot every PC game player having gamepad Using keyboard as the alternative deviceUsing keyboard as the alternative device

Hotkey systemHotkey system Each key has two states.Each key has two states.

PressedPressed ReleasedReleased

256 keys256 keys BehaviorsBehaviors

Key presses/releasedKey presses/released ASCII codeASCII code

One hotkey can represent a commandOne hotkey can represent a command

Page 247: 3 D Game Programming

247

Keyboard Control (2/3)Keyboard Control (2/3) Communication toolCommunication tool

Typing messagesTyping messages Typical game types using keyboardTypical game types using keyboard

MMORPGMMORPG Needs chatting with friendsNeeds chatting with friends

Real-time strategy gamesReal-time strategy games Hotkey systemHotkey system

First-person shooting gamesFirst-person shooting games Fighting gamesFighting games

Typical game play examples :Typical game play examples : ChattingChatting Character controlsCharacter controls

Move forwardMove forward TurningTurning

Page 248: 3 D Game Programming

248

Keyboard Control (3/3)Keyboard Control (3/3) FeaturesFeatures

Shortcut for a sequence of actionsShortcut for a sequence of actions CommandsCommands Menu selectionMenu selection

But a little bit complicated for playersBut a little bit complicated for players 256 keys256 keys

Page 249: 3 D Game Programming

249

Gamepad Control (1/3)Gamepad Control (1/3) A small “keyboard” designed for game playingA small “keyboard” designed for game playing

Gamepad can map to the hotkey systemGamepad can map to the hotkey system Same behaviorsSame behaviors Less than 20 keysLess than 20 keys

Majors keys :Majors keys :

Page 250: 3 D Game Programming

250

Gamepad Control (2/3)Gamepad Control (2/3) Recent gamepad capable of two extra digital Recent gamepad capable of two extra digital

joysticksjoysticks For buttonsFor buttons

Value range : 0 or 255Value range : 0 or 255 For joystickFor joystick

Value range : 0 to 255Value range : 0 to 255 Typical game types using gamepadTypical game types using gamepad

Almost all types of games exceptAlmost all types of games except Need typingNeed typing Need large-range selection for game unitsNeed large-range selection for game units

Typical game play examples :Typical game play examples : Character controlsCharacter controls

Move forwardMove forward Turn Turn

Page 251: 3 D Game Programming

251

Gamepad Control (3/3)Gamepad Control (3/3) Combat system in a fighting gameCombat system in a fighting game

Move forwardMove forward TurnTurn

…… FeaturesFeatures

Designed for game playingDesigned for game playing Look and feelLook and feel

Easy to hand-onEasy to hand-on If you not to challenge the players’ usual practiceIf you not to challenge the players’ usual practice

Page 252: 3 D Game Programming

252

Camera ControlCamera Control TypesTypes

First-personal viewFirst-personal view Third-personal view but following the playable characterThird-personal view but following the playable character God viewGod view

FixedFixed Following the playable characterFollowing the playable character

Fixed viewFixed view Pre-rendered backgroundPre-rendered background

Pre-set viewPre-set view ……

Very sensitive to game play design & game controlVery sensitive to game play design & game control Camera control is not an independent systemCamera control is not an independent system

Page 253: 3 D Game Programming

253

God-view Camera ExampleGod-view Camera Example

Age of Empire 3

Page 254: 3 D Game Programming

254

Case Study – Third-personal View (1/6)Case Study – Third-personal View (1/6) Use arrow keys on keyboard or gamepadUse arrow keys on keyboard or gamepad

Basic key assignments :Basic key assignments : Up key to move the playable character forwardUp key to move the playable character forward Down key to turn character facing to the camera and move Down key to turn character facing to the camera and move

forwardforward Left & right keys to turn the character to left or rightLeft & right keys to turn the character to left or right

Page 255: 3 D Game Programming

255

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

The camera following the character to moveThe camera following the character to move And keeping a range of distance, a reasonable And keeping a range of distance, a reasonable

height and look-down angle with the character.height and look-down angle with the character.

Distance

PC

Camera

Height

Page 256: 3 D Game Programming

256

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

Detailed key assignments :Detailed key assignments : Up keyUp key

Turn the character facing back to the cameraTurn the character facing back to the camera Move the character forwardMove the character forward If the distance between the character and the camera is If the distance between the character and the camera is

larger a pre-set range, move the camera forward to keep larger a pre-set range, move the camera forward to keep the distance.the distance.

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

Down keyDown key Turn the character facing to the cameraTurn the character facing to the camera Move the character forwardMove the character forward The camera will move backward to keep a distance with The camera will move backward to keep a distance with

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

with the character.with the character.

Page 257: 3 D Game Programming

257

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

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

PCCamera

Page 258: 3 D Game Programming

258

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

Turn the character facing to the right of the camera.Turn the character facing to the right of the camera. Take the camera’s position as a circle center and the Take the camera’s position as a circle center and the

distance between the camera and the character as the distance between the camera and the character as the radius.radius.

Set the circle as the movement orbit.Set the circle as the movement orbit. Let the character move on the orbit.Let the character move on the orbit. When the character moving, turn the camera to right to keep When the character moving, turn the camera to right to keep

eyes on the character.eyes on the character.

Page 259: 3 D Game Programming

259

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

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

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

Reference game examples:Reference game examples: Sprinter cell 3Sprinter cell 3 PSOPSO Prince of Persia(Prince of Persia( 波斯王子波斯王子 )) The Legend of Zelda (The Legend of Zelda ( 薩爾達傳說薩爾達傳說 )) ……

Demo : 1st DCI students’ work : iRobotDemo : 1st DCI students’ work : iRobot

Page 260: 3 D Game Programming

260

10th10thAdvanced SceneAdvanced SceneManagementManagementSystemSystem

Page 261: 3 D Game Programming

261

Bounding sphere Bounding cylinder Axis-aligned bounding box (AABB) Oriented bounding box (OBB) Discrete oriented polytope (k-DOP)

Bounding Sphere AABB

OBB

k-DOP

Bounding VolumeBounding Volume

Bounding Cylinder

Page 262: 3 D Game Programming

262

Collision detectionCollision detection VVisibility cullingisibility culling HHit testit test SSteering behaviorteering behavior

IIn “Game AI” sectionn “Game AI” section

Bounding Volume - ApplicationBounding Volume - Application

Page 263: 3 D Game Programming

263

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

If the distance between two bounding spheres islarger than the sum of radius of the spheres, thanthese two objects have no chance to collide.

D > Sum(r1, r2)

D

B1

B2

c1

c2

Application Example - Bounding SphereApplication Example - Bounding Sphere

Page 264: 3 D Game Programming

264

Axis-aligned bounding box (AABB)Axis-aligned bounding box (AABB) SSimplified calculation using axis-alignment featureimplified calculation using axis-alignment feature BBut need run-timely to track the bounding boxut need run-timely to track the bounding box

AABB

Application Example - AABBApplication Example - AABB

Page 265: 3 D Game Programming

265

Oriented bounding box (OBB)Oriented bounding box (OBB) Need intersection calculation using the transformed Need intersection calculation using the transformed

OBB geometric dataOBB geometric data 3D containment test3D containment test Line intersection with planeLine intersection with plane

FFor games, or games,

OBB

Application Example - OBBApplication Example - OBB

Page 266: 3 D Game Programming

266

This is a game-type-oriented issue Bounding Volume Hierarchies (BVHs) Binary space partition trees (BSP Trees)

“Quake” Octree PVS Culling Skills

Advanced Scene GraphsAdvanced Scene Graphs

Page 267: 3 D Game Programming

267

Bounding spheres in hierarchy

R

B

Bounding Volume Hierarchies (BVHs)Bounding Volume Hierarchies (BVHs)

Page 268: 3 D Game Programming

268

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.

BSP TreeBSP Tree

Page 269: 3 D Game Programming

269

plane1

plane

0

plane2

plane

3

0

1 2

3

Axis-aligned BSP TreeAxis-aligned BSP Tree

Page 270: 3 D Game Programming

270

AB

C

D

E

F

G

A

B C

D E F G

Polygon-aligned BSP TreePolygon-aligned BSP Tree

Page 271: 3 D Game Programming

271

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)

Why BSP Tree ?Why BSP Tree ?

Page 272: 3 D Game Programming

272

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.

Octree & QuadtreeOctree & Quadtree

Page 273: 3 D Game Programming

273

Quadtree - ExampleQuadtree - Example

Page 274: 3 D Game Programming

274

Data structure coherence Apply visibility culling from parents Split or not split ? Outdoor game scene ?

Octree – Some DiscussionOctree – Some Discussion

Page 275: 3 D Game Programming

275

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 (1/2)Culling (1/2)

Page 276: 3 D Game Programming

276

eye

View frustum

Visibilityculling

Backfaceculling

Occlusionculling

Culling (2/2)Culling (2/2)

Page 277: 3 D Game Programming

277

A Pre-processorA Pre-processor Space partition the scene data from artistSpace partition the scene data from artist Generate the BSP data structureGenerate the BSP data structure Generate the PVSGenerate the PVS

BSP walk throughBSP walk through Identify the room where you areIdentify the room where you are Show/hide the rooms according to the PVSShow/hide the rooms according to the PVS

BSP ImplementationBSP Implementation

Page 278: 3 D Game Programming

278

InputInput A scene from artistA scene from artist Cutting planes (optional)Cutting planes (optional)

Can be procedurally generated by algorithmCan be procedurally generated by algorithm Cutting policyCutting policy

Split or not splitSplit or not split Ray casting resolution for PVSRay casting resolution for PVS

OutputOutput A BSP fileA BSP file

BSP TreeBSP Tree PVSPVS Geometry DataGeometry Data

BSP Preprocessor (1/2)BSP Preprocessor (1/2)

Page 279: 3 D Game Programming

279

ProcessProcess Generate the BSP tree according to the cutting policyGenerate the BSP tree according to the cutting policy Split or sort the geometry into BSP room (leaves)Split or sort the geometry into BSP room (leaves) For each “room”, ray cast all rooms to generate the possible For each “room”, ray cast all rooms to generate the possible

visible room setvisible room set 3D3D Time consumingTime consuming

BSP Preprocessor (2/2)BSP Preprocessor (2/2)

Page 280: 3 D Game Programming

280

Effectiveness of PVSEffectiveness of PVS Data setData set

Dynamic objectsDynamic objects Room sizeRoom size

BSP ChallengesBSP Challenges

Page 281: 3 D Game Programming

281

11th11thGame AIGame AI

Page 282: 3 D Game Programming

282

SearchSearch Path findingPath finding Steering behaviorSteering behavior Finite state machinesFinite state machines

ContentsContents

Page 283: 3 D Game Programming

283

What we will talk aboutWhat we will talk about Blind searchBlind search Heuristic searchHeuristic search Adversary searchAdversary search

Blind searchBlind search Breadth-first searchBreadth-first search Depth-first searchDepth-first search

Heuristic searchHeuristic search A*A*

Adversary searchAdversary search MinimaxMinimax

SearchSearch

Page 284: 3 D Game Programming

284

Using tree diagram (usually) to describe a Using tree diagram (usually) to describe a search problemsearch problem Search startsSearch starts

NodeNode i i GoalGoal

Goal node Goal node gg SuccessorsSuccessors

cc11, , cc22, , cc33…… DepthDepth

dd = = 00, , 11, , 22, …, … Search problemSearch problem

InputInput Description of the initial and goal nodesDescription of the initial and goal nodes A procedure that produces the successors of an arbitrary A procedure that produces the successors of an arbitrary

nodenode OutputOutput

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

Introduction to SearchIntroduction to Search

i

g

c1 c2 c3

d = 1

d = 2

d = 3

d = 4

Page 285: 3 D Game Programming

285

Game playingGame playing ChessChess BackgammonBackgammon

Search Examples in Traditional AISearch Examples in Traditional AI

Finding a path to goalFinding a path to goal The towers of HanoiThe towers of Hanoi Sliding tile puzzlesSliding tile puzzles

8 puzzles8 puzzles

Simply finding a goalSimply finding a goal n-queensn-queens

Page 286: 3 D Game Programming

286

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

2.2. If If LL is empty, failed. Otherwise, pick a node is empty, failed. Otherwise, pick a node nn from from LL..

3.3. If If nn is the goal node, stop and return it and is the goal node, stop and return it and the path from the initial node to the path from the initial node to nn

4.4. Otherwise, remove Otherwise, remove nn from from LL and add to and add to LL all all of of nn’s children, labeling each with its path ’s children, labeling each with its path from the initial node.from the initial node.

5.5. Return to step 2Return to step 2

Search AlgorithmSearch Algorithm

Page 287: 3 D Game Programming

287

Always exploring the child of the most recently Always exploring the child of the most recently expanded nodeexpanded node

Terminal nodes being examined from left to rightTerminal nodes being examined from left to right If the node has no children, the procedure backs If the node has no children, the procedure backs

up a minimum amount before choosing another up a minimum amount before choosing another node to examine.node to examine.

Depth-First Search (1/3)Depth-First Search (1/3)

1

g

2 8 9

d = 1

d = 2

d = 3

d = 4

3

45 6

710

11

Page 288: 3 D Game Programming

288

We stop the search when we select the goal node We stop the search when we select the goal node gg.. Depth-first search can be implemented by pushing Depth-first search can be implemented by pushing

the children of a given node onto the front of the list the children of a given node onto the front of the list LL in step 4 of page 6 in step 4 of page 6

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

Depth-First Search (2/3)Depth-First Search (2/3)

Page 289: 3 D Game Programming

289

AlgorithmAlgorithm Set Set LL to be a list of the initial nodes in the problem to be a list of the initial nodes in the problem Let Let nn be the first node on be the first node on LL. If . If LL is empty, fail. is empty, fail. If If nn is a goal node, stop and return it and path from the initial is a goal node, stop and return it and path from the initial

node to node to nn.. Otherwise, remove Otherwise, remove nn from from LL and add to the and add to the frontfront of of LL all of all of nn’s ’s

children, labelling each with its path from the initial node. children, labelling each with its path from the initial node. Return to step 2. Return to step 2.

Depth-First Search (3/3)Depth-First Search (3/3)

Page 290: 3 D Game Programming

290

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

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

Breadth-First Search (1/2)Breadth-First Search (1/2)

1

g

2 3 4

d = 1

d = 2

d = 3

d = 4

5

910 11

67

12

8

Page 291: 3 D Game Programming

291

AlgorithmAlgorithm Set Set LL to be a list of the initial nodes in the problem to be a list of the initial nodes in the problem Let Let nn be the first node on be the first node on LL. If . If LL is empty, fail. is empty, fail. If If nn is a goal node, stop and return it and path from the initial is a goal node, stop and return it and path from the initial

node to node to nn.. Otherwise, remove Otherwise, remove nn from from LL and add to the and add to the endend of of LL all of all of nn’s ’s

children, labeling each with its path from the initial node. children, labeling each with its path from the initial node. Return to step 2. Return to step 2.

Breadth-First Search (2/2)Breadth-First Search (2/2)

Page 292: 3 D Game Programming

292

Neither depth-first nor breadth-first searchNeither depth-first nor breadth-first search Exploring the tree in anything resembling an optimal order.Exploring the tree in anything resembling an optimal order. Minimizing the cost to solve the problemMinimizing the cost to solve the problem

Heuristic Search (1/2)Heuristic Search (1/2)

1

g

2

d = 1

d = 2

d = 3

d = 4

3

4

Page 293: 3 D Game Programming

293

When we picking a node from the list When we picking a node from the list LL in step 2 of search procedure, in step 2 of search procedure, what we will do is to remove steadily from the root node toward the goal 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.by always selecting a node that is as close to the goal as possible. Estimated by distance and minimizing the cost?Estimated by distance and minimizing the cost?

A* !A* !

Heuristic Search (2/2)Heuristic Search (2/2)

Page 294: 3 D Game Programming

294

AssumptionsAssumptions Two-person games in which the players alternate Two-person games in which the players alternate

moves.moves. They are games of “perfect” information, where the They are games of “perfect” information, where the

knowledge available to each player is the same.knowledge available to each player is the same. Examples :Examples :

Tic-tac-toeTic-tac-toe CheckersCheckers ChessChess GoGo OthelloOthello BackgammonBackgammon

Imperfect informationImperfect information PokersPokers BridgeBridge

Adversary SearchAdversary Search

Page 295: 3 D Game Programming

295

Minimax (1/6)Minimax (1/6)

a

e

b c d

f g

h i j k

l m n o

p q r

max

min

max

min

max

min

-1 1 -1

1

1

-11

-1

1

-1

“ ply”Nodes with the maximizerto move are square; nodeswith the minimizer to moveare circles

Maximizer to achieve theOutcome of 1; minimizer to Achieve the outcome of -1

1

-1

Page 296: 3 D Game Programming

296

Minimax (2/6)Minimax (2/6)

a

e

b c d

f g

h i j k

l m n o

p q r

max

min

max

min

max

min

-1 1 -1

1

1

-11

-1

1

-1

The maximizer wins!

1

1

1

1

1-1

-1

-1

Page 297: 3 D Game Programming

297

Basic ideaBasic idea

1.1. Expand the entire tree below Expand the entire tree below nn

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

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

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

Minimax (3/6)Minimax (3/6)

Page 298: 3 D Game Programming

298

The algorithmThe algorithm

1.1. Set Set LL = { = { nn }, the unexpanded nodes in the tree }, the unexpanded nodes in the tree

2.2. Let Let xx be the 1st node on be the 1st node on LL. If . If xx = = nn and there is a value and there is a value assigned to it, return this value.assigned to it, return this value.

3.3. If If xx has been assigned a value has been assigned a value vvxx, let , let pp be the parent of be the parent of xx and and vvpp the value currently assigned to the value currently assigned to pp. If . If pp is a minimizing is a minimizing node, set node, set vvpp = min( = min(vvpp, , vvxx). If ). If pp is a maximizing node, set is a maximizing node, set vvpp = max(= max(vvpp, , vvxx). Remove ). Remove xx from from L L and return to step 2.and return to step 2.

4.4. If If xx has not been assigned a value and is a terminal node, 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 assign it the value 1 or -1 depending on whether it is a win for the maximizer or minimizer respectively. Assign for the maximizer or minimizer respectively. Assign xx the the value 0 if the position is a draw. Leave value 0 if the position is a draw. Leave xx on on LL and return to and return to step 2.step 2.

5.5. If If xx has not been assigned a value and is a nonterminal has not been assigned a value and is a nonterminal node, set node, set vvxx to be – to be –∞∞ if if xx is a maximizing node and + is a maximizing node and + ∞∞ if if xx is a minimizing node. Add the children of is a minimizing node. Add the children of xx to the front of to the front of LL and return to step 2.and return to step 2.

Minimax (4/6)Minimax (4/6)

Page 299: 3 D Game Programming

299

Some issuesSome issues DrawDraw Estimated value e(n)Estimated value e(n)

e(n) = 1 : the node is a win for maximizere(n) = 1 : the node is a win for maximizer e(n) = -1 : the node is a win for minimizere(n) = -1 : the node is a win for minimizer e(n) = 0 : that is a drawe(n) = 0 : that is a draw e(n) = -1 ~ 1 : the otherse(n) = -1 ~ 1 : the others

When to decide stop the tree expanding further ?When to decide stop the tree expanding further ?

Minimax (5/6)Minimax (5/6)

Page 300: 3 D Game Programming

300

Minimax (6/6)Minimax (6/6) The algorithm (final)The algorithm (final)

1.1. Set Set LL = { = { nn }, the unexpanded nodes in the tree }, the unexpanded nodes in the tree

2.2. Let Let xx be the 1st node on be the 1st node on LL. If . If xx = = nn and there is a value and there is a value assigned to it, return this value.assigned to it, return this value.

3.3. If If xx has been assigned a value has been assigned a value vvxx, let , let pp be the parent of be the parent of xx and and vvpp the value currently assigned to the value currently assigned to pp. If . If pp is a minimizing is a minimizing node, set node, set vvpp = min( = min(vvpp, , vvxx). If ). If pp is a maximizing node, set is a maximizing node, set vvpp = max(= max(vvpp, , vvxx). Remove ). Remove xx from from L L and return to step 2.and return to step 2.

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

5.5. Otherwise, set Otherwise, set vvxx to be –∞ if to be –∞ if xx is a maximizing node and + is a maximizing node and + ∞ if ∞ if xx is a minimizing node. Add the children of is a minimizing node. Add the children of xx to the to the front of front of LL and return to step 2. and return to step 2.

Page 301: 3 D Game Programming

301

A common situation of game AIA common situation of game AI Path planningPath planning

From start position to the goalFrom start position to the goal Most popular techniqueMost popular technique

A* (A Star)A* (A Star) 19681968 A search algorithmA search algorithm Favorite teaching example : 15-pizzuleFavorite teaching example : 15-pizzule Algorithm that searches in a state space for the least Algorithm that searches in a state space for the least

costly path from start state to a goal state by examining costly path from start state to a goal state by examining the neighboring statesthe neighboring states

Introduction to Path FindingIntroduction to Path Finding

Page 302: 3 D Game Programming

302

A* Algorithm (1/4)A* Algorithm (1/4)

The A* AlgorithmOpen : priorityqueue of searchnodeClosed : 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

Page 303: 3 D Game Programming

303

A* Algorithm (2/4)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 }

Page 304: 3 D Game Programming

304

A* Algorithm (3/4)A* Algorithm (3/4)

if (NewNode is in Open) { adjust NewNode’s position in Open } else { Push NewNode onto Open } } } } push Node onto Closed }}

Page 305: 3 D Game Programming

305

StateState LocationLocation Neighboring statesNeighboring states

Search spaceSearch space Related to terrain formatRelated to terrain format GridsGrids TrianglesTriangles Points of visibilityPoints of visibility

Cost estimateCost estimate PathPath

Typical A* pathTypical A* path Straight pathStraight path Smooth pathSmooth path

Hierarchical path findingHierarchical path finding

A* Algorithm (4/4)A* Algorithm (4/4)

Page 306: 3 D Game Programming

306

Rectangular gridRectangular grid Use grid centerUse grid center

Search Space & Neighboring States (1/2)Search Space & Neighboring States (1/2)

Rectangular Grid

QuadtreeTriangles

QuadtreeQuadtree Use grid centerUse grid center

Triangles or convex polygonsTriangles or convex polygons Use edge mid-pointUse edge mid-point Use triangle centerUse triangle center

Page 307: 3 D Game Programming

307

Points of visibilityPoints of visibility

Search Space & Neighboring States (2/2)Search Space & Neighboring States (2/2)

Points of Visibility Generalized Cylinders

Generalized cylindersGeneralized cylinders Use intersectionsUse intersections

Page 308: 3 D Game Programming

308

Cost functionCost function CostFromStartCostFromStart CostToGoalCostToGoal

Minimum costMinimum cost Distance traveledDistance traveled Time of traveledTime of traveled Movement points expendedMovement points expended Fuel consumedFuel consumed Penalties for passing through undesired areaPenalties for passing through undesired area Bonuses for passing through desired areaBonuses for passing through desired area ……

EstimateEstimate To goal “distance”To goal “distance”

Cost EstimateCost Estimate

Page 309: 3 D Game Programming

309

Result PathResult Path

Typical A* Path Straight Path

Smooth Path

Page 310: 3 D Game Programming

310

Catmull-Rom SplineCatmull-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)

p1

p2 p3

p4

spline output_points

Page 311: 3 D Game Programming

311

Break the terrain for path finding to several ones Break the terrain for path finding to several ones hierarchicallyhierarchically Room-to-roomRoom-to-room 3D layered terrain3D layered terrain Terrain LODTerrain LOD

ProsPros Speedup the searchSpeedup the search Solve the problem of layered path findingSolve the problem of layered path finding

Hierarchical Path FindingHierarchical Path Finding

Page 312: 3 D Game Programming

312

Moving goalMoving goal Do you need to find path each frame ?Do you need to find path each frame ?

Moving obstaclesMoving obstacles Prediction SchemePrediction Scheme

Complexity of the terrainComplexity of the terrain Hierarchical path findingHierarchical path finding

““Good” pathGood” path

Path Finding ChallengesPath Finding Challenges

Page 313: 3 D Game Programming

313

Action selectionAction selection SteeringSteering LocomotionLocomotion

Motion BehaviorMotion Behavior

A Hierarchy of Motion Behavior

Page 314: 3 D Game Programming

314

Game AI engineGame AI engine State machineState machine

Discussed in “Finite State Machine” sectionDiscussed in “Finite State Machine” section GoalsGoals PlanningPlanning StrategyStrategy

ScriptingScripting Assigned by playersAssigned by players

Players’ inputPlayers’ input

Action SelectionAction Selection

Page 315: 3 D Game Programming

315

Path determinationPath determination Path finding or path planningPath finding or path planning Discussed in “Path Finding”Discussed in “Path Finding”

BehaviorsBehaviors Seek & fleeSeek & flee Pursuit & evasionPursuit & evasion Obstacle avoidanceObstacle avoidance WanderWander Path followingPath following Unaligned collision avoidanceUnaligned collision avoidance

Group steeringGroup steering

SteeringSteering

Page 316: 3 D Game Programming

316

Character physically-based modelsCharacter physically-based models MovementMovement

Turn right, move forward, …Turn right, move forward, … AnimationAnimation

By artistsBy artists Implemented / managed by game engineImplemented / managed by game engine

LocomotionLocomotion

Page 317: 3 D Game Programming

317

A point massA point mass Linear momentumLinear momentum No rotational momentumNo rotational momentum

ParametersParameters MassMass PositionPosition VelocityVelocity

Modified by applied forcesModified by applied forces Max speedMax speed

Top speed of a vehicleTop speed of a vehicle Max steering forceMax steering force

Self-appliedSelf-applied OrientationOrientation

CarCar AircraftAircraft

A Simple Vehicle Model (1/2)A Simple Vehicle Model (1/2)

Page 318: 3 D Game Programming

318

Local spaceLocal space OriginOrigin ForwardForward UpUp SideSide

Steering forcesSteering forces AsymmetricalAsymmetrical

ThrustThrust BrakingBraking SteeringSteering

Velocity alignmentVelocity alignment No slide, spin, …No slide, spin, … TurnTurn

A Simple Vehicle Model (2/2)A Simple Vehicle Model (2/2)

Page 319: 3 D Game Programming

319

The approach :The approach : Steer_force = Truncate(streer_direction, Max_force)Steer_force = Truncate(streer_direction, Max_force) Acceleration = Steer_force / massAcceleration = Steer_force / mass Velocity = Truncate(Velocity + Acceleration, Max_speed)Velocity = Truncate(Velocity + Acceleration, Max_speed) Position = Position + VelocityPosition = Position + Velocity

Euler IntegrationEuler Integration

Page 320: 3 D Game Programming

320

Pursuit to a static targetPursuit to a static target Steer a character toward to a target positionSteer a character toward to a target position

““A moth buzzing a light bulb”A moth buzzing a light bulb” FleeFlee

Inverse of seekInverse of seek VariantsVariants

ArrivalArrival Pursuit to a moving targetPursuit to a moving target

Seek & Flee BehaviorsSeek & Flee Behaviors

Seek Steering forceSeek Steering force desired_velocity = normalize(target - position)*max_speeddesired_velocity = normalize(target - position)*max_speed steering = desired_velocity – velocitysteering = desired_velocity – velocity

Page 321: 3 D Game Programming

321

One of the idea : a stopping radiusOne of the idea : a stopping radius Outside the radius, arrival is identical to seekOutside the radius, arrival is identical to seek Inside the radius, the speed is ramped down to zeroInside the radius, the speed is ramped down to zero

target_offset = target – positiontarget_offset = target – position distance = length(target_offset)distance = length(target_offset) ramped_speed = max_speed*(distance/slowing_distance)ramped_speed = max_speed*(distance/slowing_distance) clipped_speed = minimum(ramped_speed, max_speed)clipped_speed = minimum(ramped_speed, max_speed) desired_velocity = (clipped_speed/distance)*target_offset desired_velocity = (clipped_speed/distance)*target_offset steering = desired_velocity – velocitysteering = desired_velocity – velocity

Arrival BehaviorArrival Behavior

Page 322: 3 D Game Programming

322

Target is movingTarget is moving Apply seek or flee to the target’s predicted positionApply seek or flee to the target’s predicted position

Pursuit & Evasion BehaviorsPursuit & Evasion Behaviors

Estimate the prediction interval TEstimate the prediction interval T T = DcT = Dc D = distance(pursur, quarry)D = distance(pursur, quarry) c = turning parameterc = turning parameter

VariantsVariants Offset pursuitOffset pursuit

““Fly by”Fly by”

Page 323: 3 D Game Programming

323

Use bounding sphereUse bounding sphere Not collision detectionNot collision detection

Obstacle Avoidance Behavior Obstacle Avoidance Behavior

steering force

ProbeProbe A cylinder lying along forward axisA cylinder lying along forward axis Diameter = character’s bounding sphereDiameter = character’s bounding sphere Length = speed (means Alert range)Length = speed (means Alert range)

Find the most threaten obstacleFind the most threaten obstacle Nearest intersected obstacleNearest intersected obstacle

Steering Steering

Page 324: 3 D Game Programming

324

Random steeringRandom steering One solution :One solution :

Retain steering direction stateRetain steering direction state Constrain steering force to the sphere surface Constrain steering force to the sphere surface

located slightly ahead of the characterlocated slightly ahead of the character Make small random displacements to it each frameMake small random displacements to it each frame

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

Wander BehaviorWander Behavior

Another one :Another one : Perlin noisePerlin noise

VariantsVariants ExploreExplore

Page 325: 3 D Game Programming

325

The pathThe path SpineSpine

A spline or poly-line to define the pathA spline or poly-line to define the path PipePipe

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

Path Following BehaviorPath Following Behavior

VariantsVariants Wall followingWall following ContainmentContainment

FollowingFollowing A velocity-based prediction positionA velocity-based prediction position

Inside the tubeInside the tube Do nothing about steeringDo nothing about steering

Outside the tubeOutside the tube ““Seek” to the on-path projectionSeek” to the on-path projection

Page 326: 3 D Game Programming

326

A flow field environment is defined.A flow field environment is defined. Virtual realityVirtual reality

Not common in gamesNot common in games

Flow Field Following BehaviorFlow Field Following Behavior

Page 327: 3 D Game Programming

327

Turn away from possible collisionTurn away from possible collision Predict the potential collisionPredict the potential collision

Use bounding spheresUse bounding spheres

Unaligned Collision Avoidance BehaviorUnaligned Collision Avoidance Behavior

If possibly collide,If possibly collide, Apply the steering on both charactersApply the steering on both characters Steering direction is possible collision resultSteering direction is possible collision result

Use “future” possible positionUse “future” possible position The connected line between two sphere centersThe connected line between two sphere centers

Page 328: 3 D Game Programming

328

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

The behaviors including :The behaviors including : SeparationSeparation CohesionCohesion AlignmentAlignment

Steering Behaviors for Groups of CharactersSteering Behaviors for Groups of Characters

Page 329: 3 D Game Programming

329

The local neighborhood is defined as :The local neighborhood is defined as : A distanceA distance The field-of-viewThe field-of-view

AngleAngle

The Local Neighborhood of a CharacterThe Local Neighborhood of a Character

The Neighborhood

Page 330: 3 D Game Programming

330

Make a character to maintain a distance from others Make a character to maintain a distance from others nearby.nearby. Compute the repulsive forces within local neighborhoodCompute the repulsive forces within local neighborhood

Calculate the position vector for each nearbyCalculate the position vector for each nearby Normalize itNormalize it Weight the magnitude with distanceWeight the magnitude with distance

1/distance1/distance Sum the result forcesSum the result forces Negate itNegate it

Separation BehaviorSeparation Behavior

Page 331: 3 D Game Programming

331

Make a character to cohere with the others nearbyMake a character to cohere with the others nearby Compute the cohesive forces within local neighborhoodCompute the cohesive forces within local neighborhood

Compute the average position of the others nearbyCompute the average position of the others nearby Gravity centerGravity center

Apply “Seek” to the positionApply “Seek” to the position

Cohesion BehaviorCohesion Behavior

Page 332: 3 D Game Programming

332

Make a character to align with the others nearbyMake a character to align with the others nearby Compute the steering forceCompute the steering force

Average the together velocity of all other characters nearbyAverage the together velocity of all other characters nearby The result is the desired velocityThe result is the desired velocity Correct the current velocity to the desired one with the Correct the current velocity to the desired one with the

steering forcesteering force

Alignment BehaviorAlignment Behavior

Page 333: 3 D Game Programming

333

““ Boids Model of Flocks”Boids Model of Flocks” [Reynolds 87][Reynolds 87]

Combination of :Combination of : Separation steeringSeparation steering Cohesion steeringCohesion steering Alignment steeringAlignment steering

For each combination including :For each combination including : A weight for each combinationA weight for each combination A distanceA distance An AngleAn Angle

Flocking BehaviorFlocking Behavior

Page 334: 3 D Game Programming

334

Follow a leaderFollow a leader Stay with the leaderStay with the leader

““Pursuit” behavior (Arrival style)Pursuit” behavior (Arrival style) Stay out of the leader’s wayStay out of the leader’s way

Defined as “next position” with an extensionDefined as “next position” with an extension ““Evasion” behavior when inside the above areaEvasion” behavior when inside the above area

““Separation” behavior for the followersSeparation” behavior for the followers

Leader Following BehaviorLeader Following Behavior

Page 335: 3 D Game Programming

335

A simple vehicle model with local neighborhoodA simple vehicle model with local neighborhood Common steering behaviors including :Common steering behaviors including :

SeekSeek FleeFlee PursuitPursuit EvasionEvasion Offset pursuitOffset pursuit ArrivalArrival Obstacle avoidanceObstacle avoidance WanderWander Path followingPath following

Behavior ConclusionBehavior Conclusion

Wall followingWall following ContainmentContainment Flow field followingFlow field following Unaligned collision avoidanceUnaligned collision avoidance SeparationSeparation CohesionCohesion AlignmentAlignment FlockingFlocking Leader followingLeader following

Combining the above behaviors in your applicationCombining the above behaviors in your application

Page 336: 3 D Game Programming

336

Finite State Machine (FSM) is the most commonly Finite State Machine (FSM) is the most commonly used game AI technology today.used game AI technology today. SimpleSimple EfficientEfficient Easily extensibleEasily extensible Powerful enough to handle a wide variety of situationsPowerful enough to handle a wide variety of situations

Theory (simplified)Theory (simplified) A set of states, SA set of states, S An input vocabulary, IAn input vocabulary, I Transition function, T(s, i)Transition function, T(s, i)

Map a state and an input to another stateMap a state and an input to another state

Introduction to FSM (1/2)Introduction to FSM (1/2)

Page 337: 3 D Game Programming

337

Practical usePractical use StateState

BehaviorBehavior TransitionTransition

Across statesAcross states ConditionsConditions

It’s all about driving behaviorIt’s all about driving behavior Flow-chart diagramFlow-chart diagram

UML State chartUML State chart ArrowArrow

TransitionTransition RectangleRectangle

StateState

Introduction FSM (2/2)Introduction FSM (2/2)

Page 338: 3 D Game Programming

338

An Example of FSM As a DiagramAn Example of FSM As a Diagram

GatherTreasure

Flee

Fight

Monster in sight

No monsterMonster dead

Cor

nere

d

Page 339: 3 D Game Programming

339

Character AICharacter AI ““Decision-Action” modelDecision-Action” model BehaviorBehavior

Mental stateMental state TransitionTransition

Players’ actionPlayers’ action The other characters’ actionsThe other characters’ actions Some features in the game worldSome features in the game world

FSM for GamesFSM for Games

Page 340: 3 D Game Programming

340

Code-based FSMCode-based FSM Simple Code One UpSimple Code One Up

StraightforwardStraightforward Most commonMost common

Macro-assisted FSM LanguageMacro-assisted FSM Language Data-Driven FSMData-Driven FSM

FSM Script LanguageFSM Script Language

Implement FSMImplement FSM

Page 341: 3 D Game Programming

341

Coding an FSM – Code Example 1Coding an FSM – Code Example 1

void RunLogic(int *state)void RunLogic(int *state){{ switch(*state)switch(*state) {{ case 0: // Wandercase 0: // Wander Wander();Wander(); if (SeeEnemy()) *state = 1;if (SeeEnemy()) *state = 1; if (Dead()) *state = 2;if (Dead()) *state = 2; break;break; case 1: // Attackcase 1: // Attack Attack();Attack(); *state = 0;*state = 0; if (Dead()) *state = 2;if (Dead()) *state = 2; break;break; case 2: // Deadcase 2: // Dead SlowlyRot();SlowlyRot(); break;break; }}}}

Page 342: 3 D Game Programming

342

Coding an FSM – Code Example 2Coding an FSM – Code Example 2void RunLogic(FSM *fsm)void RunLogic(FSM *fsm){{ // Do action based on the state and determine next input// Do action based on the state and determine next input input = 0;input = 0; switch(fsm->GetStateID())switch(fsm->GetStateID()) {{ case 0: // Wandercase 0: // Wander Wander();Wander(); if (SeeEnemy()) input = SEE_ENEMY;if (SeeEnemy()) input = SEE_ENEMY; if (Dead()) input = DEAD;if (Dead()) input = DEAD; break;break; case 1: // Attackcase 1: // Attack Attack();Attack(); input = WANDER;input = WANDER; if (Dead()) input = DEAD;if (Dead()) input = DEAD; break;break; case 2: // Deadcase 2: // Dead SlowlyRot();SlowlyRot(); break;break; }} // DO state transition based on computed input// DO state transition based on computed input fsm->StateTransition(input);fsm->StateTransition(input);}}

Page 343: 3 D Game Programming

343

FSM Language Use MacrosFSM Language Use Macros Coding a state machine directly causes lack of Coding a state machine directly causes lack of

structurestructure Going complex when FSM at their largestGoing complex when FSM at their largest

Use macrosUse macros Beneficial propertiesBeneficial properties

StructureStructure ReadabilityReadability DebuggingDebugging

SimplicitySimplicity

Page 344: 3 D Game Programming

344

FSM Language Use Macros – An ExampleFSM Language Use Macros – An Example#define BeginStateMachine …#define BeginStateMachine …#define State(a) …#define State(a) ………bool MyStateMachine::States(StateMachineEvent event,bool MyStateMachine::States(StateMachineEvent event, int state)int state){{ BeginStateMachineBeginStateMachine State(0)State(0) OnUpdateOnUpdate Wander();Wander(); if (SeeEnemy()) SetState(1);if (SeeEnemy()) SetState(1); if (Dead()) SetState(2);if (Dead()) SetState(2); State(1)State(1) OnUpdateOnUpdate Attack();Attack(); SetState(0);SetState(0); if (Dead()) SetState(2);if (Dead()) SetState(2); State(2);State(2); OnUpdateOnUpdate RotSlowly();RotSlowly(); EndStateMachineEndStateMachine}}

Page 345: 3 D Game Programming

345

Data-Driven FSMData-Driven FSM Scripting languageScripting language

Text-based script fileText-based script file Transformed intoTransformed into

C++C++ Integrated into source codeIntegrated into source code

BytecodeBytecode Interpreted by the gameInterpreted by the game

AuthoringAuthoring CompilerCompiler AI editing toolAI editing tool

GameGame FSM script engineFSM script engine FSM interfaceFSM interface

Page 346: 3 D Game Programming

346

Data-Driven FSM DiagramData-Driven FSM Diagram

Authoring

FSMs bytecodeCompiler

AI EditingTool

Condition &Action

Vocabulary

Games

FSM ScriptEngine

FSM Interface

Condition &Action Code

Game Engine

Artist,Designers, &Developers

Page 347: 3 D Game Programming

347

AI Editing Tool for FSMAI Editing Tool for FSM Pure textPure text

Syntax ?Syntax ? Visual graph with textVisual graph with text Used by Designers, Artists, or DevelopersUsed by Designers, Artists, or Developers

Non-programmersNon-programmers Conditions & action vocabularyConditions & action vocabulary

SeeEnemySeeEnemy CloseToEnemyCloseToEnemy AttackAttack ……

Page 348: 3 D Game Programming

348

FSM InterfaceFSM Interface Facilitating the binding between vocabulary and game Facilitating the binding between vocabulary and game

worldworld Glue layer that implements the condition & action Glue layer that implements the condition & action

vocabulary in the game worldvocabulary in the game world Native conditionsNative conditions

SeeEnemy(), CloseToEnemy()SeeEnemy(), CloseToEnemy() Action libraryAction library

Attack(…)Attack(…)

Page 349: 3 D Game Programming

349

FSM Script Language BenefitsFSM Script Language Benefits Accelerated productivityAccelerated productivity Contributions from artists & designersContributions from artists & designers Ease of useEase of use ExtensibilityExtensibility

Page 350: 3 D Game Programming

350

Processing Models for FSMsProcessing Models for FSMs Processing the FSMsProcessing the FSMs

Evaluate the transition conditions for current stateEvaluate the transition conditions for current state Perform any associated actionsPerform any associated actions

When and how ?When and how ? Depend on the exact need of gamesDepend on the exact need of games

Three common FSM processing modelsThree common FSM processing models PollingPolling Event-drivenEvent-driven MultithreadMultithread

Page 351: 3 D Game Programming

351

Polling Processing ModelPolling Processing Model Processing each FSM at regular time intervalsProcessing each FSM at regular time intervals

Tied to game frame rateTied to game frame rate Or some desired FSM update frequencyOr some desired FSM update frequency Limit one state transition in a cycleLimit one state transition in a cycle Give a FSM a time-boundGive a FSM a time-bound

ProsPros StraightforwardStraightforward Easy to implementEasy to implement Easy to debugEasy to debug

ConsCons InefficiencyInefficiency

Some transition are not necessary to check every frameSome transition are not necessary to check every frame Careful design to your FSMCareful design to your FSM

Page 352: 3 D Game Programming

352

Event-driven Processing ModelEvent-driven Processing Model Designed to prevent from wasted FSM processingDesigned to prevent from wasted FSM processing An FSM is only processed when it’s relevantAn FSM is only processed when it’s relevant ImplementationImplementation

A Publish-subscribe messaging system (Observer pattern)A Publish-subscribe messaging system (Observer pattern) Allows the engine to send events to individual FSMsAllows the engine to send events to individual FSMs An FSM subscribes only to the events that have the potential An FSM subscribes only to the events that have the potential

to change the current stateto change the current state When an event is generated, the FSMs subscribed to that When an event is generated, the FSMs subscribed to that

events are all processedevents are all processed ““As-needed” approachAs-needed” approach

Should be much more efficient than polling ?Should be much more efficient than polling ? Tricky balance for fine-grained or coarse-grained Tricky balance for fine-grained or coarse-grained

eventsevents

Page 353: 3 D Game Programming

353

Multithread Processing ModelMultithread Processing Model Both polling & event-driven are serially processedBoth polling & event-driven are serially processed Multithread processing modelMultithread processing model

Each FSM is assigned to its own thread for processingEach FSM is assigned to its own thread for processing Game engine is running in another separate threadGame engine is running in another separate thread All FSM processing is effectively concurrent and continuousAll FSM processing is effectively concurrent and continuous Communication between threads must be thread-safeCommunication between threads must be thread-safe

Using standard locking & synchronization mechanismsUsing standard locking & synchronization mechanisms ProsPros

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

ConsCons Overhead when many simultaneous characters activeOverhead when many simultaneous characters active Multithreaded programming is difficultMultithreaded programming is difficult

Page 354: 3 D Game Programming

354

Interfacing with Game Engine (1/2)Interfacing with Game Engine (1/2) FSMs encapsulate complex behavior logicFSMs encapsulate complex behavior logic

Decision, condition, action, …Decision, condition, action, … Game engine does correspondingGame engine does corresponding

Character animation, movements, sounds, …Character animation, movements, sounds, … The interface :The interface :

Code each action as a functionCode each action as a function Need recompile if any code is changedNeed recompile if any code is changed ie., FleeWolf()ie., FleeWolf()

CallbacksCallbacks Function pointersFunction pointers ie., actionFunction[fleeWolf]()ie., actionFunction[fleeWolf]()

Container methodContainer method actionFunctions->FleeWolf();actionFunctions->FleeWolf(); DLLDLL

Page 355: 3 D Game Programming

355

Interfacing with Game Engine (2/2)Interfacing with Game Engine (2/2) Take TheFly as example:Take TheFly as example:

class AArmyUnit : public FnCharacter{ … void DoAttack(…);}

AArmyUnit *army;

army->Object(…);army->MoveForward(dist, …);…army->DoAttack(…);

Page 356: 3 D Game Programming

356

FSM Efficiency & OptimizationFSM Efficiency & Optimization Two categories :Two categories :

Time spentTime spent Computational costComputational cost

Scheduled processingScheduled processing Priority for each FSMPriority for each FSM Different update frequencyDifferent update frequency

Load balancing schemeLoad balancing scheme Collecting statistics of past performance & extrapolating Collecting statistics of past performance & extrapolating

Time-bound for each FSMTime-bound for each FSM Do careful designDo careful design

At the design levelAt the design level Level-of-detail FSMsLevel-of-detail FSMs

Page 357: 3 D Game Programming

357

Level-Of-Detail FSMs Level-Of-Detail FSMs Simplify the FSM when the player won’t notice the Simplify the FSM when the player won’t notice the

differencesdifferences Outside the player’s perceptual rangeOutside the player’s perceptual range Just like the LOD technique used in 3D game engine Just like the LOD technique used in 3D game engine

Three design keys :Three design keys : Decide how many LOD levelsDecide how many LOD levels

How much development time available ?How much development time available ? The approximation extentThe approximation extent

LOD selection policyLOD selection policy The distance between the NPC with the player ?The distance between the NPC with the player ? If the NPC can “see” the player ?If the NPC can “see” the player ? Be careful the problem of “visible discontinuous behavior”Be careful the problem of “visible discontinuous behavior”

What kind of approximationsWhat kind of approximations Cheaper and less accurate solution Cheaper and less accurate solution

Page 358: 3 D Game Programming

358

Extending the Basic FSM Extending the Basic FSM Extending statesExtending states

Begin-end blockBegin-end block

BeginDoAction(); DoActions();EndDoAction();

Stacks & FSMsStacks & FSMs Stack-based “history” of FSMsStack-based “history” of FSMs

““Remember” the sequence of states passed throughRemember” the sequence of states passed through ““Retrace” its steps at willRetrace” its steps at will

Hierarchical FSMHierarchical FSM Polymorphic FSMsPolymorphic FSMs Fuzzy State MachineFuzzy State Machine

Combined with fuzzy logicCombined with fuzzy logic

Page 359: 3 D Game Programming

359

A Hierarchical FSM Example A Hierarchical FSM Example

GatherTreasure

Flee

Fight

Monster in sight

No monster

Monster dead Corn

ered

FindTreasure

Go ToTreasure

TakeTreasure

FindTreasureGather

Treasure

Live

Active FSM

Stack

Page 360: 3 D Game Programming

360

Another Hierarchical FSM Example Another Hierarchical FSM Example

Patrol

Investigate

Attack

Done

Noise

Saw Enemy

Saw Enemy

Done

Go to ALook forIntruders

Go to BLook forIntruders

Patrol

ReportNoise

Go OverTo Noise

Look forIntruders

FalseAlarm!

Investigate

noisenoise

Page 361: 3 D Game Programming

361

More Topics in Game AI More Topics in Game AI ScriptingScripting Goal-based planningGoal-based planning Rule-based inference engineRule-based inference engine Neural networkNeural network ReferencesReferences

Game Programming GemsGame Programming Gems AI Game Programming WisdomAI Game Programming Wisdom

Page 362: 3 D Game Programming

362

12th12thGame PhysicsGame Physics

Page 363: 3 D Game Programming

363

Introduction to Game PhysicsIntroduction to Game Physics Traditional game physicsTraditional game physics

Particle systemParticle system Rigid body dynamicsRigid body dynamics Flexible body dynamicsFlexible body dynamics

Some state-of-art topicsSome state-of-art topics Car physicsCar physics Fluid dynamicsFluid dynamics Rag-doll physicsRag-doll physics

PhysicsPhysics Rigid body kinematicsRigid body kinematics Newton’s LawsNewton’s Laws ForcesForces MomentumMomentum EnergyEnergy

Page 364: 3 D Game Programming

364

Basic Concepts from Physics (1/2)Basic Concepts from Physics (1/2) Newton’s LawsNewton’s Laws

11stst Law Law ““靜者恆靜,動者恆成等速度運動”靜者恆靜,動者恆成等速度運動”

22ndnd Law Law F = ma = mF = ma = mv/v/tt

33rdrd Law Law 作用力與反作用力作用力與反作用力

ForcesForces Gravity / Spring forces / Friction / ViscosityGravity / Spring forces / Friction / Viscosity TorqueTorque

= r X F= r X F EquilibriumEquilibrium

Page 365: 3 D Game Programming

365

Basic Concepts from Physics (2/2)Basic Concepts from Physics (2/2) MomentumMomentum

Linear momentumLinear momentum Angular momentumAngular momentum Moment of inertiaMoment of inertia

Page 366: 3 D Game Programming

366

Particles are objects withParticles are objects with MassMass PositionPosition VelocityVelocity Respond to forcesRespond to forces

BBut no spatial extent (no size!)ut no spatial extent (no size!) Point massPoint mass

BBased on Newton Lawsased on Newton Laws ff = m = maa xx = = f f / m/ m vv = = ff / m, / m, xx = = vv

Particle DynamicsParticle Dynamics

..

. .

Page 367: 3 D Game Programming

367

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;

xvf

m

states

xvf

m

xvf

m

xvf

m

xvf

m

xvf

m

…Particle n time

Basic Particle System (1/5)Basic Particle System (1/5)

Page 368: 3 D Game Programming

368

/* 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 (2/5)Basic Particle System (2/5)

Page 369: 3 D Game Programming

369

/* 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 (3/5)Basic Particle System (3/5)

Page 370: 3 D Game Programming

370

/* 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 (4/5)Basic Particle System (4/5)

Page 371: 3 D Game Programming

371

/* 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;}

Basic Particle System (5/5)Basic Particle System (5/5)

Page 372: 3 D Game Programming

372

Rigid Body DynamicsRigid Body Dynamics Mass of a bodyMass of a body

Mass centerMass center ForceForce

Linear momentumLinear momentum P(t) = M v(t)P(t) = M v(t) Velocity (Velocity (vv))

TorqueTorque Angular momentumAngular momentum L(t) = I L(t) = I (t)(t) Local rotation (Local rotation ())

Inertia tensorInertia tensor ReferenceReference

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

Page 373: 3 D Game Programming

373

Flexible Body Dynamics (1/2)Flexible Body Dynamics (1/2) Particle-spring modelParticle-spring model

F = k xF = k x Not a stress-strain modelNot a stress-strain model Lack of Elasticity, Plasticity, & Viscous-ElasticityLack of Elasticity, Plasticity, & Viscous-Elasticity Can be unstableCan be unstable

Page 374: 3 D Game Programming

374

Flexible Body Dynamics (2/2)Flexible Body Dynamics (2/2) Finite element methodFinite element method

有限元素法有限元素法– Solver for ODE/PDESolver for ODE/PDE– Boundary conditionsBoundary conditions– Energy equationEnergy equation– Stress-strain modelStress-strain model– Very complicated computing processVery complicated computing process

Conservation of energyConservation of energy

Page 375: 3 D Game Programming

375

Advanced Topics in Game PhysicsAdvanced Topics in Game Physics Fracture mechanics (Fracture mechanics ( 破壞力學模擬破壞力學模擬 )) Fluid dynamics (Fluid dynamics ( 流體力學流體力學 )) Car dynamics (Car dynamics ( 車輛動力學車輛動力學 )) Rag-doll physics (Rag-doll physics ( 人體物理模擬人體物理模擬 ))

Page 376: 3 D Game Programming

376

13th13thGame FXGame FX

Page 377: 3 D Game Programming

377

Introduction to Game FXIntroduction to Game FX Improve the visual & sound game effectsImprove the visual & sound game effects Includes :Includes :

Combat FXCombat FX Environment FXEnvironment FX Character FXCharacter FX Scene FXScene FX Sound FXSound FX

FX editor neededFX editor needed General 3D animation tools can not do itGeneral 3D animation tools can not do it

Key-frame system is not workingKey-frame system is not working FX animation is alwaysFX animation is always

ProcedurallyProcedurally Related to the previous frameRelated to the previous frame

Small work but large effectSmall work but large effect

Page 378: 3 D Game Programming

378

FX Editing ToolFX Editing Tool

Page 379: 3 D Game Programming

379

Combat FXCombat FX During the combatDuring the combat

Weapon motion blurWeapon motion blur Weapon effectWeapon effect Skill effectSkill effect

After the combatAfter the combat Damage effectDamage effect

FX editorFX editor

Page 380: 3 D Game Programming

380

Combat FX ExampleCombat FX Example

Page 381: 3 D Game Programming

381

Motion Blur – Image SolutionMotion Blur – Image Solution Computer animation :Computer animation :

Image solutionImage solution Blending rendered image sequenceBlending rendered image sequence

Render too many framesRender too many frames Divide the framesDivide the frames AverageAverage Done!Done!

Shader solution :Shader solution : HDRIHDRI

High dynamic range imageHigh dynamic range image Multiple pass renderingMultiple pass rendering

Page 382: 3 D Game Programming

382

Motion Blur – Geometry SolutionMotion Blur – Geometry Solution In games, use transparent objects to simulate the In games, use transparent objects to simulate the

motion blurmotion blur ““False” motion blurFalse” motion blur

Tracking the motion path of the objectTracking the motion path of the object Connecting them as a triangular meshConnecting them as a triangular mesh Use time-dependent semi-transparency to simulate the “blur”Use time-dependent semi-transparency to simulate the “blur” The path can be smoothed using Catmull-Rom splineThe path can be smoothed using Catmull-Rom spline

Local stability of the curveLocal stability of the curve

Page 383: 3 D Game Programming

383

FX Uses Texture AnimationFX Uses Texture Animation Almost all game FXs use this trickAlmost all game FXs use this trick Geometry object on which the texture animation Geometry object on which the texture animation

playingplaying BillboardBillboard 3D Plate3D Plate CylinderCylinder SphereSphere Revolving a cross section curveRevolving a cross section curve

Texture sequence with color-keyTexture sequence with color-key Semi-transparent texturesSemi-transparent textures

Alpha blendingAlpha blending Source color added to backgroundSource color added to background

Page 384: 3 D Game Programming

384

Particle System for FXs in CombatParticle System for FXs in Combat The FXsThe FXs

Fire / exposure / smoke / dustFire / exposure / smoke / dust Initial value + time dependencyInitial value + time dependency Combined with billboard FXCombined with billboard FX

Billboard to play the texture animationBillboard to play the texture animation Particle system to calculate the motion pathParticle system to calculate the motion path

Gravity is the major force usedGravity is the major force used Emitter patternEmitter pattern

Single emitterSingle emitter Area emitterArea emitter Emitter on verticesEmitter on vertices

Page 385: 3 D Game Programming

385

Environment FXEnvironment FX WeatherWeather

Use particle systemUse particle system RainRain SnowSnow WindWind

FogFog Traditional fogTraditional fog

From near to farFrom near to far Hardware standard featureHardware standard feature

Volume fogVolume fog Layered fogLayered fog Use vertex shaderUse vertex shader

Volume lightingVolume lighting Polygon solutionPolygon solution

Day & nightDay & night

Page 386: 3 D Game Programming

386

Character FXCharacter FX FatalityFatality

Case by case and need creative solutionsCase by case and need creative solutions Rendering effects on skinsRendering effects on skins

Environment mappingEnvironment mapping Bump mapBump map Normal mapNormal map

Need pixel shaderNeed pixel shader Multiple texture mapMultiple texture map

Flexible bodyFlexible body Flexible body dynamicsFlexible body dynamics

FurFur Real-time fur renderingReal-time fur rendering

……

Page 387: 3 D Game Programming

387

Scene FX – Sky BoxScene FX – Sky Box

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

Use textures on the box or dome as the backdropUse textures on the box or dome as the backdrop Use multiple textures and texture coordinates Use multiple textures and texture coordinates

animation to simulate the moving of the cloudsanimation to simulate the moving of the clouds

Page 388: 3 D Game Programming

388

Scene FX – Len’s FlareScene FX – Len’s Flare Runtime calculate the position and orientation of the Runtime calculate the position and orientation of the

camera with the suncamera with the sun Put textures to simulate the len’s flare Put textures to simulate the len’s flare

Page 389: 3 D Game Programming

389

Scene FX – Light ScatteringScene FX – Light Scattering Atmospheric light scatteringAtmospheric light scattering Caused by dust, molecules, or water vaporCaused by dust, molecules, or water vapor

These can cause light to be:These can cause light to be: Scattered into the line of sight (in-scattering)Scattered into the line of sight (in-scattering) Scattered out of the line of sight (out-scattering)Scattered out of the line of sight (out-scattering) Absorbed altogether (absorption)Absorbed altogether (absorption)

Skylight and sun lightSkylight and sun light Can be implemented by vertex shader Can be implemented by vertex shader

Page 390: 3 D Game Programming

390

Scene FX – Light Scattering ExamplesScene FX – Light Scattering Examples

With scatteringWithout scattering

Page 391: 3 D Game Programming

391

14th14thNetwork GamingNetwork Gaming

Page 392: 3 D Game Programming

392

History of Network GamingHistory of Network Gaming MUDMUD

MMulti-ulti-uuser ser DDungeonsungeons Modem gamesModem games Match makingsMatch makings

Real-time strategyReal-time strategy FPSFPS

MMORPGMMORPG MMOGMMOG MMassive assive MMultiplayer ultiplayer OOnline nline RRole-ole-pplaying laying GGamesames

Page 393: 3 D Game Programming

393

MUDMUD A large scale game with fantasy-typeA large scale game with fantasy-type Create your own characterCreate your own character

Strength/Intelligence/Endurance/WisdomStrength/Intelligence/Endurance/Wisdom Most of the MUDs are text-basedMost of the MUDs are text-based

MUD commandsMUD commands Players play them simultaneouslyPlayers play them simultaneously Persistent worldsPersistent worlds

The amount of time investedThe amount of time invested Never endingNever ending

CompetitionCompetition Skill & AttributesSkill & Attributes Experience pointsExperience points

OwnershipOwnership Acquire better itemsAcquire better items

SocietySociety

Page 394: 3 D Game Programming

394

Match MakingsMatch Makings ArcadeArcade

休閒遊戲休閒遊戲 RTSRTS

Real-time strategy gamesReal-time strategy games InternetInternet

FPSFPS First-personal-view shooting gamesFirst-personal-view shooting games

AIAI Game lobby (Game lobby ( 遊戲大廳遊戲大廳 ))

Match makingMatch making RankingRanking ChattingChatting A special caseA special case

Xbox LiveXbox Live

Page 395: 3 D Game Programming

395

Xbox LiveXbox Live

Xbox games with Live featuresXbox games with Live features Xbox is capable of networking in default.Xbox is capable of networking in default. An official lobby server created and maintained by MicrosoftAn official lobby server created and maintained by Microsoft Unique gamer tag in the whole world for each registered Unique gamer tag in the whole world for each registered

playerplayer Every player in any Xbox Live game will know whether his Every player in any Xbox Live game will know whether his

friends are on or off in any Xbox Live game.friends are on or off in any Xbox Live game. Then he/she can send the invitation to his friends for them Then he/she can send the invitation to his friends for them

to join the same game.to join the same game. All Xbox Live development project must be followed and All Xbox Live development project must be followed and

certificated by Microsoft Xbox Live guide.certificated by Microsoft Xbox Live guide. World-wide ranking on the Xbox Live lobbiesWorld-wide ranking on the Xbox Live lobbies Game points controlled by MicrosoftGame points controlled by Microsoft Voice chattingVoice chatting

Page 396: 3 D Game Programming

396

MMORPGsMMORPGs Graphical MUDGraphical MUD

With much more functionalityWith much more functionality PersistentPersistent Real-time online (Real-time online ( 即時在線即時在線 )) Pay-for-playPay-for-play New game gtyles :New game gtyles :

RPGRPG AdventureAdventure ActionAction SocietySociety MessagingMessaging AvatarAvatar

Page 397: 3 D Game Programming

397

Network Gaming ArchitectureNetwork Gaming Architecture Peer-to-peerPeer-to-peer Client serverClient server

Page 398: 3 D Game Programming

398

Peer-to-peerPeer-to-peer The client broadcasts messages to each other The client broadcasts messages to each other

clients.clients. Simple patternSimple pattern Easy to codeEasy to code But need more bandwidth for each clientBut need more bandwidth for each client Limited playersLimited players Fault toleranceFault tolerance High security riskHigh security risk

Client 1

Client 2

Client 3

Client 4Host

Page 399: 3 D Game Programming

399

Client ServerClient Server One server serves every clientsOne server serves every clients More complex in codingMore complex in coding Many playersMany players The server needs more bandwidthThe server needs more bandwidth

This means “This means “ExpensiveExpensive” for hosting the servers.” for hosting the servers. Improved securityImproved security No fault toleranceNo fault tolerance Dedicated server solutionDedicated server solution

Server

Client 2

Client 3

Client 1

Page 400: 3 D Game Programming

400

Internet ConsiderationInternet Consideration Limit network bandwidthLimit network bandwidth Unstable network environmentUnstable network environment Update frame rateUpdate frame rate

For match making : 5-8 fpsFor match making : 5-8 fps Position updatesPosition updates Inventory updatesInventory updates Kill or killedKill or killed ……

For MMOG : 1-2 fpsFor MMOG : 1-2 fps Position updatesPosition updates Inventory updatesInventory updates Kill or killedKill or killed Friend list updatesFriend list updates ChattingChatting MessagingMessaging ……

Page 401: 3 D Game Programming

401

Poor Networking ProblemsPoor Networking Problems Limited bandwidthLimited bandwidth

Data packet size is importantData packet size is important SolutionsSolutions

Data compressionData compression Lower update frame rateLower update frame rate

Low update frame rateLow update frame rate Synchronization is the keySynchronization is the key SolutionsSolutions

ExtrapolationExtrapolation PredictionPrediction ChasingChasing

Network LODNetwork LOD Network visibilityNetwork visibility

Page 402: 3 D Game Programming

402

Data PacketData Packet Data compressionData compression Encode / DecodeEncode / Decode CPU time vs data sizeCPU time vs data size SocketSocket TCP/IP & UDPTCP/IP & UDP

Page 403: 3 D Game Programming

403

Network Data CompressionNetwork Data Compression Must be lossless compression ?Must be lossless compression ? Zip ?Zip ? Bit, byte, short or long ?Bit, byte, short or long ? Fixed-point or floating-point real numberFixed-point or floating-point real number Run-length compressionRun-length compression Use index or ID instead of dataUse index or ID instead of data 1 Boolean = 4 bytes1 Boolean = 4 bytes

Page 404: 3 D Game Programming

404

Position ExtrapolationPosition Extrapolation Use position(i-1) & position(i) to get the next Use position(i-1) & position(i) to get the next

possible position (i+1)possible position (i+1) No velocity data is neededNo velocity data is needed

i-1

i+1

i

real path

i+1

i+2

Page 405: 3 D Game Programming

405

Position PredictionPosition Prediction Use current facing direction to predict next positionUse current facing direction to predict next position Update : (position, facing direction)Update : (position, facing direction)

i-1

i+1

i

real path

i+1

i+2

Page 406: 3 D Game Programming

406

Position ChasingPosition Chasing Always being late one step to the master clientAlways being late one step to the master client Not good for fast moving objectsNot good for fast moving objects

i-1

i

real path

i+1

Page 407: 3 D Game Programming

407

Network VisibilityNetwork Visibility Server will not update the position of the clients that Server will not update the position of the clients that

are out of a specific range of one client.are out of a specific range of one client. Only when the outside clients move into the range of Only when the outside clients move into the range of

the client.the client. That means the client can not “see” the clients That means the client can not “see” the clients

outside its range on the net.outside its range on the net.

Page 408: 3 D Game Programming

408

Network Level-of-detailsNetwork Level-of-details Besides the network visibility, apply the network Besides the network visibility, apply the network

LOD to the client between clients.LOD to the client between clients. For each client, the server should adaptively adjust For each client, the server should adaptively adjust

the update frame rate for the other clients to the the update frame rate for the other clients to the target client according to their physical distance.target client according to their physical distance.

5fps

3fps

1fps

invisible

Me

Page 409: 3 D Game Programming

409

TCP/IP or UDPTCP/IP or UDP Some game data can be lost.Some game data can be lost.

Position for the object in each framePosition for the object in each frame UDP is fast but not reliableUDP is fast but not reliable

Some must be notSome must be not Kill or not kill, killed or nor killedKill or not kill, killed or nor killed TCP/IPTCP/IP

Hybrid data transmission ways :Hybrid data transmission ways : Data-can-not-lost going through TCPData-can-not-lost going through TCP Data-can-lost going through UDPData-can-lost going through UDP Or implement you reliable UDP solutionOr implement you reliable UDP solution

Page 410: 3 D Game Programming

410

Network SecurityNetwork Security Anti-hackingAnti-hacking

System administration issuesSystem administration issues Very important in MMORPGVery important in MMORPG

Cheat preventionCheat prevention Technical example:Technical example:

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

Game-playing example:Game-playing example: Using the game bugs to get the improper fortuneUsing the game bugs to get the improper fortune

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

Page 411: 3 D Game Programming

411

Develop Tools & SolutionsDevelop Tools & Solutions Match-makingsMatch-makings

DirectPlay in DirectX SDKDirectPlay in DirectX SDK Socket programmingSocket programming MiddlewareMiddleware

ie., Quazal NetZie., Quazal NetZ Internet causal gamesInternet causal games

Web programmingWeb programming C#C# SQL programmingSQL programming

MMORPGMMORPG Socket programmingSocket programming MiddlewareMiddleware

Terezona from ZonaTerezona from Zona Eterna from QuazalEterna from Quazal Butterfly.netButterfly.net

Page 412: 3 D Game Programming

412

MMORPG ServerMMORPG Server

Page 413: 3 D Game Programming

413

MMORPGs FeaturesMMORPGs Features AvatarAvatar LevelsLevels RPG game playRPG game play MissionMission ChattingChatting Society & communitySociety & community FriendsFriends CombatCombat NPCs / monstersNPCs / monsters Experience pointsExperience points Extended game contentsExtended game contents Online customer services (GM)Online customer services (GM)

Page 414: 3 D Game Programming

414

MMORPGs Technical ElementsMMORPGs Technical Elements Client-server architectureClient-server architecture ServersServers Network bandwidthNetwork bandwidth Network data packetNetwork data packet Network securityNetwork security GraphicsGraphics DatabaseDatabase

Page 415: 3 D Game Programming

415

MMORPG ServersMMORPG Servers Stand alone serversStand alone servers Distributed systemDistributed system

Page 416: 3 D Game Programming

416

Standalone ServerStandalone Server Using a set of large servers as the game serversUsing a set of large servers as the game servers Each server plays as a single functionEach server plays as a single function

Login server

internet

Game play CommunityDatabase

Page 417: 3 D Game Programming

417

Distributed System - ConceptDistributed System - Concept Distributed PC clustersDistributed PC clusters

servers

internet

Game play servers

servers

Database servers

Login servers

Page 418: 3 D Game Programming

418

Distributed PC ClustersDistributed PC Clusters 1-U1-U Web Server Based on PC ArchitectureWeb Server Based on PC Architecture Two CPUsTwo CPUs Two Network IPsTwo Network IPs

1U serverNet IP 1 Net IP 2

Internal LAN Internet

Page 419: 3 D Game Programming

419

Distributed System - FeaturesDistributed System - Features Two IPsTwo IPs ScalabilityScalability Fault toleranceFault tolerance Load balanceLoad balance Dynamic zoningDynamic zoning ““Blade Servers”Blade Servers”

Page 420: 3 D Game Programming

420

Distributed System - ScalabilityDistributed System - Scalability

Serve concurrent 500 users

Serve concurrent 1000 users

Regional operation considerationRegional operation consideration Cost effectiveCost effective Flexible capacityFlexible capacity

Page 421: 3 D Game Programming

421

Distributed System – Fault ToleranceDistributed System – Fault Tolerance All servers can not down in anytime.All servers can not down in anytime. If some one is going to failed,If some one is going to failed,

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

For standalone server, use redundant policy.For standalone server, use redundant policy.

Page 422: 3 D Game Programming

422

Master server

Slave server A

Slave server B

!

!

Inte

rnal

LAN

Inte

rne

t

Page 423: 3 D Game Programming

423

Master server

Slave server A

Slave server B

X

Inte

rnal

LAN

Inte

rne

t

Page 424: 3 D Game Programming

424

Server on duty

Server off dutyZZZ

!

!

Redundant PolicyRedundant Policy

Page 425: 3 D Game Programming

425

Server off duty

Server on duty

X

Redundant policyRedundant policy

Page 426: 3 D Game Programming

426

Master servers

Slave server A

Slave server B

Inte

rnal

LAN

Inte

rne

t

.

.

Page 427: 3 D Game Programming

427

Distributed System – Load BalanceDistributed System – Load Balance Distributed MMOGs always do the load balance by Distributed MMOGs always do the load balance by

In-house approach.In-house approach. Load ?Load ?

CPU boundCPU bound Memory boundMemory bound Network bandwidthNetwork bandwidth

1 1 server = 500 concurrent playersserver = 500 concurrent players

10 servers = 5000 concurrent players10 servers = 5000 concurrent playersX

!! It’s very important to move the jobsIt’s very important to move the jobs on a crowded server to another oneson a crowded server to another ones

Page 428: 3 D Game Programming

428

Master server

Slave server A

Slave server B

Inte

rnal

LAN

Inte

rne

t

Load balance – case 1

Page 429: 3 D Game Programming

429

Master server

Slave server A

Slave server B

!

!

Inte

rnal

LAN

Inte

rne

t

Load balance case 2

Page 430: 3 D Game Programming

430

Master server

Slave server A

Slave server B

Inte

rnal

LAN

Inte

rne

t

Load balance case 2

Page 431: 3 D Game Programming

431

Zone ConceptZone Concept A “zone” is logically a region of game happening on A “zone” is logically a region of game happening on

servers.servers. We always map the “zone” to a physical 3D scene.We always map the “zone” to a physical 3D scene. A zone is not really a physical hardware server but a A zone is not really a physical hardware server but a

process usually.process usually. A region communicates to the players directly (in the A region communicates to the players directly (in the

same memory block)same memory block)

Page 432: 3 D Game Programming

432

Master server

Slave server A

Slave server B

Inte

rnal

LAN

Inte

rne

t

Page 433: 3 D Game Programming

433

messaging

Server A

Server B

combating

transaction

Zone X

Zone Y

Zone Z

NPC

messaging

Page 434: 3 D Game Programming

434

Interaction within/between Zones (Game Play)Interaction within/between Zones (Game Play) Within the zoneWithin the zone

CombatingCombating ChattingChatting TransactionTransaction

Between zonesBetween zones MessagingMessaging Transaction (remote)Transaction (remote) BankingBanking

Page 435: 3 D Game Programming

435

Server A

Server B

Zone X

Zone Y

Zone Z

NPCServer C

If server B is over-loaded,move the “Zone Y” or “Zone Z” to an available server C

Zone Z

Page 436: 3 D Game Programming

436

Load Balance Using Zone MovingLoad Balance Using Zone Moving Under the concept of “zone moving”, we can move Under the concept of “zone moving”, we can move

the zones between hardware servers to achieve the the zones between hardware servers to achieve the load balance.load balance.

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

Server A

Server B

Server C

If server A is over-loaded,the whole server is over-loadeddue that server A is occupied onlyone zone.

Thinking :

“Can we divide the zone into two or more ? “

Zone X

Zone Y

Zone Z

Page 437: 3 D Game Programming

437

Dynamic ZoningDynamic Zoning Dynamically adjust the zones to meet the loading Dynamically adjust the zones to meet the loading

requirement.requirement. Moving zones between hardwareMoving zones between hardware Divide the zone into small ones according to the load Divide the zone into small ones according to the load

and move the smaller ones to different hardware.and move the smaller ones to different hardware. For the players in the same game zone but running For the players in the same game zone but running

on different process or machine, the data on different process or machine, the data synchronization is becoming a challenge job.synchronization is becoming a challenge job.

Page 438: 3 D Game Programming

438

My Suggestion about Dynamic ZoningMy Suggestion about Dynamic Zoning

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

A zone is composed by several groups.A zone is composed by several groups. A “group” is a collection of players (A “group” is a collection of players (populationpopulation)) All players in the same group are running in the same All players in the same group are running in the same

process.process. Players in different group communicate between Players in different group communicate between

processes.processes. Inter-process communication (IPC)Inter-process communication (IPC) or Networking.or Networking.

Group is more dynamically to be managed due to the Group is more dynamically to be managed due to the concept of concept of populationpopulation

Page 439: 3 D Game Programming

439

Zone = A 3D SceneZone = A 3D Scene A 3D sceneA 3D scene

3D models3D models Moving objectsMoving objects NPCsNPCs ……

Zones are physically neighboring togetherZones are physically neighboring together Using portals to connect the relationship among Using portals to connect the relationship among

zones.zones. Player travels between zones.Player travels between zones.

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

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

Page 440: 3 D Game Programming

440

Population GroupPopulation Group A data structure to manage the players.A data structure to manage the players. When a player logins into one zone, he should be When a player logins into one zone, he should be

assigned into one available population group and got assigned into one available population group and got the ID.the ID.

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

A zone can be divided into several groups.A zone can be divided into several groups. Groups can be created/deleted/moved by the Groups can be created/deleted/moved by the

servers for load balance management.servers for load balance management.

Page 441: 3 D Game Programming

441

Players in a ZonePlayers in a Zone

Zone X

P1G1

P5G1

P2G2

P7G2

P12G2

P6G3

P5G3

Page 442: 3 D Game Programming

442

Server A

Server B

Zone X

Group 1Group 2

When a new playerlogins into Zone X, insertthe player into group 2 whichhas space for new comer

Page 443: 3 D Game Programming

443

Server A

Server B

Zone X

Group 1Group 2

But if there are no groups with available space, create a new group for new comers

Group 3

Page 444: 3 D Game Programming

444

Server A

Server B

Zone X

Group 1Group 2Group 3Group 4Group 5Group 6

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

Zone X (2)

Group 7

Page 445: 3 D Game Programming

445

Server A

Server B

Zone X

Group 1Group 2Group 3Group 4Group 5Group 6

Zone X (2)

Group 7Group 8

IPC

Network communication

Page 446: 3 D Game Programming

446

The Challenge of Dynamic Zoning (1/2)The Challenge of Dynamic Zoning (1/2) Physical terrain and models are not easy to be divided Physical terrain and models are not easy to be divided

dynamically in-game.dynamically in-game. If your zones are coupling with the physical scenes, to divide If your zones are coupling with the physical scenes, to divide

the geometric data in runtime needs some specific algorithms the geometric data in runtime needs some specific algorithms for 3D scenesfor 3D scenes

From my suggestion, don’t do itFrom my suggestion, don’t do it Find a mechanism that makes the zone dividableFind a mechanism that makes the zone dividable And that is the “Population Group”And that is the “Population Group”

One server can have multiple zones running.One server can have multiple zones running. One zone can run on several servers. One zone can run on several servers.

Hard to codeHard to code Duplicated memory on serversDuplicated memory on servers Synchronization between serversSynchronization between servers Data communication overheadData communication overhead

Game Play SensitiveGame Play Sensitive

Page 447: 3 D Game Programming

447

The Challenge of Dynamic Zoning (2/2)The Challenge of Dynamic Zoning (2/2) Running players in the same zone by multiple Running players in the same zone by multiple

processes.processes. Duplicated memory on the same serverDuplicated memory on the same server Synchronization between processesSynchronization between processes Data communication between processesData communication between processes

Players’ attribute data should be classified as :Players’ attribute data should be classified as : Frequently used between playersFrequently used between players

Must be duplicated between processesMust be duplicated between processes Seldom used between playersSeldom used between players

Send between players when necessarySend between players when necessary Locally used by player himselfLocally used by player himself

Cross-platform server API designCross-platform server API design