dm6111 games engine architecture
TRANSCRIPT
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
Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//
1. Intro1. Intro1. Intro
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
Some free/OS enginesSome free/OS engines
• Delta3D– www.delta3d.org
• Panda3D– www.panda3d.org
• Quake{2,3,4}– www.idsoftware.com
Some free/OS enginesSome free/OS engines
• HGE– hge.relishgames.com
• PopCap– developer.popcap.com
THE GAME LOOPTHE GAME LOOP
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
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
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
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…)
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)
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
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
Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//
2. GUI2. GUI2. GUI
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
Graphical User InterfaceGraphical User Interface
• GUI libraries– GLUT (freeGLUT), GLUI, DirectX UI, MFC
– SDL, wxWidgets, FLTK, Crazy Eddie GUI, OpenGUI, TrollTech’s QT, …
A “hello world” example…A “hello world” example…
Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//
3. Input3. Input3. Input
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
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
An example…An example…
Viewing & Input Topics : Viewing & Input Topics : PickingPicking
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.
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//
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);
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…
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
}
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
Viewing & Input Topics : Viewing & Input Topics : ZoomingZooming
FrustumFrustum
Near (front)Near (front)
Far (back)Far (back)
3D model from 3D model from www.itswww.its--ming.comming.com//
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//
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…
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);
Background image from http://Background image from http://fastrak.deviantart.comfastrak.deviantart.com//
4. Physics4. Physics4. Physics
Simultaneous eventsSimultaneous events
• How to make many things continuously happen at the same time?
Simultaneous eventsSimultaneous events
• How to make many things continuously happen at the same time?
Move/Process/Modify each one bit at a time
Simultaneous movementSimultaneous movement
Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3
Simultaneous movementSimultaneous movement
Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3
Simultaneous movementSimultaneous movement
Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3
Simultaneous movementSimultaneous movement
Frame 0Frame 0Frame 1Frame 1Frame 2Frame 2Frame 3Frame 3
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….
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)
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
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)
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)
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)
Basic movement : common modelBasic movement : common model
4. Add P and Displacement vector to obtain new position
xx
yy
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))
• 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
FrameFrame--rate Independent rate Independent MovementMovement
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))
• 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
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
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
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?
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
}
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;
}
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;
}
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;
}
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