dm6111 games engine architecture

60
Background image from http:// Background image from http:// fastrak.deviantart.com fastrak.deviantart.com / / DM6111 Games engine architecture DM6111 DM6111 Games engine architecture Games engine architecture Dioselin Gonzalez Jan – Feb 2007 Dioselin Gonzalez Jan – Feb 2007

Upload: others

Post on 03-Feb-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DM6111 Games engine architecture

Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//

DM6111Games engine architecture

DM6111DM6111Games engine architectureGames engine architecture

Dioselin GonzalezJan – Feb 2007

Dioselin GonzalezJan – Feb 2007

Page 2: DM6111 Games engine architecture

Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//

1. Intro1. Intro1. Intro

Page 3: DM6111 Games engine architecture

Some free/OS enginesSome free/OS engines

• XNA Game Studio Express– msdn.microsoft.com/directx

• OGRE– www.ogre3d.org

• Crystal Space– www.crystalspace3d.org/main/Main_Page

Page 4: DM6111 Games engine architecture

Some free/OS enginesSome free/OS engines

• Delta3D– www.delta3d.org

• Panda3D– www.panda3d.org

• Quake{2,3,4}– www.idsoftware.com

Page 5: DM6111 Games engine architecture

Some free/OS enginesSome free/OS engines

• HGE– hge.relishgames.com

• PopCap– developer.popcap.com

Page 6: DM6111 Games engine architecture

THE GAME LOOPTHE GAME LOOP

Page 7: DM6111 Games engine architecture

The main : why?The main : why?L o o o o o o pL o o o o o o p

EXCELEXCEL WOWWOW

Open documentOpen document

Wait for commandWait for command

Process commandProcess command

ExitExit

Linear + app waits for youLinear + app waits for you

Trademarks are property of their respective owners Trademarks are property of their respective owners

Page 8: DM6111 Games engine architecture

The main : why?The main : why?The main : why?L o o o o o o pL o o o o o o p

EXCELEXCEL WOWWOW

Open documentOpen document

Wait for commandWait for command

Process commandProcess command

ExitExit

Linear + app waits for youLinear + app waits for you

HumanHuman--Troll duelTroll duel

Hunter training Hunter training a peta pet

Monster attacking Monster attacking you from behindyou from behind

Trademarks are property of their respective owners Trademarks are property of their respective owners

Page 9: DM6111 Games engine architecture

The main : why?The main : why?The main : why?L o o o o o o pL o o o o o o p

EXCELEXCEL WOWWOW

NonNon--linear, simulation keeps running linear, simulation keeps running even when you don’t do anythingeven when you don’t do anything

HumanHuman--Troll duelTroll duel

Hunter training Hunter training a peta pet

Monster attacking Monster attacking you from behindyou from behind

Trademarks are property of their respective owners Trademarks are property of their respective owners

Open documentOpen document

Wait for commandWait for command

Process commandProcess command

ExitExit

Linear + app waits for youLinear + app waits for you

Page 10: DM6111 Games engine architecture

The main : why?The main : why?The main : why?L o o o o o o pL o o o o o o p

ONE computer, ONE cpu, processing ONE instruction at a time

(in a simplified example…)

Page 11: DM6111 Games engine architecture

The mainThe main L o o o o o o pL o o o o o o p

Process player inputProcess player input

Do game logicDo game logic

Present game Present game state to all viewsstate to all views

2525--30 fps 30 fps (at least)(at least)

Page 12: DM6111 Games engine architecture

TermsTerms

• Tick – one iteration

GameLogic

TickAllAI();

TickAllParticles();

TickAllPhysics();

TickAllMovement();

while (true) {GameAppLayer->GetUserInput(); GameLogic->TickAllAI();GameLogic->TickAllParticles();GameLogic->TickAllPhysics();GameLogic->TickAllMovement();

}

Main loopMain loop

IT3157 slides by Mr. Wee Juan TanIT3157 slides by Mr. Wee Juan Tan

Page 13: DM6111 Games engine architecture

Tick, tick, tick, tick…Tick, tick, tick, tick…

• Some TickXXX() can be executed quickly.– A simple A.I. processing takes << 0.01 ms to

complete

• Some take longer– Graphics may take >30ms

– Can be solved by using lower polygon count, hardware acceleration, scenegraphs, etc

IT3157 slides by Mr. Wee Juan TanIT3157 slides by Mr. Wee Juan Tan

Page 14: DM6111 Games engine architecture

Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//

2. GUI2. GUI2. GUI

Page 15: DM6111 Games engine architecture

Graphical User InterfaceGraphical User Interface

• 2D layer– scalar vector (SVG) or raster graphics [Robin Tan’s

IT3935 slides]

– Issues?

• Keyword: widget– Short for ‘window gadget’, it refers to

components in a graphical user interface [www.cs.ucc.ie/~dgb/courses/swd/glossary.html]

– Event-driven, derive from base class

Page 16: DM6111 Games engine architecture

Graphical User InterfaceGraphical User Interface

• GUI libraries– GLUT (freeGLUT), GLUI, DirectX UI, MFC

– SDL, wxWidgets, FLTK, Crazy Eddie GUI, OpenGUI, TrollTech’s QT, …

Page 17: DM6111 Games engine architecture

A “hello world” example…A “hello world” example…

Page 18: DM6111 Games engine architecture

Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//

3. Input3. Input3. Input

Page 19: DM6111 Games engine architecture

Game architectureGame architecture

Game Application LayerGame Application Layer

Game Logic Game View

McShaffryMcShaffry, M. (2005). Game coding complete. 2, M. (2005). Game coding complete. 2ndnd ed. Arizona, US: ed. Arizona, US: ParaglyphParaglyph Press.Press.

HW and OSHW and OS

Page 20: DM6111 Games engine architecture

Application layerApplication layer

Game application layer

DevicesDevices Operating System Game lifetime

InputInput FilesFiles RAMRAM TimeTime Language DLL Threads Network CoreLibs

MainLoop

Init &shutdown

McShaffryMcShaffry, M. (2005). Game coding complete. 2, M. (2005). Game coding complete. 2ndnd ed. Arizona, US: ed. Arizona, US: ParaglyphParaglyph Press.Press.

cross-platform libraries deal with underlying hardware device layer

Page 21: DM6111 Games engine architecture

An example…An example…

Page 22: DM6111 Games engine architecture

Viewing & Input Topics : Viewing & Input Topics : PickingPicking

Page 23: DM6111 Games engine architecture

To detect which objects are below the To detect which objects are below the mouse or in a square region of the window mouse or in a square region of the window

WarcraftWarcraft® III: Reign of Chaos™® III: Reign of Chaos™

©2002 Blizzard Entertainment, Inc.. All rights reserved. Reign o©2002 Blizzard Entertainment, Inc.. All rights reserved. Reign of Chaos is a trademark and f Chaos is a trademark and WarcraftWarcraft and Blizzard Entertainment are trademarks or and Blizzard Entertainment are trademarks or registered trademarks of Blizzard Entertainment, Inc. in the U.Sregistered trademarks of Blizzard Entertainment, Inc. in the U.S. and/or other countries.. and/or other countries.

Page 24: DM6111 Games engine architecture

PickingPicking

((win_xwin_x, , win_zwin_z))

((obj_xobj_x, , obj_yobj_y, , obj_zobj_z))

From Windows From Windows coordinates to coordinates to World coordinatesWorld coordinates

3D model from 3D model from www.itswww.its--ming.comming.com//

Page 25: DM6111 Games engine architecture

Picking in OpenGLPicking in OpenGL

• GL provides GL_SELECTION render mode

1. Init names and selection buffer#define BODY 1#define HEAD 2 glSelectBuffer(int buffersize, unsigned int *buffer);

2.Enter selection modeglRenderMode(GL_SELECT);

Page 26: DM6111 Games engine architecture

Picking in OpenGLPicking in OpenGL

• GL provides GL_SELECTION render mode

3. “Draw” the scene as usual, give “names” to objects and define “pickable” area. gluPickMatrix((double) xmouse, (double)

(viewport[3] - ymouse), pickwidth, pickheight, viewport);

glInitNames(0); glPushName(BODY); render_body; glPushName(BODY); //etc…

Page 27: DM6111 Games engine architecture

Picking in OpenGLPicking in OpenGL

• GL provides GL_SELECTION render mode

4. Leave selection mode and return to render modehits = glRenderMode(GL_RENDER);

5. Process the hit recordsif (hits != 0) {

//process, highlight, etc…//using selection buffer

}

Page 28: DM6111 Games engine architecture

Picking in OpenGLPicking in OpenGL

• Other methods– Render each primitive in a unique color

• Use glReadPixels() to read single pixel under mouse location

– Shoot a pick ray through the mouse location • Call gluUnProject() twice

Page 29: DM6111 Games engine architecture

Viewing & Input Topics : Viewing & Input Topics : ZoomingZooming

Page 30: DM6111 Games engine architecture

FrustumFrustum

Near (front)Near (front)

Far (back)Far (back)

3D model from 3D model from www.itswww.its--ming.comming.com//

Page 31: DM6111 Games engine architecture

FrustumFrustum

glFrustum(xmin,xmax, ymin, ymax, near, far)

gluPerspective(fovy, aspect, near, far)

fovyfovy

WidthWidth

HeightHeight

3D model from 3D model from www.itswww.its--ming.comming.com//

Page 32: DM6111 Games engine architecture

Q&D zoom in OpenGLQ&D zoom in OpenGL

1. Uniform scale on ModelView matrix• glScale3f(0.25f,0.25f,0.25f)

• Often results in clipping by z-near and z-far

2. A better method is to play with the view volume…

Page 33: DM6111 Games engine architecture

Q&D zoom in OpenGLQ&D zoom in OpenGL

Maintain a zoom factorGLfloat zoom_factor = 1.0f; // 0 <= zf <= 1

a. Modify field of viewgluPerspective (50.0*zoomFactor,

(float)width/(float)height, zNear, zFar);

OR

b. Change frustumglFrustum(left*zoomFactor, right*zoomFactor,

bottom*zoomFactor, top*zoomFactor, zNear, zFar);

Page 34: DM6111 Games engine architecture

Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//

4. Physics4. Physics4. Physics

Page 35: DM6111 Games engine architecture

Simultaneous eventsSimultaneous events

• How to make many things continuously happen at the same time?

Page 36: DM6111 Games engine architecture

Simultaneous eventsSimultaneous events

• How to make many things continuously happen at the same time?

Move/Process/Modify each one bit at a time

Page 37: DM6111 Games engine architecture

Simultaneous movementSimultaneous movement

Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3

Page 38: DM6111 Games engine architecture

Simultaneous movementSimultaneous movement

Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3

Page 39: DM6111 Games engine architecture

Simultaneous movementSimultaneous movement

Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3

Page 40: DM6111 Games engine architecture

Simultaneous movementSimultaneous movement

Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3

Page 41: DM6111 Games engine architecture

Simultaneous movementSimultaneous movement

Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3

When you play it at 30 fps….When you play it at 30 fps….

Page 42: DM6111 Games engine architecture

Basic movementBasic movement

Going one level down:

How to move each object?

Move all objectsMove all objects

Move car (a little)Move car (a little) Move ball (a little)Move ball (a little)

Page 43: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

1. For each object, store its position P and direction D

P = (P = (x,yx,y))

xx

yy

DirectionDirection

Page 44: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

2. Position and Direction are vectors, Direction is normalized.

P = (P = (x,yx,y))

xx

yy

D = (d1, d2)D = (d1, d2)

Page 45: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

3. Multiply D by a DisplacementFactor (df) and obtain a Displacement vector (DV)

P = (P = (x,yx,y))

xx

yyD = (d1, d2)D = (d1, d2)

DV = DV = dfdf*D*D= = dfdf*(d1,d2)*(d1,d2)= (= (dfdf*d1,df*d2)*d1,df*d2)

Page 46: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

4. Add P and Displacement vector to obtain new position

PP00 = (= (x,yx,y))

xx

yy D = (d1, d2)D = (d1, d2)DV = DV = dfdf*(d1,d2)*(d1,d2)

PP11 = (= (x,yx,y) + DV) + DV= (= (x,yx,y) + () + (dfdf*d1,df*d2)*d1,df*d2)= (= (x+dfx+df*d1, *d1, y+dfy+df*d2)*d2)

Page 47: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

4. Add P and Displacement vector to obtain new position

xx

yy

Page 48: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

Position (P)Position (P)

xx

yyDirection (D)Direction (D)

Displacement Vector Displacement Vector (DV)(DV)

Displacement Factor (Displacement Factor (dfdf) = ) = length(DVlength(DV))

Page 49: DM6111 Games engine architecture

• Displacement Factor (df) is a number

• A fixed df will result in same distance traveled each frame– But ????

Basic movement : common modelBasic movement : common model

Page 50: DM6111 Games engine architecture

FrameFrame--rate Independent rate Independent MovementMovement

Page 51: DM6111 Games engine architecture

Basic movement : common modelBasic movement : common model

Position (P)Position (P)

xx

yyDirection (D)Direction (D)

Displacement Vector Displacement Vector (DV)(DV)

Displacement Factor (Displacement Factor (dfdf) = ) = length(DVlength(DV))

Page 52: DM6111 Games engine architecture

• Displacement Factor (df) is a number

• A fixed df will result in same distance traveled each frameDifferent computers have different frame rates

Movement will be faster in some machines

Basic movement : common modelBasic movement : common model

Page 53: DM6111 Games engine architecture

Setting your car in cruise controlSetting your car in cruise control

See See http://auto.howstuffworks.com/cruisehttp://auto.howstuffworks.com/cruise--control.htmcontrol.htm

160 km/h160 km/h

160 km/h160 km/h

Pentium® DPentium® DGeForceGeForce® 7® 7

Pentium® IIPentium® IIGeForceGeForce® 2® 2

timetime(seconds)(seconds)tt00 tt11 tt22

Page 54: DM6111 Games engine architecture

Setting your car in cruise controlSetting your car in cruise control

See See http://auto.howstuffworks.com/cruisehttp://auto.howstuffworks.com/cruise--control.htmcontrol.htm

160 km/h160 km/h

160 km/h160 km/h

Pentium® DPentium® DGeForceGeForce® 7® 7

Pentium® IIPentium® IIGeForceGeForce® 2® 2

timetime(seconds)(seconds)tt00 tt11 tt22

Time becomes an important factorTime becomes an important factor

Page 55: DM6111 Games engine architecture

Keep track of…Keep track of…

1. At which speed do you want to travel?

160 Km160 Km 1 hour1 hour

? Km? Km 1/3600 hour1/3600 hour

? = 1/3600 x 160? = 1/3600 x 160

2. How much time has passed?

Page 56: DM6111 Games engine architecture

In your game…In your game…

Game_start {- Set desired speed- Record start time

}

Main_loop{1. See how much time has passed2. Calculate DF using time and speed

}

Page 57: DM6111 Games engine architecture

In your code…In your code…

Game_start {Direction = (0.0, 0.0, -1.0);Speed = 16; //this is world units per secondt0 = current_time;

}

Page 58: DM6111 Games engine architecture

In your code…In your code…

Game_start {Direction = (0.0, 0.0, -1.0);Speed = 16; //this is world units per secondt0 = current_time;

}Main_loop {

t1 = current_time;elapsed_time = t1 – t0;DF = Speed * elapsed_time;Position += DF * Direction;t0 = t1;

}

Page 59: DM6111 Games engine architecture

In your code…In your code…Game_start {

Direction = (0.0, 0.0, -1.0);Speed = 16; //this is world units per secondglutInit(&argc, argv);int t0 = glutGet(GLUT_ELAPSED_TIME); //milliseconds!

}

Main_loop {int t1 = glutGet(GLUT_ELAPSED_TIME); elapsed_time = (t1 – t0)/1000.0; //secondsDF = Speed * elapsed_time;Position += DF * Direction;t0 = t1;

}

Page 60: DM6111 Games engine architecture

Image credits / referencesImage credits / references

• http://devimg.net

• http://fly.srk.fer.hr/~unreal/theredbook/

• http://www.lighthouse3d.com/opengl/viewfrustum/

• http://www.opengl.org/resources/faq/technical/viewing.htm

• http://www.lighthouse3d.com/opengl/picking/

• http://www.cse.ucsc.edu/classes/cmps160/Fall01/picking.txt

• http://www.opengl.org/resources/faq/technical/selection.htm

• http://www.cs.utah.edu/~xchen/columbia/session2/lec31/html/index.html

• IT3157 slides prepared by Mr. Wee Juan Tan