Java introduction

Lecture 13 --- Building a 3D-game using XNA
Elias Holmlid
1

First part
 General thoughts on game engine architecture,
and some XNA considerations

Second part
 Adding a skybox and mouse-look to our previous
code
Elias Holmlid
2
Engine (library)
 Shared package (library)
 The actual game
 Editor
 Playpen (sandbox)
 Content pipelineextensions

Elias Holmlid
3

Break down code into smaller components
 RenderingComponent
 PhysicsComponent
▪ Collision
▪ Response
 SoundComponent
 Components should not be aware of eachother
 You might use the already existing
GameComponent-class
Elias Holmlid
4



When working in teams, naturally breaks
down responsibilities
Easier to replace code
Alot easier to nail down bugs and
performance problems
Elias Holmlid
5
Elias Holmlid
6

Since components are not aware of
eachother, they have to use an indirect way
to communicate
 This can be accomplished using events
Elias Holmlid
7

EventManager
 Basically contains a dictionary from event type to
a list of listeners for each event
 When an event is sent, all the listeners are notified
Elias Holmlid
8

Examples
 PlayerDiedEvent
 GameAboutToPauseEvent
 LevelCompletedEvent
Elias Holmlid
9

Break it down into separable parts:
Elias Holmlid
10


Find a good way to quickly add new effects
For example, decide upon common variable /
semantic names for different common
 The application can then set the parameters
automatically for you
Elias Holmlid
11




SceneManager
CameraManager
LightManager
EffectManager
Elias Holmlid
12



A tree containing the entire scene
Objects can be placed and rotated relatively
to eachother
Easy to group objects
Elias Holmlid
13
Elias Holmlid
14
Elias Holmlid
15

Example1: EnemyObject
 Contains common stuff like position, model etc
 Enemies might have different behaviors:
ChaseBehavior, JumpAroundBehavior...

Example2: Camera
 Camera does not control itself, instead a
CameraController-class is moving the camera
Elias Holmlid
16

Use a fixed time-step or multiply all your
movement calculations with the time passed
since the last frame
 Game.IsFixedTimeStep = true/false
No ”best” approach: Shawn Hargreaves has a
good (as always) article about this:
http://blogs.msdn.com/b/shawnhar/archive/200
7/07/25/understanding-gametime.aspx

Elias Holmlid
17

Flips the back-buffer when the electron beam
travels to the top-left of the screen (a state
called vertical retrace)

SynchronizeWithVerticalRetrace property of
GraphicsDeviceManager
Elias Holmlid
18

The most important rule:
 Never allocate reference type objects (classes)
while the game is being played
 Value types are ok (structs, primitive types...)
 The garbage collector is a much bigger problem
on the Xbox360
Elias Holmlid
19

Garbage collection
 Memory allocated on the heap exceeds a given limit:
▪ GC kicks in to release unused memory


Unacceptable 1: GC triggers too often
Unacceptable 2: GC triggers infrequently but it
takes a looong time for it to complete

Read more here:
http://blogs.msdn.com/b/shawnhar/archive/2007
/07/02/twin-paths-to-garbage-collectornirvana.aspx
Elias Holmlid
20

Use pooling
 For particle systems, enemies etc
 Should use this in non-garbage collected
environments anyway
Elias Holmlid
21

So, allocate everything while the game is
loading
 You might also force garbage collection through
System.GC.Collect() which will reset memory
count to zero
▪ Do not do this while playing the game though!
Elias Holmlid
22

CPU or GPU bound?
Elias Holmlid
23

How can you tell if you are GPU/CPU bound?
 Some detective work is needed

Example: run the game at a tiny resolution.
Does the framerate improve?
 In that case, you are bound by the pixel shader
stage
▪ Try doing cheaper pixel calculations
▪ Or if you are happy with the framerate, you can do more
heavy vertex processing for free
Elias Holmlid
24

Two blog-posts I recommend:
 http://blogs.msdn.com/b/shawnhar/archive/2008/
03/31/an-elf-in-a-box.aspx
 http://blogs.msdn.com/b/shawnhar/archive/2008/
04/11/santa-s-production-line.aspx
 (Read them in that order)
 [email protected]
Elias Holmlid
25

Subscribe to Shawn Hargreaves blog:
 http://blogs.msdn.com/b/shawnhar/

Hang around at the Creators Club Forum:
 http://forums.xna.com/forums/

People there are really friendly, and if you’re
lucky, Shawn himself will answer your
questions 
Elias Holmlid
26


A cheap way to simulate scenery far far away
Imagine walking around in the center of a
textured sphere, where the sphere is always
moving along with you
Elias Holmlid
27

We can use a cubemap for the sky-texture
Elias Holmlid
28

Use the local position of the vertex as a direction
vector for lookup into the cube texture
Elias Holmlid
29


Turn off backside culling for sphere or change
cullmode (we’ll be inside the sphere)
Always pass depth test and write maximum
depth (1.0) for each pixel to depth-buffer
 Nothing in the scene should be farther away than
the sky
Elias Holmlid
30


Cubemaps constructed with special tools
CubeMapGen is a free tool from AMD:
http://developer.amd.com/gpu/cubemapgen/
pages/default.aspx
Elias Holmlid
31