Title: SOURCE 2006
1SOURCE 2006
Game Programming Optimization
- Presentation by Luke Arntson
- ArntsonL_at_cwu.edu
2Optimization Research
- How important is optimization in games?
- Optimization is the most essential part of
coding when it comes to game development. Coders
must always take into consideration the
optimization of their solutions to ensure
full-speed games. - What types of optimization will we cover?
- There are many ways to properly optimize
programs, including games. In this presentation,
we will cover many ways of solving the same
problems using optimization as key. The beauty of
programming is the ability to be creative with
the solutions, and finding new ways to approach
old problems.
3What Games Are We Going To Cover?
- Tetris the classic puzzle
- block game
- Bullet Dodger a vertical
- space shooter
- Death Bomberman a clone of
- the original Bomberman
4Creating and Optimizing Tetris
- First, start with the layout
- The most prevalent type of layout for the Tetris
piece is a 4x4 array. This array is used to tell
the game where the Tetris blocks will be. By
simplifying the Tetris piece itself, we can
simplify the problem. I have implemented a system
using the Tetris block coordinates in the 4x4
array, rather than the array itself. - Next, let us look at rotations
- So now that we have determined the Tetris pieces
can be created with coordinates, let us observe
how we can apply rotations to these pieces.
5Layout of Tetris Pieces
- With permission of Phil Hassey, code from FTetris
- philhassey FTetris _at_ http//www.imitatio
npickles.org/ftetris/ - 1. Box shape
- 2. Z shape
- 3. T shape
- my Tetris
example - Note Numbers represent distance from
top left corner - Numbers are also in order Left, Top,
Right, and Bottom - 1. Box shape 1.
(0,1), (0,0), (1,0), (1,1) - 2. Z shape 2.
(0,0), (1,0), (2,1), (1,1) - 3. T shape 3.
(0,0), (1,0), (2,0), (1,1) - See how much code was saved? Not only has this
decreased the code amount significantly, but has
also lead a path of optimization.
2. (0,0,0,0), (1,1,0,0),
(0,1,1,0), (0,0,0,0),
1. (0,0,0,0),
(0,1,1,0), (0,1,1,0),
(0,0,0,0),
3.(0,0,0,0), (1,1,1,0),
(0,1,0,0), (0,0,0,0),
6Visual Example Of Phil Hasseys Code
- Here is a visual representation of how the 4x4
array is being drawn in Fractal Tetris by Phil
Hassey.
- ? Let us pretend we are building a Tetris piece,
and the piece is made of four blocks
- ? Each block is represented as a 1 or a 0 in the
4x4 array
- ? We can now observe the array and watch how the
1s and 0s fill the grid
7Visual Example Of Coordinates in Tetris
- Let use now examine the code given for
coordinates of the piece, rather than a 4x4 array.
- ? Let us pretend we are building a Tetris piece,
and the piece is made of four blocks
- ? Each block has (x,y) coordinates on a 4x4 grid
- ? We can now match the coordinates given with the
code (0,0), (1,0), (2,0), (1,1)
8How Do We Represent Rotations?
- First, what is a set of rotations?
- A set of rotations can be defined as a list of
pieces. So using (X,Y) coordinates, we can
define the following - Z Piece
- 1st.(0,0), (1,0), (2,1), (1,1) 2nd.(0,1),
(1,0), (1,1), (0,2) - T Shape Piece
- 1st.(0,0), (1,0), (2,0), (1,1) 2nd.(0,1),
(1,0), (1,1), (1,2) - 3rd.(0,1), (1,0), (2,1), (1,1) 4th.(0,1),
(0,0), (1,1), (0,2) - 7 Shape Piece
- 1st.(0,0), (1,0), (1,1), (1,2) 2nd.(0,1),
(2,0), (2,1), (1,1) - 3rd.(0,1), (0,0), (1,2), (0,2) 4th.(0,0),
(1,0), (2,0), (0,1)
9Optimizing The Rotation
- Now that we know our rotation, lets look at how
we can optimize - Notice that different pieces have a different
number of rotations. For example, the large block
shape only has one rotation, and the Z shape has
two rotations, as where the T shape has four
rotations. Now we can break these pieces into
sub-groups based on the number of rotations. - Break our shapes into sub-groups, and apply
rotation - After each sub-group is found, rotation is
simple. In high level code, if the shape is in
the current state, increment or decrement
depending on which function is called. The number
of states to rotate is determined by the
sub-group given.
10Bullet Dodger How To Optimize All Those Bullets?
- Code must be simple yet effective
- For games such as Bullet Dodger, the speed of
the frames are vital. If the game had been
written on a console such as the Gameboy Advance,
limitations with the number of cycles would
always be a factor of programming. - Collision checking only potentials
- It would be pointless to check every single
bullet to every single enemy or player unless
there was a potential of harm. By using a
grid-collision system, collision checking can be
brought down significantly while still
maintaining a solid collision checking system.
11Simple Code With Effective Results
- Bullet patterns in commercial games
- All space shooting games, old and new, rely on
the concept of bullet patterns. In an older game
such as Space Invaders, bullets simply flew down
in a straight line towards the player. Later
games such as Gradius used simple angle
calculations to shoot bullets at the player. - Moving the bullets in a linear path
- Applying simple math skills, it is easy to find
the calculation for bullets. The simplest way to
sum this up is by imagining there is a velocity
vector to each bullet. The vector consists of an
angle, and a vector length. By using the
following code every frame, bullets can achieve a
smooth travel across the playing field - Bullet X Coordinate cos(vector.angle)
vector.length - Bullet Y Coordinate sin(vector.angle)
vector.length - This simple calculation is used in Bullet
Dodger to allow for a very nice looking effect
not only for the bullets fired, but also for the
simple particle engine used when there is a
collision.
12Simple Yet Effective Bullet Patterns
- Applying the unit circle to a bullet spread
- Currently using Pi/2 (-Pi/8 lt x lt Pi/8)
13Collision Detection Using Grid-collision
- Lets split our screen into separate grid
sections. Notice how the player ship and enemies
are only in a few boxes, not ALL boxes. This is
the idea we will implement to our grid-collision
14Observing Where Objects Can Collide In Our Grid
- Areas of collision
- Now observe the red areas, see that the only
places these objects can be hit is within these
red sections. So ONLY do tedious collision
detection in these boxes areas, saving a vast
amount of processor time.
15Death Bomberman World Manager and Optimization
- Managing the world is difficult
-
- However, eliminating the need for objects to be
self-sufficient allows for much cleaner code. If
an object tells the world its desired movement or
action, the world can then dictate whether or not
the object is allowed to do so. This can be
applied to many different games, and has been
applied to many coding schemes used by major
companies. - How world management optimizes games
- By eliminating unnecessary checks such as
wall-collision, game code can run at full speed
while the world takes care of all special cases.
For example when bombs explode, they create a
fire burst. Instead of each fire burst checking
for an object to burn, the world manager tells
the object to burn, and the fire burst to die
out.
16Applying to Death Bomberman
- Utilize the manager to make the world function
- By applying the world manager effectively, each
object in the world will behave accordingly.
Bombs will be placed correctly, and maps will
know which items to destroy and which to leave
intact.
17Questions?
- For more information on this presentation,
feel free to visit my website at - http//source.eyeforcode.com
-
- or email me at
- ArntsonL_at_cwu.edu