source 2006 presentation by luke arntson [email protected] game programming optimization
TRANSCRIPT
SOURCE 2006SOURCE 2006Presentation by Luke ArntsonPresentation by Luke Arntson
[email protected]@cwu.edu
Game Programming OptimizationGame Programming Optimization
Optimization ResearchOptimization Research
How important is optimization in games?How important is optimization in games?
Optimization is the most essential part of coding when it comes to game Optimization is the most essential part of coding when it comes to game development. Coders must always take into consideration the optimization development. Coders must always take into consideration the optimization of their solutions to ensure full-speed games.of their solutions to ensure full-speed games.
What types of optimization will we cover?What types of optimization will we cover?
There are many ways to properly optimize programs, including games. In There are many ways to properly optimize programs, including games. In this presentation, we will cover many ways of solving the same problems 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 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.creative with the solutions, and finding new ways to approach old problems.
What Games Are We Going To Cover?What Games Are We Going To Cover?
Tetris – the classic puzzle Tetris – the classic puzzle block gameblock game
Bullet Dodger – a vertical Bullet Dodger – a vertical space shooterspace shooter
Death Bomberman – a clone of Death Bomberman – a clone of the original Bombermanthe original Bomberman
Creating and Optimizing TetrisCreating and Optimizing Tetris
First, start with the layoutFirst, start with the layout
The most prevalent type of layout for the Tetris piece is a 4x4 array. 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. 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 By simplifying the Tetris piece itself, we can simplify the problem. I have implemented a system using the Tetris block coordinates in have implemented a system using the Tetris block coordinates in the 4x4 array, rather than the array itself.the 4x4 array, rather than the array itself.
Next, let us look at rotationsNext, let us look at rotations
So now that we have determined the Tetris pieces can be created So now that we have determined the Tetris pieces can be created with coordinates, let us observe how we can apply rotations to these with coordinates, let us observe how we can apply rotations to these pieces.pieces.
Layout of Tetris PiecesLayout of Tetris PiecesWith permission of Phil Hassey, code from FTetrisWith permission of Phil Hassey, code from FTetris
philhassey – FTetris @ http://www.imitationpickles.org/ftetris/philhassey – FTetris @ http://www.imitationpickles.org/ftetris/
1. Box shape1. Box shape2. Z shape2. Z shape3. T shape3. T shape
my Tetris examplemy 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)]1. Box shape 1. [(0,1), (0,0), (1,0), (1,1)]2. Z shape 2. [(0,0), (1,0), (2,1), (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)]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 See how much code was saved? Not only has this decreased the code amount significantly, but has also lead a path of optimization.amount significantly, but has also lead a path of optimization.
2. [(0,0,0,0),2. [(0,0,0,0), (1,1,0,0),(1,1,0,0), (0,1,1,0),(0,1,1,0), (0,0,0,0),](0,0,0,0),]
1. [(0,0,0,0),1. [(0,0,0,0), (0,1,1,0),(0,1,1,0), (0,1,1,0),(0,1,1,0), (0,0,0,0),](0,0,0,0),]
3.[(0,0,0,0),3.[(0,0,0,0), (1,1,1,0),(1,1,1,0), (0,1,0,0),(0,1,0,0), (0,0,0,0),](0,0,0,0),]
X 0 1 2 3Y
0
1
2
3
[(0,0,0,0),[(0,0,0,0), (1,1,1,0),(1,1,1,0), (0,1,0,0),(0,1,0,0),
(0,0,0,0),](0,0,0,0),]
Visual Example Of Phil Hassey’s CodeVisual Example Of Phil Hassey’s CodeHere 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
Visual Example Of Coordinates in TetrisVisual Example Of Coordinates in TetrisLet 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
X 0 1 2 3Y
0
1
2
3
We can now match the coordinates given with the code: [(0,0), (1,0), (2,0), (1,1)][(0,0), (1,0), (2,0), (1,1)]
(0,0)
(1,0)
(2,0)
(1,1)
How Do We Represent How Do We Represent Rotations?Rotations?
First, what is a set of rotations?First, what is a set of rotations?A set of rotations can be defined as a list of pieces. So using A set of rotations can be defined as a list of pieces. So using
(X,Y) coordinates, we can define the following:(X,Y) coordinates, we can define the following:
Z PieceZ Piece11stst.(0,0), (1,0), (2,1), (1,1) 2.(0,0), (1,0), (2,1), (1,1) 2ndnd.(0,1), (1,0), (1,1), (0,2).(0,1), (1,0), (1,1), (0,2)
T Shape PieceT Shape Piece11stst.(0,0), (1,0), (2,0), (1,1) 2.(0,0), (1,0), (2,0), (1,1) 2ndnd.(0,1), (1,0), (1,1), (1,2).(0,1), (1,0), (1,1), (1,2)
33rdrd.(0,1), (1,0), (2,1), (1,1) 4.(0,1), (1,0), (2,1), (1,1) 4 thth.(0,1), (0,0), (1,1), (0,2).(0,1), (0,0), (1,1), (0,2)
7 Shape Piece7 Shape Piece11stst.(0,0), (1,0), (1,1), (1,2) 2.(0,0), (1,0), (1,1), (1,2) 2ndnd.(0,1), (2,0), (2,1), (1,1) .(0,1), (2,0), (2,1), (1,1)
33rdrd.(0,1), (0,0), (1,2), (0,2) 4.(0,1), (0,0), (1,2), (0,2) 4 thth.(0,0), (1,0), (2,0), (0,1).(0,0), (1,0), (2,0), (0,1)
Optimizing The RotationOptimizing The Rotation
Now that we know our rotation, lets look at how we can Now that we know our rotation, lets look at how we can optimizeoptimize
Notice that different pieces have a different number of rotations. For 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 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 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.these pieces into sub-groups based on the number of rotations.
Break our shapes into sub-groups, and apply rotationBreak our shapes into sub-groups, and apply rotation
After each sub-group is found, rotation is simple. In high level code, if the 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 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-function is called. The number of states to rotate is determined by the sub-group given.group given.
Bullet Dodger: How To Optimize All Bullet Dodger: How To Optimize All Those Bullets?Those Bullets?
Code must be simple yet effectiveCode must be simple yet effective
For games such as Bullet Dodger, the speed of the frames are vital. If the 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, game had been written on a console such as the Gameboy Advance, limitations with the number of cycles would always be a factor of limitations with the number of cycles would always be a factor of programming.programming.
Collision checking only potentialsCollision checking only potentials
It would be pointless to check every single bullet to every single enemy or 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 player unless there was a potential of harm. By using a grid-collision system, collision checking can be brought down significantly while still system, collision checking can be brought down significantly while still maintaining a solid collision checking system.maintaining a solid collision checking system.
Simple Code With Effective ResultsSimple Code With Effective ResultsBullet patterns in commercial gamesBullet patterns in commercial games
All space shooting games, old and new, rely on the concept of All space shooting games, old and new, rely on the concept of
bullet patterns. In an older game such as Space Invaders, bullets simply 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 flew down in a straight line towards the player. Later games such as Gradius used simple angle calculations to shoot bullets at the player.Gradius used simple angle calculations to shoot bullets at the player.
Moving the bullets in a linear pathMoving the bullets in a linear pathApplying simple math skills, it is easy to find the calculation for 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 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. 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 By using the following code every frame, bullets can achieve a smooth travel across the playing field:travel across the playing field:
Bullet X Coordinate += cos(vector.angle) * vector.lengthBullet X Coordinate += cos(vector.angle) * vector.length
Bullet Y Coordinate += sin(vector.angle) * vector.lengthBullet Y Coordinate += sin(vector.angle) * vector.length
This simple calculation is used in Bullet Dodger to allow for a very 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 nice looking effect not only for the bullets fired, but also for the simple particle engine used when there is a collision.particle engine used when there is a collision.
Simple Yet Effective Bullet PatternsSimple Yet Effective Bullet PatternsApplying the unit circle to a bullet spreadApplying the unit circle to a bullet spread
Currently using Pi/2 + (-Pi/8 < x < Pi/8)Currently using Pi/2 + (-Pi/8 < x < Pi/8)
Collision Detection Using Grid-collisionCollision Detection Using Grid-collisionLet’s split our screen into separate grid sections.Let’s split our screen into separate grid sections.
Notice how the player ship and enemies are only in a few boxes, 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-collisionnot ALL boxes. This is the idea we will implement to our grid-collision
Observing Where Objects Can Collide In Our GridObserving Where Objects Can Collide In Our GridAreas of collisionAreas of collisionNow observe the red areas, see that the only places these objects can be 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 hit is within these red sections. So ONLY do tedious collision detection in these boxes areas, saving a vast amount of processor time.these boxes areas, saving a vast amount of processor time.
Death Bomberman: World Manager and OptimizationDeath Bomberman: World Manager and Optimization
Managing the world is difficultManaging the world is difficult
However, eliminating the need for objects to be self-sufficient However, eliminating the need for objects to be self-sufficient allows for much cleaner code. If an object tells the world its desired 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 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 allowed to do so. This can be applied to many different games, and has been applied to many coding schemes used by major companies.been applied to many coding schemes used by major companies.
How world management optimizes gamesHow world management optimizes games
By eliminating unnecessary checks such as wall-collision, game By eliminating unnecessary checks such as wall-collision, game code can run at full speed while the world takes care of all special cases. 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 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 fire burst checking for an object to burn, the world manager tells the object to burn, and the fire burst to die out.to burn, and the fire burst to die out.
Applying to Death BombermanApplying to Death BombermanUtilize the manager to make the world functionUtilize the manager to make the world function
By applying the world manager effectively, each object in the world By applying the world manager effectively, each object in the world will behave accordingly. Bombs will be placed correctly, and maps will know will behave accordingly. Bombs will be placed correctly, and maps will know which items to destroy and which to leave intact.which items to destroy and which to leave intact.
Questions?Questions?For more information on this presentation, For more information on this presentation,
feel free to visit my website at:feel free to visit my website at:
http://source.eyeforcode.com http://source.eyeforcode.com
or email me at or email me at
[email protected]@cwu.edu