Document

advertisement
3D Game Programming
Using TheFly3D©
王銓彰
kevin.cwang@msa.hinet.net
1
課程大綱















Introduction to Game Development (3hr)
Game System Analysis (3hr)
The Game Main Loop (3hr)
3D Game Engine Training (TheFly3D) (6hr)
Game Mathematics (3hr)
Geometry for Games (3hr)
Advanced Scene Management System (3hr)
Terrain (3hr)
Game AI (9hr)
Game Physics (3hr)
Game FX (3hr)
Characters (3hr)
Network Gaming (3hr)
Introduction to MMOG (3hr)
The 2D Sprites (3hr)
2
課程要求

One Term Project
– The Students will divide into several teams
– Use TheFly3D Game Engine to code a 3D Real-time Strategy Game
– The Teacher will Provide Graphics Materials


Two Examinations
Homework will be closely coupled with the term project
3
王銓彰

目前
– 數位內容學院 專任講師 / 顧問
– 資策會多媒體研究所 專案顧問

學歷
– 台灣大學土木工程學系畢業

資歷
–
–
–
–
–
97-03
96-96
93-96
90-93
89-90
昱泉國際股份有限公司 技術長
虛擬實境電腦動畫股份有限公司 研發經理
西基電腦動畫股份有限公司 研發經理
國家高速電腦中心 助理研究員
台灣大學土木工程學系 CAE Lab 研究助理
4
王銓彰

Game作品
– 昱泉國際
» DragonFly 3D Game Engine

M2神甲奇兵, VRLobby, 天劍記
» Lizard 3D Game Engine

幻影特攻、笑傲江湖 I & II、神鵰俠侶 I & II、風雲、小李飛刀、笑傲江
湖網路版、怪獸總動員、聖劍大陸、笑傲外傳
– 西基電腦動畫
» Ultimate Fighter – 1st Realtime 3D fighting game in Taiwan
5
王銓彰

專長 (Expertise)
–
–
–
–
–
–
–
3D Computer Graphics
Geometric Modeling
Numerical Methods
Character Animation
Photo-realistic Rendering
Real-time Shading
Volume Rendering
6
王銓彰

應用領域 (Applications)
–
–
–
–
–
即時3D遊戲開發 (Real-time 3D Game Development)
電腦動畫 (Computer Animation)
虛擬實境 (Virtual Reality)
電腦輔助設計 (Computer-aided Design, CAD)
科學視算 (Scientific Visualization)
7
Introduction
To
Game Development
8
Introduction to Game Dev
Game Platform
 Game Types
 Game Team
 Game Development Pipeline
 Game Software System
 Tools

9
Game Platform

PC
–
–
–
–

Single player
Match Makings
MMOG (Massive Multi-player Online Game)
Web-based Games
Console
– Sony PS2
– MS Xbox
– Nintedo GameCube


Arcade
Mobile
– GBA
– Hand-held
10
Game Development on PC








Designed for Office Application
Not for Entertainment
A Virtual Memory System
– Unlimited memory using
But Video Memory is Limited
PCI/AGP might be a Challenge
Open Architecture
Compatibility Test is Important
Development is Easy to Setup
11
Game Development on Console





Specific Hardware Designed for Games
Single User / Single Process OS
In General no Hard Disk Drive (??)
Closed System
Very Native Coding Way
– Proprietary SDK
– Hardware related features

Limited Resources
– Memory

One Console runs, the others do!
12
Game Types










RPG (Role playing games)
AVG (Adventure games)
RTS (Real-time strategy games)
FPS (First-person shooting games)
MMORPG
SLG (???, 戰棋)
Simulation
Sports
Puzzle games
Table games
13
Game Team Members




開發團隊
– 製作人
– 執行製作人
– 企劃團隊
– 程式團隊
– 美術團隊
行銷業務團隊
測試團隊
遊戲審議委員會
14
Game Producer 遊戲製作人





Team Leader (always)
資源管理 (Resource Management)
行政管理 (Administration)
向上負責 (Upward Management)
專案管理 (Project Management)
15
遊戲執行製作人




專案管理執行 (Project Management)
Daily 運作
House Keeping
Not full-time job position
16
遊戲企劃









故事設計 (Story Telling)
腳本設計 (Scripting)
玩法設計 (Game Play Design)
關卡設計 (Level Design)
遊戲調適 (Game Tuning)
數值設定 (Numerical Setup)
AI 設計 (Game AI)
音效設定 (Sound FX Setup)
場景設定 (Scene Setup)
17
遊戲美術







場景 (Terrain)
人物 (Character)
建模 (Models)
材質 (Textures)
動作 (Motion / Animation)
特效 (FX)
User Interface
18
遊戲程式





遊戲程式 (Game Program)
遊戲開發工具 (Game Tools)
– Level Editor
– Scene Editor
– FX Editor
– Script Editor
遊戲Data Exporters from 3D Software
– 3dsMax / Maya / Softimage
Game Engine Development
Online Game Server Development
19
遊戲開發流程
Basic Procedures for Game Development
Idea
Proposal
Concept
Approval





Production
Prototype
發想 (Idea)
提案 (Proposal)
製作 (Production)
整合 (Integration)
測試 (Testing)
– 除錯 (Debug)
– 調適 (Tuning)
Integration
Pre-alpha
Testing
Debug
Alpha
Tuning
Beta
Final
> Concept Approval
> 雛形 (Prototype)
> Pre-alpha
> Alpha
> Beta
20
遊戲發想(Concept Design)







遊戲類型 (Game Types)
遊戲世界觀 (Game World)
故事 (Story)
遊戲特色 (Features)
遊戲玩法 (Game Play)
遊戲定位 (Game Product Positioning)
– Target player
– Marketing segmentation / positioning
風險評估 (Risk)
– SWOT (Strength/Weakness/Opportunity/Threat)
21
遊戲提案(Proposal)







系統分析 (System Analysis)
GDD 撰寫 (Game Design Document)
MDD 撰寫 (Media Design Document)
TDD 撰寫 (Technical Design Document)
遊戲專案建立 (Game Project)
– Schedule
– Milestones / Check points
– Risk management
測試計畫書
團隊建立 (Team Building)
22
遊戲開發(Production)




美術量產製作
– Modeling
– Textures
– Animation
– Motion
– FX
程式開發 (Coding)
企劃數值設定
…
量產 !
23
遊戲整合(Integration)






關卡串聯 (Level Integration)
數值調整 (Number Tuning)
音效置入 (Audio)
完成所有美術
程式與美術結合
Focus Group (User Study)
– Release some playable levels for focus group
24
遊戲測試(Test)



Alpha 測試
– 除錯 (Debug)
Beta 測試
– 數值微調
– Game play 微調
– 對線上遊戲而言 (MMOG)
» 封閉測試 (Closed Beta)
» 開放測試 (Open Beta)
極限測試 (Critical Testing)
– 線上遊戲才有
25
Bugs


Bug 分級 (Bug Classification)
– A Bug
– B Bug
– C Bug
– S Bug
Principles
– Bug 分級從嚴
– Tester vs Debugger
Bug
Bug Classification
Bug Dispatch
Debug
N
?
Verify
Y
FAQ
26
Game Software System
Game
NPC System
Virtual Agent
Trading System
Fighting System
FX System
Terrain
Character Dynamics Sound FX
Collision
3D Scene Mngmt
3D Graphics API
2D Sprite
Game AI
Story
Gamepad
2D API
Hardware
Script System
UI
Audio
Network
Input Device
OS API
Game Play
Layer
Engine
Layer
System
Layer
27
System Layer – APIs (1/2)

3D Graphics API
– DirectX 9.0 SDK – Direct3D
– OpenGL 2.0

2D API
– DirectX 9.0 SDK - DirectMedia
– Win32 GDI

Input Device
– DirectX 9.0 SDK – DirectInput

Audio
– DirectX 9.0 SDK – DirectSound / Direct3DSound /
DirectMedia
– OpenAL
28
System Layer – APIs (2/2)

OS API
– Win32 SDK
– MFC

Network
– DirectX 9.0 SDK – DirectPlay
– Socket library
29
Engine Layer (1/2)

3D Scene Management System
– Scene Graph









Shaders
2D Sprite System
Audio System
Gamepad
Hotkey
Mouse
Timers
Network
DDK Interface
30
Engine Layer (2/2)


Terrain
Advanced Scene Management – Space Partition
– BSP Tree
– Octree






Character System
Motion Blending Techniques
Dynamics
Collision Detection
SoundFX
User Interface
31
Game Play Modula


NPC (Non-playable Characters)
Game AI
– Path Finding
– Finite State Machine
– …







Avatar
Combat System
FX System
Script System
Trading System
Number System
…
32
Game Development Tools

Visual C/C++
– .Net 2003
– Visual C/C++ 6.0+ SP5

DirectX
– Current 9.0c



NuMega BoundsChecker
Intel vTune
3D Tools
– 3dsMax/Maya/Softimage

In-house Tools
33
Game System Analysis
34
What Will We Talk Here



Idea about System Analysis (SA)
Mind mapping
Case Study - Our Term Project
35
Why System Analysis (1/2)

For 程式結構 Analysis
– Program modulus
– Tools

To Identify 工作量
– Programs/tools under development

For 資源 management
–
–
–
–

Man month
How many programmers ?
Development tools ?
Specific tools ?
For Job Dependency Analysis
36
Why System Analysis (2/2)

To make 技術可行性 Analysis
– R&D ?

Pre-processor for
– Technical design document
– Project management

Bridge from Game Design to Programming
37
Something about System Analysis
 No Standard Procedures
 It’s Not a Theory, Just Something Must Be Done!
 You Can Have Your Own Method
– UML
– Mind mapping (心智圖法)
» This is the one we will use for this course
– …
38
My System Analysis Steps





Brainstorming
Integration
Dependency Analysis
Create the Project
Write the Technical Design Document (TDD)
39
Brainstorming



Based on the Game Design to Put Everything As
Many As You Could
Use Mind mapping
Including
– Game system
» Combat / Village / Puzzle / …
– Program modulus
» Camera / PC control / NPC AI / UI / FX /…
– Tools
» Level editor / Scene editor / …
– Entities in games
» Characters / vehicle / terrain / audio / …
40
Integration




Confirm the Resource Limitation
Technical Implement Possibility
Put All Related Items Together
Man Month Analysis
– How many ?
– Who ?

Jobs/System Identification
41
Dependency Analysis

Sort the Jobs
– By dependency
– By programmers

Prototype for scheduling
42
System Analysis – Create the Project






Scheduling
Job Assignment
Resource Allocation
Check points
Milestones
Risk Management Policy
43
Write the Technical Design Document








Specification
Resources
Design in details
Implement Methods (工法)
Algorithms
The Project
Output in Each Milestone
SOP (optional)
44
Mind Map



心智圖法
A Radiant Thinking Tool
Applications
–
–
–
–
–
–

讀書心得
Proposal
上課筆記
遊記
System Analysis
…
Reference
– Program
» Visio
» MindManager
– Tony Buzan, Barry Buzan, “The Mind Map Book: How to Use
Radiant Thinking to Maximize Your Brain's Untapped
Potential”
45
46
47
Mind Map Demo Using MindManager


Use MindManager X5 pro
Developed By MindJet
48
Term Project
System Analysis
49
Game Design (1/3)






Real-time Strategy War Game
Mission-based Levels
Mouse-driven Controls
Player vs Computer
State-based AI
Group Movement
50
Game Design (2/3)

PC – Controlled by Player
– “Blue Team”
» Move
» Attack
– AI
» Standby
» Anti-attack

NPC – Controlled by Computer
– “Red Team” - Enemy
» Triggered by time table
– AI
»
»
»
»
Move
Standby
Anti-attack
Attack
51
Game Design (3/3)

Game AI
– Finite state machine
– Path finding
» 3D
» A*
– Steering behavior
» Flocks / Schools / Herds
» http://www.red3d.com/cwr/boids/
– AI Game Programming Wisdom, Charles River Media (1 & 2)
52
Use Mind Map Tool for SA

Run MindManager.exe
53
Game Main Loop
54
Win32 Application (1/3)
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
...
// register window class
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.style
= CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = KKMainProc;
...
RegisterClassEx(&wc);
...
// the main loop
KKMainLoop();
// unregister the window class
UnregisterClass(wc.lpszClassName, hInst);
return id;
}
55
Win32 Application (2/3)
LRESULT CALLBACK KKMainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT l = FALSE;
...
// switch for all incoming messages from WindowsX
switch (uMsg) {
case WM_KEYDOWN:
...
l = TRUE;
break;
...
}
// echo the result
if (l) {
return l;
}
else {
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
56
Win32 Application (3/3)
void KKMainLoop()
{
MSG msg;
BOOL kkBeQuit = FALSE;
// the main loop
while (!kkBeQuit) {
// check window's messages
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
kkBeQuit = TRUE;
}
// invoke the WindowsX to handle the incoming messages
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// do your jobs here, for example, check the timing and do something in regular
...
}
}
57
Event-driven Programming (1/3)

Win32 Programs Are Event-driven
– Messages = events
– So as all windows system (for example : X window)


We Need a Loop to Check All Incoming Events
The Loop
– Check all incoming events (messages)
– Handle the events
– Check timing and do something in regular

Incoming Events
– Interrupts
– System requests
58
Event-driven Programming (2/3)

Timers (do something in regular timing)
– The sub-system to handle timing
– Must be precise to at least 1 ms or less
» 30fps = 1/30 second = 33.333… ms
– On win32 platform, you can use “performance counter” instead of
the win32’s “WM_TIMER” message
» For windows9x, WM_TIMER = 18.2 fps (maximum)

Events
– Input devices
» Mouse
» Keyboard
– Something coming from network
– System requests
» Re-draw
» Losing/getting the input focus
» …
59
Event-driven Programming (3/3)

Therefore, You Can Have Two Types of Jobs
(Callbacks) to Do (Call)
– In regular
» Timers callbacks
– By requests
» Input device callbacks

Same As a Game Main Program
– A game is an interactive application
– A game is time-bound
»
»
»
»
Rendering in 30fps or 60fps
Motion data in 30fps
Game running in 30fps
…
60
Implement the Timer (1/6)

Use Performance Counter on PC
// timers data structure
typedef struct {
BOOL beAble;
// is the timer is enabled/disabled ?
BOOL be1st;
// is this the 1st time for the timer to be checked
// after last initialization ?
BOOL beLockFps; // is locked on FPS ?
double initTime; // initial time
double timeInv;
// system ticks for one frame
double nxtTime; // next checking time
void (*timer)(int); // timer's callback
double resetTime; // reset time
} TIMERs, *TIMERptr;
61
Implement the Timer (2/6)
/*---------------------------------------------------------------------initialize a timer and bind a user-defined timer callback
------------------------------------------------------------------------*/
void FyBindTimer(DWORD id, float fps, void (*fun)(int), BOOL beLock)
{
if (id < 0 || id >= MAXTIMERS) return;
/* assign the timer's callback */
fyTimer[id].timer = fun;
/* set lock-to-fps flag */
fyTimer[id].beLockFps = beLock;
/* calculate the ticks for one frame */
fyTimer[id].timeInv = (double) (fyFreq) / (double) fps;
fyTimer[id].be1st = TRUE;
fyTimer[id].beAble = TRUE;
}
62
Implement the Timer (3/6)
/*-----------------------------------get current system clock tick
--------------------------------------*/
double FYGetCurrentSystemTick()
{
LARGE_INTEGER timeCount;
/* get current tick */
QueryPerformanceCounter(&timeCount);
return (double) timeCount.QuadPart;
}
/*
// get the system ticks for one second
QueryPerformanceFrequency(&timeFreq);
fyFreq = timeFreq.LowPart;
*/
63
Implement the Timer (4/6)
/*--------------------check all timers
----------------------*/
void FYInvokeTimer()
{
int i, skipS;
double dTime;
// get current time
dTime = FYGetCurrentSystemTick();
for (i = 0; i < MAXTIMERS; i++) {
if (fyTimer[i].beAble && fyTimer[i].timer != NULL) {
// for the first time .....
if (fyTimer[i].be1st) {
// initialize the timer
fyTimer[i].be1st = FALSE;
fyTimer[i].initTime = dTime;
fyTimer[i].nxtTime = dTime + fyTimer[i].timeInv;
(*(fyTimer[i].timer))(1);
}
64
Implement the Timer (5/6)
else {
if (fyTimer[i].beLockFps) {
if (dTime >= fyTimer[i].nxtTime) {
// calculate skip frames
skipS = (int)((dTime - fyTimer[i].nxtTime) / (double)fyTimer[i].timeInv) + 1;
// get next checking time
fyTimer[i].nxtTime += (double) (skipS * fyTimer[i].timeInv);
// check some abnormal conditions
...
// invoke the timer callback
(*(fyTimer[i].timer))(skipS);
}
}
else {
(*(fyTimer[i].timer))(1);
}
}
}
}
}
65
Implement the Timer (6/6)
/*-----------------------------------------------------------invoke the TheFly3D system to handle the timers
--------------------------------------------------------------*/
void FyInvokeTheFly(BOOL beTimer)
{
MSG msg;
if (fyBeQuit) return;
while (!fyBeQuit) {
// check window's messages
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) fyBeQuit = TRUE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// check the timer
if (beTimer && fyBeTimer) FYInvokeTimer();
}
}
66
Game Loop (1/2)

Single Player
Loop
Check game over
y
Exit the loop
n
Peek player input
Implement
timer callback
Rendering
67
Game Loop (2/2)

Network Client
Loop
y
Check game over
Exit
n
Peek user input
Receive messages
From network
Timer callbacks
Send messages
To network
Rendering
68
Jobs in Regular (Typically)





Check Win/Loose
Check Quit
Objects Moving …
Play Character’s Motion to Next Frame
Play Animation to Next Frame
– Models
– Textures
– …


Perform Some Game Calculation
Perform Geometry Calculation
– LOD




Perform AI “Thinking”
Perform Collision Detection
Perform the 3D Rendering
…
69
Jobs By Request (Typically)

Mouse Input
–
–
–
–

Press/release the mouse button
Drag
Double-click
Move
Keyboard Input
– Hotkey
– Typing

Gamepad
– Same as the hotkey



Network
System
…
70
TheFly3D Game Engine
- The Main Program
71
The Main Program
void main(int argc, char **argv)
{
// create the game world & 3D scene
...
// set Hotkeys
FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE);
...
// define some mouse functions
FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, EndPivot, NULL);
...
// bind a timer for rendering, frame rate = 60 fps
FyBindTimer(0, 60.0f, RenderIt, TRUE);
// bind a timer for game AI, frame rate = 30 fps
FyBindTimer(1, 30.0f, GameAI, TRUE);
// invoke the system
FyInvokeTheFly(TRUE);
}
72
Hotkey Callback
//------------------// quit the game
//------------------void QuitGame(WORLDid gID, BYTE code, BOOL value)
{
if (code == FY_ESCAPE) {
if (value) {
FyWin32EndWorld(gID);
}
}
}
73
Mouse Callback
/*----------------------------------------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;
}
}
74
Timer Callback
//---------------------------------------------------------------------------------------// Render callback which will be invoked by TheFly3D every 1/60 second
//---------------------------------------------------------------------------------------void RenderIt(int skip)
{
FnViewport vp;
FnWorld gw;
// render the scene
vp.Object(vID);
vp.Render(cID, TRUE, TRUE);
// perform double-buffering
gw.Object(gID);
gw.SwapBuffers();
}
75
What will be included in TheFly3D
Game
NPC System
Virtual Agent
Trading System
Combat System
FX System
Terrain
Character Dynamics Sound FX
Collision
3D Scene Mngmt
3D Graphics API
2D Sprite
Game AI
Story
Gamepad
2D API
Hardware
Script System
UI
Audio
Network
Input Device
OS API
Game Play
Layer
Engine
Layer
System
Layer
Under Development
76
Introduction to TheFly3D

3D Graphics/Game Programming Library
– Using C++

Based on DirectX9.0a
– Currently



A Frame Work for 3D Graphics Developers
According to the Experiences of the Author
Some Game Development Features are
added
Scene Management System
– Scene tree



Built-in Visibility Culling
Characters
Current version 0.5a1 (1009, 2004)
– Win32 version
77
Development Environment



.net2003 Visual C++ 7.1
DirectX9.0a SDK
Include files
– TheFly.h
– TheFlyWin32.h (win32 version + D3D)

Linked libraries
– TheFlyLibD_05a1.lib
– d3d9.lib d3dx9.lib
78
Create the Visual C++ Project for “TheFly3D”

Create folders for TheFly3D API
– …\include
– …\lib





New a Win32 Application Project
Set the additional include/library directories
to TheFly3D API
Add DirectX 9.0 include/lib to additional
search directories
Add d3d9.lib d3dx9.lib to additional
dependencies
Add TheFly.h, TheFlyWin32.h,
TheFlyLibD_xxxx.lib into the project
79
The 1st TheFly3D Program – hello.cpp




Create
Create
Create
Create
a 3D world
a viewport
a scene
3D entities
– A camera
– A teapot model
– A light source


Translate the camera to show the model
Bind callbacks to make the program
interactive
80
Demo - Hello
Do it!
81
The Basics to Write TheFly3D Program

All Win32 code is transparent here
– void main(int argc, char *argv[])

ID & Function class
– TheFly3D creates the objects for you
» Return the ID
– TheFly3D owns the objects
– You have the right to handle the objects
» Use function classes
// create a viewport
vID = gw.CreateViewport(ox, oy, ww, hh);
FnViewport vp;
vp.Object(vID);
vp.SetBackgroundColor(0.3f, 0.3f, 0.0f);
82
Initialize TheFly3D

The 1st function to use TheFly3D is
– FyWin32CreateWorld()


After the calling successfully, you can get the
non-zero ID of a world object
Assign the ID to a world function for
manipulating the world object
// create a new world
WORLDid gID = FyWin32CreateWorld(Tester", 0, 0, 800, 600, 16, FALSE);
FnWorld gw;
gw.Object(gID);
gw.SetEnvironmentLighting(0.5f, 0.5f, 0.8f);
gw.SetTexturePath("Data\\textures");
83
The World in TheFly3D

A world is a set of layers where the 3D
objects acts on
A World
3D Graphics Layers
Frame Buffers
(front + back)
84
The 3D Grpahics Layer



A 3D graphics layer is a projection of the
rendering of a 3D world
The 3D world we call the “Scene”
The place for projection we call the “Viewport”
Backdrop
Lights
Camera
3D Models
Board
A 3D Scene
85
The Viewports & Scenes



TheFly3D supports the multiple viewports
A scene can be rendered on different
viewports
Viewports & scenes are created by the world
object
// create a new world
WORLDid gID = 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);
86
Something about the Scene


A scene is not a “real” 3D object, just a “set”
of 3D objects
A scene provides multiple rendering group
concept to handle the 1st priority sorting for
the rendering of 3D objects
87
The 3D Entities – Objects


A 3D scene is constructed by a set of
“objects” which are the basic entities in a
scene.
An object is a carrier to carry real 3D data
including cameras, lights, 3D models, audio
sources, billboards, etc.,






Models
Cameras
Lights
Terrains
Objects are created/deleted by his/her host
scene
Objects can be switched between scenes
88
The Objects Can …







Can
Can
Can
Can
Can
Can
Can
have shapes (geometric data)
be grouped (hierarchy)
move (transformation)
look alike (clone or data sharing)
perform (animation, deformation)
be affected (lighted, listened)
be changed (modification)
89
A Scene Object
Hierarchy
Parent Object
Parameters
Etc
Transformation
Move
Animation
Motion Data
Shape
Geometric Data
Clone
90
A Model



An object to carry a set of geometry data is
a model object
You can load the model data from files
TheFly3D loads .cw3 & .lz3
// create 3D entities
nID = scene.CreateModel(ROOT);
FnModel model;
model.Object(nID);
// load a teapot
model.Load("Teapot.cw3");
91
TheFly3D Scene Tree
A tree-based representation
 Simplified scene graph

Root
92
TheFly3D Scene Tree Is Simplified Scene Graph
A tree-based representation
 Simplified scene graph

Root
93
Object Hierarchy
nID = scene.CreateModel(ROOT);
cID = scene.CreateCamera(ROOT);
FnModel model;
model.Object(nID);
model.SetParent(cID);
cID
nID
94
Clone a Model Object
MODELid nID = scene.CreateModel(ROOT);
FnModel model;
model.Object(nID);
MODELid nID1 = model.Instance();
nID
nID1
Data
instance
95
TheFly3D Model Functions (1/2)







void model.SetParent(parent_object_ID);
void model.Show(be_show);
void model.SetOpacity(opacity);
 opacity = 0.0 – 1.0
void model.SetRenderMode(mode);
 mode = WIREFRAME or TEXTURE
MODELid clonedID = Model.Instance();
void model.ChangeScene(sID);
BOOL model.Load(char *file);


Load a .cw3 or .lz3 model file to a model object
void model.ShowBoundingBox(beShow);
96
TheFly3D Model Functions (2/2)

Model.SetRenderOption(item, value);
 (item, value) =



(Z_BUFFER, TRUE/FALSE)
(Z_BUFFER_WRITE, TRUE/FALSE)
(ALPHA, TRUE/FALSE)








Add/remove the model to/from alpha sorting list
(FOG, TRUE/FALSE)
(SPECULAR, TRUE/FALSE)
(LIGHTING, TRUE/FALSE)
(ANTIALIASING, TRUE, FALSE)
(TEXTURE_ADDRESS_MODE, WRAP_TEXTURE /
MIRROR_TEXTURE / CLAMP_TEXTURE / BORDER_TEXTURE /
MIRROR_ONCE_TEXTURE)
(SOURCE_BLEND_MODE BLEND_ZERO / BLEND_ONE /
BLEND_SRC_COLOR / BLEND_INV_SRC_COLOR /
BLEND_SRC_ALPHA / BLEND_INV_SRC_ALPHA /
BLEND_DEST_ALPHA / BLEND_INV_DEST_ALPHA /
BLEND_DEST_COLOR / BLEND_INV_DEST_COLOR /
BLEND_SRC_ALPHA_SAT / BLEND_BOTH_SRC_ALPHA /
BLEND_BOTH_INV_SRC_ALPHA
(DESTINATION_BLEND_MODE values are same as the
97
SOURCE_BLEND_MODE
Coordinate System

Every model should have its own local
coordinate system (local space)


The space when it’s modeled
To its parent model, it is in the global space

The space for reference
z
Z
y
x
Y
X
98
Transformation

Three basic linear transformations




Translate
Rotate
Scale
z
y
Principles



Right-handed rule
v’ = v M0 M1
Matrix in 12-element (I call the M12)
Rotation matrix
Translation vector
a0
a3
a6
a9
x
a1 a 2 0
a4 a 5 0
a7 a 8 0
a10 a11 1
99
Translation
model.Translate(dx, dy, dz, op);
x’ = x + dx
y’ = y + dy
z’ = z + dz
T =
1
0
0
dx
0
1
0
dy
(dx dy dz)
0
0
1
dz
100
Rotation
i.e. rotate with z axis
model.Rotate(Z_AXIS, 30.0f, op);
x’ = x cosq – y sinq
y’ = x sinq + y cosq
z’ = z
Rz =
cosq
-sinq
0
0
sinq
cosq
0
0
0
0
1
0
z
y
x
101
Scaling
model.Scale(sx, sy, sz, op);
x’ = x * sx
y’ = y * sy
z’ = z * sz
T =
sx
0
0
0
0
sy
0
0
0
0
sz
0
102
Matrix Operations

Matrix operation
Z
– REPLACE, LOCAL, GLOBAL
Y
op = LOCAL
X
op = REPLACE
[ML] [M] [MG]
z
x
y
op = GLOBAL
Object Transformation Matrix
103
TheFly3D Model Transformation Functions






model.Translate(x, y, z, op);
 op = LOCAL, GLOBAL, or REPLACE
model.Rotate(axis, angle, op);
 op = LOCAL, GLOBAL, or REPLACE
 axis = X_AXIS, Y_AXIS, Z_AXIS
model.Scale(sx, sy, sz, op);
 op = LOCAL, GLOBAL, or REPLACE
model.Quaternion(w, x, y, z, op);
 w : the scalar part of the quaternion
 x, y, z : the vector part of the quaternion
 The quaternion should be a unit quaternion
 op = LOCAL, GLOBAL, or REPLACE
model.SetMatrix(M12, op);
 M12 : a M12 matrix
 op = LOCAL, GLOBAL, or REPLACE
float *model.GetMatrix();
 Get the pointer of the model object’s matrix
104
Movements

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 / right
…
Turn right / left
Move up
Move right
Move forward
105
Facing 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
z
 But for a camera :



-z axis is the facing direction
y axis is the up direction
y
x
z up + facing to -y
106
Movements
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
Turn right / left
scene.

Move up
Move right
Move forward
107
Move Forward
new position = old position +
distance *(facing direction in unit)
108
Move Forward (2)





The object has a local coordinate system.
Align a local axis of the object with the facing
direction
Make a translation matrix to move the object align
the axis
Apply the matrix first before to apply the existing
transformations
Then the object is moving forward!
FnModel model;
model.Object(nID);
model.Translate(0.0f, -dist, 0.0f, LOCAL);
109
Turn Right/Left

An example
-1
-1
-1
M = T1 * R1 * R2 * Rx(angle) * R2 * R1 * T1
T1 = / 1 0 0
0 1 0
0 0 1
-x -y -z
0
0
0
1 /
R1 = / cs2 -sn2 0 0
-z
sn2 cs2 0 0
0
0 1 0
0
0 0 1 /
R2 = / cs1 0 -sn1 0
y
0 1
0 0
sn1 0 cs1 0
0 0
0 1 /
Rx = / 1
0 0 0
0 cs sn 0
0 -sn cs 0
0
0 0 1 /
110
Turn Right/Left (2)





The object has a local coordinate system.
Align a local axis of the object with the up direction
Make a rotation matrix to turn the object along the
axis
Apply the matrix first before to apply the existing
transformations
Then the object is turning!
FnModel model;
model.Object(nID);
model.Rotate(Z_AXIS, -angle, LOCAL); // turn right
111
Move Forward – Graphics Approach
new position = old position +
distance *(facing direction in unit)
112
Move Forward – Our Approach





The object model has a local coordinate
system
Align a local axis of the object with the
facing direction
Make a translation matrix to move the
object align the axis
Apply the matrix first before to apply the
existing transformations
Then the object is moving forward!
FnModel model;
model.Object(nID);
model.Translate(0.0f, -dist, 0.0f, LOCAL);
113
Turn Right/Left – Graphics Approach

An example
-1
-1
-1
M = T1 * R1 * R2 * Rx(angle) * R2 * R1 * T1
T1 = / 1 0 0
0 1 0
0 0 1
-x -y -z
0
0
0
1 /
R1 = / cs2 -sn2 0 0
-z
sn2 cs2 0 0
0
0 1 0
0
0 0 1 /
R2 = / cs1 0 -sn1 0
y
0 1
0 0
sn1 0 cs1 0
0 0
0 1 /
Rx = / 1
0 0 0
0 cs sn 0
0 -sn cs 0
0
0 0 1 /
114
Turn Right/Left – Our Approach





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 axis
Apply the matrix first before to apply the
existing transformations
Then the object is turning!
FnModel model;
model.Object(nID);
model.Rotate(Z_AXIS, -angle, LOCAL); // turn left
115
Terrain




A terrain is a place for 3D objects to walk on
A terrain is generated from a model object
Neighboring triangles are the next searching
target for current triangle for terrain
following
A terrain for terrain following is not the same
as the terrain in visual
116
Terrain in TheFly3D
// create a terrain object
tID = scene.CreateTerrain();
FnTerrain t;
t.Object(tID);
// load a terrain model (just like a regular model)
// but a terrain is invisible in default
t.Load("test_terrain_following.cw3");
// generate the neighboring data for terrain following
t.GenerateTerrainData();
117
Terrain Following
Terrain Following (3D)
offset
Terrain Following (2D)
offset
118
TheFly3D Model Movement Functions (1/2)






void model.GetPosition(pos);
 pos is a 3D vector to get the position of the model object
void model.GetDirection(faceDir, upDir);
 If you just want to get one of the directions, just send NULL
pointer to the one that you do not want to query
void model.SetPosition(pos);
 The position is related to its parent object
void model.SetDirection(faceDIr, upDir);
 If you just want to set one of the directions, just send NULL
pointer to the one that you do not want to set
Void model.SetDirectionAlignment(fDAxis, uDAxis);
 You can change the local axes for facing and up directions
BOOL model.PutOnTerrain(tID, be3D, offset, probeFront,
probeBack, probeAngle, hightLimit)
 tID is a terrain object
 be3D = TRUE for 3D terrain following
 Offset is the height above the terrain
 hightLimit is the terrain following height limit
119
 Return TURE if you successfully put the model on a terrain
Probe for a Model on Terrain
probeBack
probeFront
: terrain following check point
120
TheFly3D Model Movement Functions (2/2)


int model.MoveForward(dist, beTerrainFollow, be3D, offset);
 If you just want to move the model forward but not on
terrain, set beTerrainFollow to FALSE
 You should put a model on terrain first. Then you can move
it forward on terrain.
void model.TurnRight(float angle);
 Angle is in degree
121
Character Introduction
122
123
Motion Production – by 3D Tools
 Keyframe System
 3DS MAX
 Softimage
 Maya
 ...
 Low Cost (Relatively)
 Easy to Combine Animations
 Hard to Make “Good” Motions
 Long Production Time
124
Motion Production – by Motion Capture
 Motion Capture
 Optical
 Magnetic
 ...
 Costly Investment
 Every Frame is a Keyframe
 Very Live Motion
 Need Post-processing for Games
 Hard to Combine Motions
125
A Segmented Character


A character is composed by a set of
models with motion data to simulate a
live creature in real world
A segmented character :
head
up arm
hand
body
fore arm
groin
thigh
foot
shin
126

The scene tree of a segmented
character :
Base
groin
body
head
thigh_r
thigh_l
up_arm_r
shin_r
up_arm_l
shin_l
fore_arm_l
hand_l
fore_arm_r
hand_r
foot_r
foot_l
127
The Root-Base Concept (1/2)

Use root-base structure to construct the
character
Base
Root
(groin)
Base
128
The Root-Base Concept (2/2)







A character has some models to be the
geometry roots of the character system.
The root plays as the gravity center of the
character
The root can be translated and rotated
The others are joints
The joints can rotate only
A ghost object is added to be the parent of
the root, which is the base of the character
The base is the center for the character’s
movement. We move the base to perform
character’s moves.
129
Motion Data - Pose




A set of frames to describe a character’s
motion
Walk, run, attack, …
Keyframed or non-keyframed
Motion data in
–
–
–
–
Position (pivot) + quaternion
Position (pivot) + Euler angles
Position (pivot) + (q, n)
Matrix
walk
run
attack
fall
130
Load a Character
FnScene scene;
CHARACTERid actorID;
Scene.Object(sceneID);
actorID = scene.LoadCharacter("m012.lxx");
.lxx is a character
description file
which is a bone-skin
character
actorID = scene.LoadCharacter(“m60a3.lzc”);
.lzc is a segmented
character file
131
Play a Pose
BLENDTREEid btID = actor.GetBlendTree();
FnBlendTree bt;
bt.Object(btID);
aaaID = bt.CreateAnimationNode(2); // 2nd motion
// start to play a pose (1st time)
actor.PlayBlendNode(aaaID, (float) 0, START, TRUE);
// continue to play a pose
actor.PlayBlendNode(CURRENT_ONE, (float) skip, LOOP, TRUE);
132
Make a Character to Move Forward
FnCharacter actor;
// play walking pose
actor.Object(actorID);
actor.PlayBlendNode(CURRENT_ONE, (float) skip, LOOP, TRUE);
// move it forward
actor.MoveForward(dist, beTF, be3D, offset);
133
TheFly3D Character Movement Functions (1/2)





void actor.GetPosition(pos);
 pos is a 3D vector to get the position of the character
void actor.GetDirection(faceDir, upDir);
 If you just want to get one of the directions, just send NULL
pointer to the one that you do not want to query
void actor.SetPosition(pos);
 The position is related to its parent object
void actor.SetDirection(faceDIr, upDir);
 If you just want to set one of the directions, just send NULL
pointer to the one that you do not want to set
BOOL actor.PutOnTerrain(tID, be3D, offset, probeFront,
probeBack, probeAngle, hightLimit)
 tID is a terrain object
 be3D = TRUE for 3D terrain following
 Offset is the height above the terrain
 hightLimit is the terrain following height limit
 Return TURE if you successfully put the character on a
terrain
134
TheFly3D Character Movement Functions (2/2)


void actor.MoveForward(dist, beTerrainFollow, be3D, offset);
 If you just want to move the character forward but not on
terrain, set beTerrainFollow to FALSE
 You should put a character on terrain first. Then you can
move it forward on terrain.
 A character is always using his local -y-axis as facing
direction
void actor.TurnRight(float angle);
 Angle is in degree
 A character is always using his local z-axis as up direction
135
TheFly3D Character Functions





MODELid actor.GetBaseObject();
 You can get the base object of the character
MODELid actor.GetObjectByName(name);
 You can get the each model part of the character by its
name in character file
 For a bone-skin character, this function can get the bones in
the skeleton
BOOL actor.PlayFrame(frame, beIncludeBase);
 The function is to play the motion for a specific frame
 Set beIncludeBase = TRUE to play the base object’s motion
BLENDTREEid actor.GetBlendTree();
 Get the character’s blend tree data
 You can define all poses in a blend tree (blend nodes)
float actor.PlayBlendNode(btNodeID, skipFrame, mode,
beIncludeBase);
 All poses for a character is defined as a blend node
 Skip frames can be floating-point
 Mode can be ONCE or LOOP!
136
Game Mathematics
137
Essential Mathematics for Game Development
Matrices
 Vectors
 Fixed-point Real Numbers
 Triangle Mathematics
 Intersection Issues
 Euler Angles
 Angular Displacement
 Quaternion
 Differential Equation Basics

138
Matrices

Matrix basics
– Definition
A
=
(aij) =
C
=
A
C
=
A + B
a11 .. a1n
.
.
.
.
am1 .. amn
– Transpose
T
cij = aji
– Addition
cij = aij + bij
139
– Scalar-matrix multiplication
C
=
aA
cij = aaij
– Matrix-matrix multiplication
C
=
A B
cij
r
= Saikbkj
k = 1
140

Transformations in Matrix form
– A point or a vector is a row matrix (de facto convention)
V = [x y z]
– Using matrix notation, a point V is transformed under
translation, scaling and rotation as :
V’ = V + D
V’ = VS
V’ = VR
where D is a translation vector and
S and R are scaling and rotation matrices
141
– To make translation be a linear transformation, we
introduce the homogeneous coordinate system
V (x, y, z, w)
where w is always 1
– Translation Transformation
x’ = x + Tx
y’ = y + Ty
z’ = z + Tz
V’ = VT
[x’ y’ z’ 1] = [x y z 1]
= [x y z 1] T
1
0
0
Tx
0
0
1
0
0
1
Ty Tz
0
0
0
1
142
– Scaling Transformation
x’ = xSx
y’ = ySy
z’ = zSz
V’ = VS
Sx
[x’ y’ z’ 1] = [x y z 1] 0
0
0
0
Sy
0
0
0
0
Sz
0
0
0
0
1
= [x y z 1] S
Here Sx, Sy and Sz are scaling factors.
143
– Rotation Transformations
Rx
Ry
Rz
=
1
0
0
0 cosq sinq
0 -sinq cosq
0
0
0
0
0
0
1
=
cosq
0
sinq
0
0 -sinq
1
0
0 cosq
0
0
0
0
0
1
=
cosq sinq 0
-sinq cosq 0
0
0
1
0
0
0
0
0
0
1
144
– Net Transformation matrix
[x’ y’ z’ 1] = [x y z 1] M1
and
[x” y” z” 1] = [x’ y’ z’ 1] M2
then the transformation matrices can be concatenated
M3 = M1 M2
and
[x” y” z” 1] = [x y z 1] M3
– Matrix multiplication are not commutative
M1 M2 = M2 M1
145
Vectors


A vector is an entity that possesses magnitude
and direction.
A 3D vector is a triple :
– V = (v1, v2, v3), where each component vi is a scalar.

A ray (directed line segment), that possesses
position, magnitude and direction.
(x1,y1,z1)
V = (x2-x1, y2-y1, z2-z1)
(x2,y2,z2)
146

Addition of vectors
X = V + W
= (x1, y1, z1)
= (v1 + w1, v2 + w2, v3 + w3)
W
V + W
W
V

V + W
V
Length of vectors
|V| = (v12 + v22 + v32)1/2
U
= V / |V|
147

Cross product of vectors
– Definition
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)
– Application
» A normal vector to a polygon is calculated from 3 (non-collinear)
vertices of the polygon.
Np
V2
polygon defined by 4 points
Np = V1 X V2
V1
148
» Normal vector transformation
N(X) = detJ J-1T N(x)
where X = F(x)
dF(x)
J the Jacobian matrix, Ji(x) =
dxi
"Global and Local Deformations of Solid Primitives"
Alan H. Barr
Computer Graphics Volume 18, Number 3 July 1984
(take scaling as example)
149

Dot product of vectors
– Definition
|X| = V . W
= v1w1 + v2w2 + v3w3
– Application
V
q
cosq =
W
V
.
W
|V||W|
150
Fixed Point Arithmetics (1/2)

Fixed Point Arithmetics : N bits (signed) Integer
– Example : N = 16 gives range –32768  ă  32767
– We can use fixed scale to get the decimals
a = ă / 28
8 integer bits
11
1
8 fractional bits
ă = 315, a = 1.2305
151
Fixed Point Arithmetics (2/2)

Multiplication then Requires Rescaling
e = a.c = ă / 28 . ĉ / 28
 ĕ = (ă . ĉ) / 28

Addition just Like Normal
e = a+c = ă / 28 + ĉ / 28
ĕ=ă+ĉ
152
Fixed Point Arithmetics - Application

Compression for Floating-point Real Numbers
– 4 bytes reduced to 2 bytes
– Lost some accuracy but affordable
– Network data transfer

Software 3D Rendering
153
Triangular Coordinate System
ha
(xa,ya,za)
Ac p
hb
h
(xb,yb,zb)
Ab
Aa
hc (x ,y ,z )
c
h=
Aa
A
ha +
Ab
A
hb +
c
Ac
A
c
hc
where A = Aa + Ab + Ac
If (Aa < 0 || Ab < 0 || Ac < 0) than
the point is outside the triangle
“Triangular Coordinate System”
154
Triangle Area – 2D
Area of a triangle in 2D
A=½
xa ya
xb yb
xc yc
xa ya
= ½ (xa*yb + xb*yc + xc*ya – xb*ya – xc*yb – xa*yc)
(xa,ya,za)
(xb,yb,zb)
(xc,yc,zc)
155
Triangle Area – 3D
Area of a triangle in 3D
A = ½ (N. Sum(Pi1 cross Pi2))
where (i1, i2) = (a,b), (b,c), (c,a)
float GmArea3(float *x0, float *x1, float *x2, float *n)
{
float area, len, sum1, sum2, sum0;
len = (float) sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) * 2.0f;
/* find sum of cross products */
sum0 = x1[1] * (-x0[2] + x2[2]) + x2[1] * (-x1[2] + x0[2]) +
x0[1] * (-x2[2] + x1[2]);
sum1 = x1[2] * (-x0[0] + x2[0]) + x2[2] * (-x1[0] + x0[0]) +
x0[2] * (-x2[0] + x1[0]);
sum2 = x1[0] * (-x0[1] + x2[1]) + x2[0] * (-x1[1] + x0[1]) +
x0[0] * (-x2[1] + x1[1]);
/* find the area */
return = (sum0 * n[0] + sum1 * n[1] + sum2 * n[2]) / len;
}
156
Triangular Coordinate System - Application




Terrain Following
Hit Test
Ray Cast
Collision Detection
157
Intersection


Ray Cast
Containment Test
158
Ray Cast – The Ray


Shot a Ray to Calculate the Intersection of the Ray
with Models
Use Parametric Equation for a Ray
{



x = x0 + (x1 – x0) t
y = y0 + (y1 – y0) t,
z = z0 + (z1 – z0) t
t = 0, 8
When t = 0, the Ray is on the Start Point (x0,y0,z0)
Only the t  0 is the Answer Candidate
The Smallest Positive t is the Answer
159
Ray Cast – The Plane







Each Triangle in the Models has its Plane Equation
Use ax + by + cz + d = 0 as the Plane Equation
(a, b, c) is the Plane Normal Vector
|d| is the Distance of the Plane to Origin
Substitute the Ray Equation into the Plane
Solve the t to Find the Intersect
Check the Intersect Point Within the Triangle or
not by Using “Triangle Area Test” (p. 154)
160
2D Containment Test
Intersection = 1, inside
Intersection = 2, outside
(x0, y0)
Intersection = 0, outside
Trick : Parametric equation for a ray which is parallel to the x-axis
{
x = x0 + t
y = y0
, t = 0, 8
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
161
3D Containment Test

Same as the 2D containment test
“if the No. of intersection is odd, the point is inside,
otherwise, is outside”
162
Euler Angles

A rotation is described as a sequence of rotations
about three mutually orthogonal coordinates axes
fixed in space
– X-roll, Y-roll, Z-roll
R(q1, q2, q3) represents an x-roll, followed by y-roll, followed by z-roll
R(q1, q2, q3) =
c 2c 3
c 2s 3
s1s2c3-c1s3 s1s2s3+c1c3
c1s2c3+s1s3 c1s2s3-s1c3
0
0
where si = sinqi and ci = cosqi

-s2
s 1c 2
c 1c 2
0
0
0
0
1
There are 6 possible ways to define a rotation
– 3!
163
Euler Angles & Interpolation


Interpolation happening on each angle
Multiple routes for interpolation
More keys for constrains
R
z
z
y
x
y
x
R

164
Angular Displacement

R(q, n), n is the rotation axis
rv
q
r
r Rr
rh
V = nxrv = nxr
n
n
rv
V
rh = (n.r)n
rv = r - (n.r)n , rotate into position Rrv
q
V
Rrv
Rrv = (cosq)rv + (sinq)V
->
Rr = Rrh + Rrv
= rh + (cosq)rv + (sinq)V
= (n.r)n + (cosq) (r - (n.r)n) + (sinq) nxr
= (cosq)r + (1-cosq) n (n.r) + (sinq) nxr
165
Quaternion








Sir William Hamilton (1843)
From Complex numbers (a + ib), i 2 = -1
16,October, 1843, Broome Bridge in Dublin
1 real + 3 imaginary = 1 quaternion
q = a + bi + cj + dk
i2 = j2 = k2 = -1
ij = k & ji = -k, cyclic permutation i-j-k-i
q = (s, v), where (s, v) = s + vxi + vyj + vzk
166
Quaternion Algebra
q1 = (s1, v1) and q2 = (s2, v2)
q3 = q1q2 = (s1s2 - v1.v2 , s1v2 + s2v1 + v1xv2)
Noncommutative
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)
167
Quaternion VS Angular Displacement
Take a pure quaternion p = (0, r)
and a unit quaternion q = (s, v) where qq = 1
and define Rq(p) = qpq-1 where q-1 = q for a unit quaternion
Rq(p) = (0, (s2 - v.v)r + 2v(v.r) + 2svxr)
Let q = (cosq, sinq n),
|n| = 1
Rq(p) = (0, (cos2q - sin2q)r + 2sin2q n(n.r) + 2cosqsinq nxr)
= (0, cos2qr + (1 - cos2q)n(n.r) + sin2q nxr)
Conclusion :
The act of rotating a vector r by an angular displacement (q, n)
is the same as taking this displacement, ‘lifting’ it into
quaternion space, by using a unit quaternion (cos(q/2),
168
sin(q/2)n)
Quaternion VS Rotation Matrix
q = (w,x,y,z)
1-2y2-2z2 2xy-2wz 2xz+2wy
2xy+2wz 1-2x2-2z2 2yz-2wx
2xz-2wy 2yz+2wx 1-2x2-2y2
0
0
0
0
0
0
1
169
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;
M0 M1 M2 0
M3 M4 M5 0
M6 M7 M8 0
0 0 0 1
}
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];
}
170
Quaternion Interpolation

Spherical linear interpolation, slerp
A
P
B
t
f
slerp(q1, q2, t) = q1
sin((1 - t)f)
sinf
+ q2
sin(tf)
sinf
171
Differential Equation Basics


Initial value problems
ODE
– Ordinary differential equation

Numerical solutions
– Euler’s method
– The midpoint method
172
Initial Value Problems

An ODE
.
x = f (x, t)
where f is a known function
.
x is the state of the system, x is the x’s time derivative
.
x & x are vectors
x(t0) = x0, initial condition


Vector field
Solutions
– Symbolic solution
– Numerical solution
Start here
Follow the vectors …
173
Euler’s Method
x(t + Dt) = x(t) + Dt f(x, t)

A numerical solution
– A simplification from Tayler series


Discrete time steps starting with initial value
Simple but not accurate
– Bigger steps, bigger errors
– O(Dt2) errors


Can be unstable
Not even efficient
174
The Midpoint Method
Error term
.
..
2
Concept : x(t0 + h) = x(t0) + h x(t0) + h /2 x(t0) + O(h3)
Result : x(t0+h) = x(t0) + h(f(x0 + h/2 f(x0))
Method : a. Compute an Euler step
Dx = Dt f(x, t)
b. Evaluate f at the midpoint
fmid = f((x+Dx)/2, (t+Dt)/2)
c. Take a step using the midpoint
x(t+Dt) = x(t) + Dt fmid
c
b
a
175
The Runge-Kutta Method


Midpoint = Runge-Kutta method of order 2
Runge-Kutta method of order 4
– O(h5)
k1
k2
k3
k4
=
=
=
=
h
h
h
h
f(x0, t0)
f(x0 + k1/2, t0 + h/2)
f(x0 + k2/2, t0 + h/2)
f(x0 + k3, t0 + h)
x(t0+h) = x0 + 1/6 k1 + 1/3 k2 + 1/3 k3 + 1/6 k4
176
Initial Value Problems - Application

Dynamics
– Particle system

Game FX System
177
Game Geometry
178
Game Models

Geometry
– Position / vertex normals / vertex colors / texture
coordinates

Topology
– Primitive
» Lines / triangles / surfaces / …

Property
– Materials
– Textures



Motion
Hierarchy
Level-of-detail
179
Geometry Data

Vertex position
– (x, y, z, w)
– In model space or screen spane

Vertex normal
– (nx, ny, nz)

Vertex color
– (r, g, b) or (diffuse, specular)

Texture coordinates on vertex
– (u1, v1), (u2, v2), …

Skin weights
– (bone1, w1, bone2, w2, …)
180
Topology Data

Lines
– Line segments
– Polyline
» Open / closed



Indexed triangles
Triangle Strips / Fans
Surfaces
– Non-uniform Rational B Spline (NURBS)

Subdivision
181
Indexed Triangles

Geometric data

Vertex data
 v0, v1, v2, v3, …
 (x, y, z, nx, ny, nz, tu, tv)
 or (x, y, z, cr, cg, cb, tu, tv, …)

polygon normal
Topology


v0
Face v0 v3 v6 v7
Edge table
vertex normal
v7
v3
v6
Right-hand rule for index
182
Triangle Strips
v0
v2
v6
v4
T0
T4
T2
T1
T5
T3
v5
v1
v7
v3
v0 , v1 , v2 , v3 , v4 , v5 , v6 , v7
“Get great performance to use triangle strips for
rendering on current hardware
183
Property on Surface



Material
Textures
Shaders
184
Materials

Material
– Ambient
» Environment
» Non-lighted area
– Diffuse
» Dynamic lighting
– Emissive
» Self-lighting
– Specular with shineness
» Hi-light
» View-dependent
» Not good for hardware rendering


Local illumination
For fixed function rendering pipeline
185
Textures

Textures
–
–
–
–
–
Single texture
Texture coordinate animation
Texture animation
Multiple textures
Alphamap
Lightmap
Base color texture
Material or vertex colors
186
Shaders

Programmable Shading Language
– Vertex shader
– Pixel shader

Procedural way to implement some process of
rendering
–
–
–
–
–
–
Transformation
Lighting
Texturing
BRDF
Rasterization
Pixel fill-in
187
Shader Pipeline
Vertex Data
Topology Data
Classic
Transform &
Lighting
Vertex Shader
Geometry
Stage
Clipping & Viewport Mapping
Texturing
Pixel Shader
Fog
Rasterizer
Stage
Alpha, Stencil, Depth Testing
188
Powered by Shader

















Per-pixel lighting
Motion blur
Volume / Height fog
Volume lines
Depth of field
Fur fighting
Reflection / Refraction
NPR
Shadow
Linear algebra operators
Perlin noise
Quaternion
Sparse matrix solvers
Skin bone deformation
Normal map
Displacement map
Particle shader


Procedural Morphing
Water Simulation
189
Motion Data


Time-dependent data
Transformation data
– Position
– Orientation

Formats
–
–
–
–
–
Pivot
Position vector
Quaternion
Eurler angles
Angular displacement
190
Level-of-detail

Discrete LOD
– Switch multiple resolution models run-timely

Continuous LOD
– Use progressive mesh to dynamically reduce the
rendered polygons

View-dependent LOD
– Basically for terrain
191
Progressive Mesh



Render a Model in Different Level-of-Detail at Runtime
User-Controlledly or Automatically Change the Percentage
of Rendered Vertices
Use Collapse Map to Control the Simplification Process
Collapse map
Index
0
1
2
3
4
5
6
7
8
Map
0
1
1
2
3
0
4
5
6
Vertex list
0
1
2
3
4
5
6
7
8
Triangle list
0
2
5
0
1
2
3
5
8
0
6
0
4
192
View-dependent LOD for Terrain - ROAM





Real-time Optimal Adapting Meshes (ROAM)
Use height map
Run-timely to re-construct the active (for
rendering) geometric topology (re-mesh) to get an
optimal mesh (polygon count) to improve the
rendering performance
Someone calls this technique as the viewdependent level-of-detail
Very good for fly-simulation-like application
193
Level-of-detail Suggestion



Apply progressive mesh for multi-resolution model
generation
Use in-game discrete LOD for performance tuning
Why ?
– For modern game API / platform, dynamic vertex
update is costly on performance
» Lock video memory stall CPU/GPU performance
194
A Model File Format, CW3 - Introduction




With .cw3 extension
A generic file form used in TheFly3D
ASCII format
Current Version 1.0 alpha
195
A Model File Format, CW3 - Example
# CW3 model file, designed by Chuan-Chang Wang
Model v 1
version
VertexType position color texture 2 2 2
UseVertexShader VertexColor_tex1.lvs
vertex type
vertex shader used
Material 2
AnimTexture aaa 6
MultiTexture 2 a008 aaa00 Effect TextureMap 0 Lightmap 1
Vertex 1 6
-10.0 10.0 0.0 1.0 0.0 1.0 -1.0 1.0 -1.0 1.0
-10.0 -10.0 0.0 1.0 0.0 0.0 -1.0 0.0 -1.0 0.0
0.0 -10.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
10.0 -10.0 0.0 0.0 0.0 1.0 1.0 0.0 1.0 0.0
10.0 10.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0 1.0
0.0 10.0 0.0 1.0 1.0 1.0 0.0 1.0 0.0 1.0
Polygon 2
4 0 0 1 2 5 both
4 1 2 3 4 5 both
materials
vertices
topology
196
Bounding Volume





Bounding sphere
Bounding cylinder
Axis-aligned bounding box (AABB)
Oriented bounding box (OBB)
Discrete oriented polytope (k-DOP)
Bounding Sphere
Bounding Cylinder
AABB
k-DOP
OBB
197
Bounding Volume - Application



Collision Detection
Visibility Culling
Hit Test
198
Application Example - Bounding Sphere
B2
B1
D
c2
c1
Bounding sphere B1(c1, r1), B2(c2, r2)
If the distance between two bounding spheres is
larger than the sum of radius of the spheres, than
these two objects have no chance to collide.
D > Sum(r1, r2)
199
Application Example - AABB

Axis-aligned bounding box (AABB)
– Simplified calculation using axis-alignment
feature
– But need run-timely to track the bounding box
AABB
200
Application Example - OBB

Oriented bounding box (OBB)
– Need intersection calculation using the
transformed OBB geometric data
» 3D containment test
» Line intersection with plane

OBB
For games, 
201
Advanced
Scene Management
202
Advanced Scene Graphs

This is a game-type-oriented issue
 Bounding Volume Hierarchies (BVHs)
 Binary space partition trees (BSP Trees)

“Quake”

Octree
 PVS
 Culling Skills
203
Bounding Volume Hierarchies (BVHs)

Bounding spheres in hierarchy
R
B
204
BSP Tree

Two varients



Axis-aligned
Polygon-aligned
The trees are created by using a plane to divide the
space into two, and then sorting the geometry into
two spaces.
205
Axis-aligned BSP Tree
0
plane
plane
3
plane1
2
1
0
plane2
3
206
Polygon-aligned BSP Tree
F
A
C
G
B
A
B
C
D
E
D
E
F
G
207
Why BSP Tree ?

Quickly to identify where you are


BSP = Sorting
Need a pre-processor to generate the PVS


Visibility culling + occlusion culling
PVS : Possible Visible Set

Optimized for in-door game environment
 [Fuch80]



Fuchs, H.,
On Visible Surface Generation by a Priori Tree Structures,
Computer Graphics, 14, 124-33, (Proc. SIGGRAPH’80)
208
Octree & Quadtree





Very similar to axis-aligned BSP tree
Except that a box is split simultaneously along all
three axes
The split point must be the center of the box
This creates eight new boxes
Quadtree is the 2D version of octree
209
Quadtree - Example
210
Octree – Some Discussion

Data structure coherence
 Apply visibility culling from parents
 Split or not split ?
 Outdoor game scene ?
211
Culling (1/2)
Culling means “remove from a flock”
 Visibility culling




Remove the object not in view frustum
A “must” for game engine
Backface culling

Remove the polygons facing away from camera
 Hardware standard

Occlusion culling

Remove the objects hidden by the others
212
Culling (2/2)
View frustum
Occlusion
culling
eye
Visibility
culling
Backface
culling
213
BSP Implementation

A Pre-processor
– Space partition the scene data from artist
– Generate the BSP data structure
– Generate the PVS

BSP Walk Through
– Identify the room where you are
– Show/hide the rooms according to the PVS
214
BSP Preprocessor (1/2)

Input
– A scene from artist
– Cutting planes (optional)
» Can be procedurally generated by algorithm
– Cutting policy
» Split or not split
– Ray casting resolution for PVS

Output
– A BSP file
» BSP Tree
» PVS
» Geometry Data
215
BSP Preprocessor (2/2)

Process
– Generate the BSP tree according to the cutting policy
– Split or sort the geometry into BSP room (leaves)
– For each “room”, ray cast all rooms to generate the
possible visible room set
– 3D
– Time consuming
216
BSP Challenges

Effectiveness of PVS
– Data set


Dynamic Objects
Room size
217
Terrain
218
Introduction


Game Type Oriented
Terrain
– For visual (廣義的場景)
» Ground / Building / Static models / Dynamic models
– For terrain following
» Polygon mesh
» Grids
– For path finding
» Polygon mesh
» Grids

Terrain Following
– Make a 3D entity walking on terrain

Path Finding
– Find a path before walking
219
Terrain Formats

Grid
– 2D
– Quadtree

Height map
– Procedural height map

ROAM
– Real-time Optimally Adapting Meshes

Triangular Mesh
– Procedurally generated
– Created by artists
Perlin Noise
220
Grid Map

2D Grid Map
– Rectangular or Hexagonal grids
– Attributes
» Height
» Walkable or not
» Texture pattern ID


Step Look Terrain
Application
– 2D games
– 3D games with god view
» 2D tile-based game terrain
221
Height Map

Almost as Same as 2D Grid Map but
–
–
–
–

Height on grid vertex
Only height is saved
Regular grid
Irregular grid but structured
Top view
Application
– As the base data structure for ROAM terrain
– Water simulation
222
ROAM

Real-time Optimally Adapting Mesh
– http://www.llnl.gov/graphics/ROAM/

Application
– Fly-simulation
223
Chunked LOD Terrain

Use quad tree to construct the level-of-detail of
terrain
– A quad tree for LOD
224
Triangular Mesh

Possibly the Most Popular Way for Games
– General
– Can be created by artists

Multiple-layered Terrain
225
Terrain Following Using Triangular Mesh

Solve the Terrain Height for the Object to Stand on
– Use the triangular coordinate system (p. 154)

Find the Next Neighboring Triangle
– Half-edge data structure
226
Half-edge (1/2)


Create cohesive relationship between triangles using
“half edge”
Use half-edge table to search the neighboring
triangles
Edge = two halves
227
Half-edge (2/2)
struct HE_edge
{
HE_vert* vert; // vertex at the end of the half-edge
HE_edge* pair; // oppositely oriented adjacent half-edge
HE_face* face; // face the half-edge borders
HE_edge* next; // next half-edge around the face
};
struct HE_vert
{
float x;
float y;
float z;
HE_edge* edge; // one of the half-edges
// emantating from the vertex
};
struct HE_face
{
HE_edge* edge; // one of the half-edges bordering the face
};
http://www.flipcode.com/tutorials/tut_halfedge.shtml
228
Game AI
Path Finding
229
Introduction to Path Finding


A Common Situation of Game AI
Path Planning
– From start position to the goal

Most Popular Technique
– A* (A Star)
»
»
»
»
1968
A search algorithm
Favorite teaching example : 15-pizzule
Algorithm that searches in a state space for the least costly path
from start state to a goal state by examining the neighboring
states
230
A* Algorithm (1/4)
The A* Algorithm
Open : priorityqueue of searchnode
Closed : list of searchnode
AStarSearch( location StartLoc, location GoalLoc, agenttype Agent) {
clear Open & Closed
// initialize a start node
StartNode.Loc = StartLoc;
StartNode.CostFromStart = 0;
StartNode.CostToGoal = PathCostEstimate(StartLoc, GoalLoc, Agent);
StartNode.TotalCost = StartNode.CostToGoal ;
StartNode.Parent = NULL;
push StartNode on Open;
// process the list until success or failure
while Open is not empty {
pop Node from Open // node has the lowest TotalCost
231
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
232
}
A* Algorithm (3/4)
if (NewNode is in Open) {
adjust NewNode’s position in Open
}
else {
Push NewNode onto Open
}
}
}
}
push Node onto Closed
}
}
233
A* Algorithm (4/4)

State
– Location
– Neighboring states

Search Space
–
–
–
–


Related to terrain format
Grids
Triangles
Points of visibility
Cost Estimate
Path
– Typical A* path
– Straight path
– Smooth path

Hierarchical Path Finding
234
Search Space & Neighboring States (1/2)

Rectangular Grid
– Use grid center

Quadtree
– Use grid center

Triangles or Convex Polygons
– Use edge mid-point
– Use triangle center
Rectangular Grid
Triangles
Quadtree
235
Search Space & Neighboring States (2/2)


Points of Visibility
Generalized Cylinders
– Use intersections
Points of Visibility
Generalized Cylinders
236
Cost Estimate

Cost Function
– CostFromStart
– CostToGoal

Minimum Cost
–
–
–
–
–
–
–

Distance traveled
Time of traveled
Movement points expended
Fuel consumed
Penalties for passing through undesired area
Bonuses for passing through desired area
…
Estimate
– To goal “distance”
237
Result Path
Typical A* Path
Straight Path
Smooth Path
238
Catmull-Rom Spline
Output_point = p1*(-0.5u3 +u2 - 0.5u) +
p2*(1.5u3 – 2.5u2 + 1) +
p3*(-1.5u3 + 2u2 + 0.5u) +
p4*(0.5u3 – 0.5u2)
spline output_points
p2
p1
p3
p4
239
Hierarchical Path Finding

Break the Terrain for Path Finding to Several Ones
Hierarchically
– Room-to-room
– 3D layered terrain
– Terrain LOD

Pros
– Speedup the search
– Solve the problem of layered path finding
240
Path Finding Challenges

Moving Goal
– Do you need to find path each frame ?

Moving Obstacles
– Prediction Scheme

Complexity of the Terrain
– Hierarchical path finding

“Good” Path
241
Game AI
Steering Behavior &
Group Movement
242
Introduction

Reference
– Craig W. Reynolds
– 1987 “Flocks, Herds, and Schools: A Distributed
Behavioral Model”, Siggraph’87 Proceedings
– 1999 “Steering Behaviors for Autonomous Characters”,
GDC Proceedings*
– www.red3d.com/cwr/steer

Autonomous Characters
– Autonomous agents
– NPCs in Games

Related Work
– Robotics
– Artificial Intelligence (AI)
– Artificial Life
243
Motion Behavior



Action Selection
Steering
Locomotion
A Hierarchy of Motion Behavior
244
Action Selection

Game AI engine
– State machine
» Discussed in next chapter
– Goals
– Planning
– Strategy


Scripting
Assigned by player
245
Steering


Discussed in This Chapter
Path Determination
– Path finding or path planning
– Discussed in the last chapter

Behaviors
–
–
–
–
–
–

Seek & flee
Pursuit & evasion
Obstacle Avoidance
Wander
Path following
Unaligned collision avoidance
Group Steering
246
Locomotion


Character Physically-based Models
Movement
– Turn Right, Move forward, …

Animation
– Quaternion

Implemented / Managed by Game Engine
247
A Simple Vehicle Model (1/2)

A Point Mass
– Linear momentum
– No rotational momentum

Parameters
– Mass
– Position
– Velocity
» Modified by applied forces
» Max speed

Top speed of a vehicle
– Max steering force
» Self-applied
– Orientation
» Car
» Aircraft
248
A Simple Vehicle Model (2/2)

Local Space
–
–
–
–

Origin
Forward
Up
Side
Steering Forces
– Asymmetrical
» Thrust
» Braking
» Steering

Velocity Alignment
– No slide, spin, …
– Turn
249
Euler Integration




Steer_force = Truncate(streer_direction, Max_force)
Acceleration = Steer_force / mass
Velocity = Truncate(Velocity + Acceleration,
Max_speed)
Position = Position + Velocity
250
Seek & Flee Behaviors

Pursuit to a Static Target
– Steer a character toward to a target position

Seek Steering force
– desired_velocity = normalize(target - position)*max_speed
– steering = desired_velocity – velocity


“A moth buzzing a light bulb”
Flee
– Inverse of Seek

Variants
– Arrival
– Pursuit to a moving target
251
Arrival Behavior

A Stopping Radius
– Outside the radius, arrival is identical to seek
– Inside the radius, the speed is ramped down to zero
»
»
»
»
»
»
target_offset = target – position
distance = length(target_offset)
ramped_speed = max_speed*(distance/slowing_distance)
clipped_speed = minimum(ramped_speed, max_speed)
desired_velocity = (clipped_speed/distance)*target_offset
steering = desired_velocity – Velocity
252
Pursuit & Evasion Behaviors



Target Character is Moving
Apply Seek or Flee to the Target’s Predict Position
Estimate the Prediction Interval T
– T = Dc
– D = distance(Pursur, Quarry)
– c = turning parameter

Variants
– Offset pursuit
» “Fly by”
253
Obstacle Avoidance Behavior



Use Bounding Sphere
Not Collision Detection
Probe
– A cylinder lying along forward axis
– Diameter = character’s bounding sphere
– Length = speed (means Alert range)

Find the most Threaten Obstacle
– Nearest intersected obstacle

Steering
254
Wander Behavior


Random Steering
One Solution :
– Retain steering direction state
» Constrain steering force to the sphere surface located slightly
ahead of the character
– Make small random displacements to it each frame
» A small sphere on sphere surface to indicate and constrain the
displacement

Another one :
– Perlin noise

Variants
– Explore
– Forage
255
Path Following Behavior

A Path
– Spine
» A spline or poly-line to define the path
– Pipe
» The tube or generated cylinder by a defined “radius”

Following
– A velocity-based prediction position
» Inside the tube

Do nothing about steering
» Outside the tube


“Seek” to the on-path projection
Variants
– Wall following
– Containment
256
Flow Field Following Behavior


A Flow Field Environment is Defined
Virtual Reality
– Not common in games
257
Unaligned Collision Avoidance Behavior


Turn Away from Possible Collision
Predict the Potential Collision
– Use bounding spheres

If possibly collide,
– Apply the steering on both characters
– Steering direction is possible collision result
» Use “future” possible position
» The connected line between two sphere centers
258
Steering Behaviors for Groups of Characters


Steering Behaviors Determining How the Character
Reacts to the Other Characters within His Local
Neighborhood
The Behaviors include
– Separation
– Cohesion
– Alignment
259
The Local Neighborhood of a Character

The Local Neighborhood is Defined
– A distance
– The field-of-view
» Angle
The Neighborhood
260
Separation Behavior

Make a Character to Maintain a Distance from Others
Nearby
– Compute the repulsive forces within local neighborhood
» Calculate the position vector for each nearby
» Normalize it
» Weight the magnitude with distance

1/distance
» Sum the result forces
» Negate it
261
Cohesion Behavior

Make a Character to Cohere with the others Nearby
– Compute the cohesive forces within local neighborhood
» Compute the average position of the other nearbys

Gravity center
» Apply “Seek” to the position
262
Alignment Behavior

Make a Character to Align with the Others Nearby
– Compute the steering force
» Average the together velocity of all other characters nearby
» The result is the desired velocity
» Correct the current velocity to the desired one with the steering
force
263
Flocking Behavior

Boids Model of Flocks
– [Reynolds 87]

Combination of
– Separation steering
– Cohesion steering
– Alignment steering

For Each Combination
– A weight for combing
– A distance
– An Angle
264
Leader Following behavior

Follow a Leader
– Stay with the leader
» “Pursuit” behavior (Arrival style)
– Stay out of the leader’s way
» Defined as “next position” with an extension
» “Evasion” behavior when inside the above area
– “Separation” behavior for the followers
265
Behavior Conclusion


A Simple Vehicle Model with Local Neighborhood
Common Steering Behaviors
–
–
–
–
–
–
–
–
–

Seek
Flee
Pursuit
Evasion
Offset pursuit
Arrival
Obstacle avoidance
Wander
Path following
Combining Behaviors
–
–
–
–
–
–
–
–
–
Wall following
Containment
Flow field following
Unaligned collision avoidance
Separation
Cohesion
Alignment
Flocking
Leader following
266
Game AI
Finite State Machine
267
Introduction (1/2)

Finite State Machine (FSM) is the Most Commonly
used Game AI Technology Today
–
–
–
–

Simple
Efficient
Easily extensible
Powerful enough to handle a wide variety of situations
Theory (Simplified)
– A set states, S
– An input vocabulary, I
– Transition function, T(s, i)
» Map a state and an input to another state
268
Introduction (2/2)

Practical Use
– State
» Behavior
– Transition
» Across states
» Conditions
– It’s all about driving behavior

Flow-chart Diagram
– UML State Chart
» Arrow

Transition
» Rectangle

State
269
An Example of FSM As a Diagram
Monster in sight
Gather
Treasure
No monster
Flee
Fight
270
FSM for Games



Character AI
“Decision-Action” Model
Behavior
– Mental State

Transition
– Players’ action
– The other characters’ actions
– Some features in the game world
271
Implement FSM

Code-based FSM
– Simple Code One Up
» Straightforward
» Most common
– Macro-assisted FSM Language

Data-Driven FSM
– FSM Script Language
272
Coding an FSM – Code Example 1
void RunLogic(int *state)
{
switch(*state)
{
case 0: // Wander
Wander();
if (SeeEnemy()) *state = 1;
if (Dead()) *state = 2;
break;
case 1: // Attack
Attack();
*state = 0;
if (Dead()) *state = 2;
break;
case 2: // Dead
SlowlyRot();
break;
}
}
273
Coding an FSM – Code Example 2
void RunLogic(FSM *fsm)
{
// Do action based on the state and determine next input
input = 0;
switch(fsm->GetStateID())
{
case 0: // Wander
Wander();
if (SeeEnemy()) input = SEE_ENEMY;
if (Dead()) input = DEAD;
break;
case 1: // Attack
Attack();
input = WANDER;
if (Dead()) input = DEAD;
break;
case 2: // Dead
SlowlyRot();
break;
}
// DO state transition based on computed input
274
fsm->StateTransition(input);
}
Mealy & Moore Machines

Mealy Machine
– A Mealy machine is an FSM whose actions are performed
on transitions

Moore Machine
– A Moore machine’s actions reside in states
– More intuitive for game developers
275
FSM Language Use Macros

Coding a State Machine Directly Causes Lack of
Structure
– Going complex when FSM at their largest


Use Macro
Beneficial Properties
– Structure
– Readability
– Debugging

Simplicity
276
FSM Language Use Macros – An Example
#define BeginStateMachine …
#define State(a) …
…
bool MyStateMachine::States(StateMachineEvent event,
int state)
{
BeginStateMachine
State(0)
OnUpdate
Wander();
if (SeeEnemy()) SetState(1);
if (Dead()) SetState(2);
State(1)
OnUpdate
Attack();
SetState(0);
if (Dead()) SetState(2);
State(2);
OnUpdate
RotSlowly();
EndStateMachine
}
277
Data-Driven FSM

Scripting Language
– Text-based script file
– Transformed into
» C++

Integrated into source code
» Bytecode


Interpreted by the game
Authoring
– Compiler
– AI editing tool

Game
– FSM script engine
– FSM interface
278
Data-Driven FSM Diagram
Authoring
Artist,
Designers, &
Developers
FSMs
AI Editing
Tool
Condition &
Action
Vocabulary
Compiler
Games
bytecode
FSM Script
Engine
FSM Interface
Condition &
Action Code
Game Engine
279
AI Editing Tool for FSM

Pure Text
– Syntax ?


Visual Graph with Text
Used by Designers, Artists, or Developers
– Non-programmers

Conditions & Action Vocabulary
–
–
–
–
SeeEnemy
CloseToEnemy
Attack
…
280
FSM Interface



Facilitating the Binding between Vocabulary and
Game World
Glue Layer that Implements the Condition & Action
Vocabulary in the Game World
Native Conditions
– SeeEnemy(), CloseToEnemy()

Action Library
– Attack(…)
281
FSM Script Language Benefits




Accelerated Productivity
Contributions from Artists & Designers
Ease of Use
Extensibility
282
Processing Models for FSMs

Processing the FSMs
– Evaluate the transition conditions for current state
– Perform any associated actions

When and How ?
– Depend on the exact need of games

Three Common FSM Processing Models
– Polling
– Event-driven
– Multithread
283
Polling Processing Model

Processing Each FSM at Regular Time Intervals
–
–
–
–

Tied to game frame rate
Or some desired FSM update frequency
Limit one state transition in a cycle
Give a FSM a time-bound
Pros
– Straightforward
– Easy to implement
– Easy to debug

Cons
– Inefficiency
» Some transition are not necessary to check every frame

Careful Design to Your FSM
284
Event-driven Processing Model



Designed to Prevent from Wasted FSM Processing
An FSM is Only Processed When It’s relevant
Implementation
– A Publish-subscribe messaging system (Observer pattern)
– Allows the engine to send events to individual FSMs
– An FSM subscribes only to the events that have the
potential to change the current state
– When an event is generated, the FSMs subscribed to that
events are all processed

“As-needed” Approach
– Should be much more efficient than polling ?

Tricky Balance for Fine-grained or Coarse-grained
Events
285
Multithread Processing Model


Both Polling & Event-Driven are Serially Processed
Multithread Processing Model
– Each FSM is assigned to its own thread for processing
– Game engine is running in another separate thread
– All FSM processing is effectively concurrent and
continuous
– Communication between threads must be thread-safe
» Using standard locking & synchronization mechanisms

Pros
– FSM as an autonomous agent who can constantly and
independently examine and react to his environment

Cons
– Overhead when many simultaneous characters active
– Multithreaded programming is difficult
286
Interfacing with Game Engine (1/2)

FSMs Encapsulate Complex Behavior Logic
– Decision, condition, action, …

Game Engine Does Corresponding
– Character animation, movements, sounds, …

The Interface :
– Code each action as a function
» Need recompile if any code is changed
» ie., FleeWolf()
– Callbacks
» Function pointers
» ie., actionFunction[fleeWolf]()
– Container method
» actionFunctions->FleeWolf();
» DLL
287
Interfacing with Game Engine (2/2)

Take TheFly3D as Example:
class AArmyUnit : public FnCharacter
{
…
void DoAttack(…);
}
AArmyUnit *army;
army->Object(…);
army->MoveForward(dist, …);
…
army->DoAttack(…);
288
FSM Efficiency & Optimization

Two Categories :
– Time spent
– Computational cost

Scheduled Processing
– Priority for each FSM
– Different update frequency

Load Balancing Scheme
– Collecting statistics of past performance & extrapolating


Time-bound for Each FSM
Do careful design
– At the design level

Level-of-detail FSMs
289
Level-Of-Detail FSMs

Simplify the FSM When the Player Won’t Notice the
Differences
– Outside the player’s perceptual range
– Just like the LOD technique used in 3D game engine

Three design Keys :
– Decide how many LOD levels
» How much development time available ?
» The approximation extent
– LOD selection policy
» The distance between the NPC with the player ?
» If the NPC can “see” the player ?
» Be careful the problem of “visible discontinuous behavior”
– What kind of approximations
» Cheaper and less accurate solution
290
Extending the Basic FSM

Extending States
– Begin-end block
BeginDoAction();
DoActions();
EndDoAction();

Stacks & FSMs
– Stack-based “history” of FSMs
» “Remember” the sequence of states passed through
» “Retrace” its steps at will
– Hierarchical FSM


Polymorphic FSMs
Fuzzy State Machine
– Combined with fuzzy logic
291
A Hierarchical FSM Example
Monster in sight
Gather
Treasure
No monster
Flee
Fight
Go To
Treasure
Find
Treasure
Find
Treasure
Gather
Treasure
Active FSM
Live
Take
Treasure
Stack
292
Another Hierarchical FSM Example
Done
Done
Patrol
Noise
Saw Enemy
Investigate
Attack
Saw Enemy
Patrol
Go to A
Look for
Intruders
noise
Investigate
Report
Noise
Go to B
Look for
Intruders
noise
Go Over
To Noise
Look for
Intruders
False
Alarm!
293
More Topics in Game AI





Scripting
Goal-based Planning
Rule-based Inference Engine
Neural Network
References
– Game Gems
– AI Game Programming Wisdom
294
Game Physics
295
Introduction to Game Physics

Traditional Game Physics
– Particle system
– Rigid body dynamics
– Flexible body dynamics

Some State-of-art Topics
– Car physics
– Fluid dynamics
– Rag-doll physics

Physics
–
–
–
–
–
Rigid body kinematics
Newton’s Laws
Forces
Momenta
Energy
296
Basic Concepts from Physics (1/2)

Newton’s Laws
– 1st Law
» “靜者恆靜,動者恆成等速度運動”
– 2nd Law
» F = ma = mdv/dt
– 3rd Law
» 作用力與反作用力

Forces
– Gravity / Spring forces / Friction / Viscosity
– Torque
»=rXF
– Equilibrium
297
Basic Concepts from Physics (2/2)

Momenta
– Linear momentum
– Angular momentum
– Moment of inertia
298
Particle Dynamics


Particles are objects with
– Mass
– Position
– Velocity
– Respond to forces
But no spatial extent (no size!)
– Point mass

Based on Newton Laws
– f = ma
..
– x=f/m
.
.
– v = f / m, x = v
299
Basic Particle System
typedef struct {
float m;
/*
float *x;
/*
float *v;
/*
float *f;
/*
} *Particle;
mass */
position */
velocity */
force accumulator */
x
v
f
m
states
typedef struct {
Particle *p /* array of pointers to particles */
int n;
/* number of particles */
float t;
/* simulation clock */
} *ParticleSystem;
Particle n time
x
v
f
m
x
v
f
m
x
v
f
m
x
x
v … v
f
f
m
m
300
/* 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];
}
}
301
/* 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++);
}
}
302
/* calculate derivative, place in dst */
void ParticleDerivative(ParticleSystem p, float *dst)
{
int i;
ClearForce(p);
ComputeForce(p);
for (i = 0;
*(dst++)
*(dst++)
*(dst++)
*(dst++)
*(dst++)
*(dst++)
}
i
=
=
=
=
=
=
< p->n; i++) {
p->p[i]->v[0];
p->p[i]->v[1];
p->p[i]->v[2];
p->p[i]->f[0]/p->p[i]->m;
p->p[i]->f[1]/p->p[i]->m;
p->p[i]->f[2]/p->p[i]->m;
}
303
/* 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;
}
304
Rigid Body Dynamics

Mass of a Body
– Mass center

Force
– Linear momentum
– P(t) = M v(t)
– Velocity (v)

Torque
– Angular momentum
– L(t) = I w(t)
– Local rotation (w)


Inertia Tensor
Reference
– www-2.cs.cmu.edu/afs/cs/user/baraff/www/pbm
305
Flexible Body Dynamics (1/2)

Particle-Spring Model
–
–
–
–
F=kx
Not a stress-strain model
Lack of Elasticity, Plasticity, & Viscous-Elasticity
Can be unstable
306
Flexible Body Dynamics (2/2)

Finite Element Method
 有限元素法
–
–
–
–
–

Solver for ODE/PDE
Boundary conditions
Energy equation
Stress-strain model
Very complicated computing process
Conservation of Energy
307
Advanced Topics in Game Physics




Fracture Mechanics (破壞力學模擬)
Fluid Dynamics (流體力學)
Car Dynamics (車輛動力學)
Rag-doll Physics (人體物理模擬)
308
Game FX
309
Introduction to Game FX


Improve the Visual & Sound Game Effects
Includes
–
–
–
–
–

Combat FX
Environment FX
Character FX
Scene FX
Sound FX
FX Editor Needed
– General 3D animation tools can not do it
» Key-frame system is not working
» FX animation is always



Procedurally
Related to the previous frame
Small Work But Large Effect
310
FX Editing Tool
311
Combat FX

During the Combat
– Weapon motion blur
– Weapon effect
– Skill effect

After the Combat
– Damage effect

FX Editor
312
Combat FX Example
313
Motion Blur – Image Solution

Computer Animation :
– Image solution
– Blending rendered image sequence
»
»
»
»
Render too many frames
Divide the frames
Average
Done!
314
Motion Blur – Geometry Solution


In Games, Use Transparent Objects to Simulate the
Motion Blur
“False” Motion Blur
– Tracking the motion path of the object
– Connecting them as a triangular mesh
– Use time-dependent semi-transparency to simulate the
“blur”
– The path can be smoothed using Catmull-Rom spline
» Local stability of the curve
315
FX Uses Texture Animation


Almost All Game FXs Use this Trick
Geometry Object on which the Texture Animation
Playing
–
–
–
–
–


Billboard
3D Plate
Cylinder
Sphere
Revolving a cross section curve
Texture Sequence with Color-key
Semi-transparent Textures
– Alpha blending
» Source color added to background

Demo!!!!
316
Particle System for FXs in Combat

The FXs
– Fire / exposure / smoke / dust


Initial Value + Time dependency
Combined with Billboard FX
– Billboard to play the texture animation
– Particle system to calculate the motion path


Gravity is the major force used
Emitter pattern
– Single emitter
– Area emitter
– Emitter on vertices

Demo !!!
317
Environment FX

Weather
– Use particle system
» Rain
» Snow
» Wind

Fog
– Traditional fog
» From near to far
» Hardware standard feature
– Volume fog
» Layered fog
» Use vertex shader

Day & Night
318
Character FX

Fatality
– Case by case and need creative solutions

Rendering Effects on Skins
–
–
–
–

Environment mapping
Bump map
Normal map
Multiple texture map
Flexible body
– Flexible body dynamics

Fur
– Real-time fur rendering

…
319
Scene FX – Sky Box



Use a very large box or dome-like model to surround
the whole game scene
Use textures on the box or dome as the backdrop
Use multiple textures and texture coordinates
animation to simulate the moving of the clouds
320
Scene FX – Len’s Flare


Runtime calculate the position and orientation of
the camera with the sun
Put textures to simulate the len’s flare
321
Scene FX – Light Scattering


Atmospheric Light Scattering
Caused by dust, molecules, or water vapor
– These can cause light to be:
» Scattered into the line of sight (in-scattering)
» Scattered out of the line of sight (out-scattering)
» Absorbed altogether (absorption)


Skylight and sun light
Can be Implemented by Vertex Shader
322
Scene FX – Light Scattering Examples
Without scattering
With scattering
323
Characters
324
Introduction


The Characters are the Actors of the Games.
Three Types of Characters :
– Segmented
– Mesh
– Bone-skin


Root-base Concept (P.128-129)
Production :
– 3D animation tools
»
»
»
»
3dsMax
Maya
Softimage
…
Base
– Motion capture (Mocap)
» For motion data
325
A Segmented Character



A character is composed by a set of models with
motion data to simulate a live creature in real
world
P. 126-127
Benefits
– Hierarchical structure
– Easy to implement in a scene tree

Drawbacks
– Segment-like
326
A Mesh Character

Vertex animation on skins
– Animated positional data on skins
– 3D warping

Benefits
– Easy to implement
– Flexible mesh in animation

Drawbacks
– No hierarchy
– No keyframes
– Massive dataset
327
A Bone-skin Character

Bone-Skin Skeleton
Bone A
– Hierarchical bones
– Skin deformation run-timely

Benefits
– Hierarchical structure
– Not segmented look

Skin
Drawbacks
– More complicated than the other
solutions
– Skin deformation need more CPU
cost than transformation only
Bone B
328
Motion Data



Euler Angles
Angular Displacement
Quaternion
– Slerp

But Finally They Will Be Converted into “Matrix”
329
Optical Motion Capture

Data Acquired
– From skin to joint (Mocap)
– From joint to skeleton (Post-processing)
– From skeleton to skin (In-game)


Device
The Shooting Plan
330
Data Acquirement During the Mocap
Raw Data (Positional Data)
Bio-Data
Joint
End Point
331
Bone-skin Implementation In Game
Skeletons
Skin
Bone-Skin
Skeletons
332
Mocap Devices
333
Planning a Mocap Shoot
 Starting Out –
Reviewing the
Animation List and
Flowchart
334
335
 Creating a Shot List
 Create a Database
 File Names
 Preliminary Shot List
A Data Record of Shot List
336
A Shoot List
337
 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
338
A Daily Schedule
339
340
341
342
Motion for Characters

Apply motion data on bones
(x,y,z,q,axis)
A
(q,axis)
Joint = pivot(px,py,pz) in A
B
<v’> = <V> [RB][TB][RA][TA]
From pivot
From position
343
Motion Editing



To Create More Animation from Limited Ones
Run-time or Pre-processing
Issues :
– Motion re-targeting
» Run-time
– Re-key-framing
» Pre-processing
– Interpolation
» Run-time
– Motion blending
» Run-time
344
Poses



A Set of Frame Data to Describe the Character’s
Motion
Walk, Run, Attack, …
Keyframed or Non-keyframed
walk
run
attack
fall
345
A Pose Definition Example
start_frame
raw_start_frame
end_frame
walk
raw_end_frame
cut_frame
Parameter {
raw_start_frame
raw_end_frame
start_frame
end_frame
cut_frame
play_speed
length
transition_mode
}
346
Play a Pose
walk
0
4
8
Frame 5.3
1.
2.
3.
4.
5.
6.
If the motion data is in quaternion form
Get the motion data on frame 5 & 6
Convert the data into quaternion format
Apply slerp(5, 6, 0.3) to get the interpolation on frame 5.3
Convert the result of step 3 into a rotation matrix
Apply the matrix to the object for its transformation
347
Pose Connection
cut_frame
Pose 1
start_frame
Pose 2
length
348
Pose Blending



Motion blending in run-time
Quaternion is used
“Blend Tree”
– Cross fade
– Countinuous blending
– Feather blending
349
Blend Tree

Reference
– Game Developers Conference 2003
– Proceedings CD, Programming Track
– “Animation Blending : Achieving Inverse Kinematics and
More”
– Jerry Edsall
– Mech Warrior blend tree
Walk
Forward
Motion
Fall Transition
Run
Fall
Down
350
Cross Fade
1
Pose 1
0
1
Pose2
0
351
Continuous Blending
1
Pose 1
0
1
Pose 2
0
352
Feather Blending

左右搏擊
Pose 1
Pose 2
Pose 3
353
Skin Deformation

Weights to Assign the Influences of the Deformation
by Bones on Skin Vertices
– 1-weight
– 2-weight
– N-weight


CPU cost
It’s Very Good for GPU to Perform the Calculation
Using Vertex Shader on DirectX
354
Bone A
(root object)
base
1.
2.
3.
4.
5.
base
Bone B
(Bone A’s child)
Apply motion data to bones
Convert the vertex from “base” space to its associated
bone’s space using the natural pose’s inverse
transformation
Multiple the influence weight
Accumulate all influences
Then the vertex is deformed by the bone in “base” space
355
A two-weight skin vertex example
Mb = RbTpivot
Ma = RaTposition
Mvb = Mnb-1 MbMa
Mva = Mna-1Ma
vin_base = vs*waMva +
vs*wbMvb
356
Network Gaming
357
Download